php插入数据到MYSQL中出错,如何正确插入呢?
//php代码如下:
<?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."失败<br>";
else echo "选择数据库".$db."成功<br>";
}
else echo "连接服务器失败<hr>".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']."<br>";
}
$id="1234123";
$name="张学究";
$insert="INSERT ignore into student('id','name')values($id,$name)";
mysql_query($insert) or die("<br>插入失败".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')";