您的 SQL 语法有错误...? [英] You have an error in your SQL syntax...?

查看:60
本文介绍了您的 SQL 语法有错误...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//谁能告诉我下面sql查询中的错误在哪里.

//Can anyone tell me where is the error in the below sql query.

$sql = "insert into $table ($columns) values ($values)";

/它给了我这个错误

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册>在第 1 行的)"附近使用正确的语法

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 ')' at line 1

推荐答案

您必须检查 $columns$values 变量.另外,为确保起见,请分别在列和值周围加上适当的引号.

You will have to check if there is no starting and trailing comma's in the $columns or $values variables. Plus, just to be sure, put appropriate quotes around the columns and values individually.

public function insert($data, $table) { 
$columns = ""; 
$values = ""; 

foreach ($data as $column=>$value) 
{
     $columns .= "`" . $column . "`, ";
     $values .=  "'" . $value . "', ";
 }

 $columns = substr($columns, 0, strlen($columns)-2);
 $values = substr($values, 0, strlen($values)-2);
 $sql = "insert into ".$table." (".$columns.") values (".$values.")"; mysql_query($sql) or die (mysql_error()); return mysql_insert_id(); }

这篇关于您的 SQL 语法有错误...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆