mysql_connect('127.0.0.1', 'code1', ''); mysql_select_db('code1'); mysql_query("set names 'utf8'"); //已知的数据变量有 $name = '李四'; $age = 18; $class = '高三一班'; $array=array($name,$age,$class); function insert($i){ $keys=join(",",array_keys($i)); $vals="'".join("','",array_values($i))."'"; $sql="insert into user ($keys) values ($vals)"; mysql_query($sql); $id=mysql_insert_id(); return $id; } $id=insert($array)||exit("添加记录失败"); function scan_noce($where){ $sql="select * from user where id=$where"; $res=mysql_query($sql); $res=mysql_fetch_array($res,MYSQL_ASSOC); print_r($res); } scan_once($id);
老师,为什么提示添加记录失败呢?排查了好久,问题似乎出在insert()函数的$sql语句中!