Home  >  Article  >  Backend Development  >  php插入数据到MYSQL中出错,怎么正确插入呢

php插入数据到MYSQL中出错,怎么正确插入呢

WBOY
WBOYOriginal
2016-06-13 10:19:31781browse

php插入数据到MYSQL中出错,如何正确插入呢?
//php代码如下:


//UseInsert.php
  function getDatabase(){  
  $link=mysql_connect("localhost","root","mysql");//连接服务器
  mysql_query("SET CHARACTER SET GB2312"); //数据库数据以gb2312存储的
  if($link){ //连接服务器成功
  $db="studentdata"; //选择数据库  
  if(!mysql_select_db($db,$link))echo "选择数据库".$db."失败
";
  else echo "选择数据库".$db."成功
";
  }
  else echo "连接服务器失败


".mysql_error(); 
  }
//插入数据
  getDatabase();
  mysql_query("SET NAMES GB2312");
  $readSelect=mysql_query("select * from student");
  $array=mysql_fetch_array($readSelect,MYSQL_ASSOC);
  for(;$array;$array=mysql_fetch_array($readSelect,MYSQL_ASSOC)){
  echo $array['id']." ".$array['name']."
";
  }
  $id="1234123";
  $name="张学究";
  $insert="INSERT ignore into student('id','name')values($id,$name)";  
  mysql_query($insert) or die("
插入失败".mysql_error());  
   
?>

//网页输出:
选择数据库studentdata成功
1110009999 李四
1110009997 张三
1110009996 王五

插入失败You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''id','name')values(1234123,张学究)' at line 1



如何正确插入呢?

------解决方案--------------------
$insert="INSERT ignore into student('id','name')values($id,'$name')";
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn