【问题标题】:Inserting Data Into MySQL database with errors spitting back error?将数据插入 MySQL 数据库时出现错误吐回错误?
【发布时间】:2017-04-10 19:59:38
【问题描述】:

我正在尝试使用 php 将数据插入 mysql 数据库。看起来很简单。

在使用虚拟数据(只是文本字符串)时,它工作得非常好。当我用实际变量替换虚拟数据时,我得到了这个错误:

ERROR: Could not able to execute INSERT INTO tbl_users (id, timestamp, testid, email, wants_newsletter, ip, country) VALUES (NULL, CURRENT_TIMESTAMP, , , , , ). 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

下面是我正在使用的脚本。我 100% 确定所有变量都有效。我目前也让他们吐出虚拟数据。

$testid = 1;
$email = 'test@yahoo.com'
$wants_newsletter = true;
//get $ip
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}
$country = 'US';

$user_id = 100; 
$question = 14
$answer = 14    

function saveToDB() {

/* Attempt MySQL server connection. */
$link = mysqli_connect("localhost", "$user", "$pass", "$db");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Attempt insert query execution
$sql = "INSERT INTO tbl_users (id, timestamp, testid, email, wants_newsletter, ip, country) VALUES (NULL, CURRENT_TIMESTAMP, $testid, $useremail, $newsletter, $ip, $country)";

if(mysqli_query($link, $sql)){
    echo "Records inserted successfully.";
    $user_id = $link->insert_id;
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// Attempt insert query execution
$sql = "INSERT INTO tbl_answers (id, user_id, question_id, answer) VALUES (NULL, $user_id, $question, $answer)";

if(mysqli_query($link, $sql)){
    echo "Records inserted successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// Close connection
mysqli_close($link);


}//end saveToDB

saveToDB(); //Call saveToDB()

为了找出这个错误,我已经绞尽脑汁好几个小时了。它可能是我忽略的小东西,也许我需要一双新的眼睛。

任何帮助将不胜感激。

【问题讨论】:

  • 您应该使用准备好的语句。就目前而言,您有 string 类型变量,并且值周围没有引号。
  • 您需要在字符串周围加上单引号。 '$useremail'
  • 我尝试在字符串周围使用单引号,但我得到一个空(或 0)值
  • @AbraCadaver 这不是重复的帖子。
  • 您从哪里获得变量的值?我没有看到它们被设置。函数内部。

标签: php mysql insert syntax-error


【解决方案1】:

根据您收到的 SQL 错误,您传递给内插字符串的变量似乎没有设置。

在插入语句之前检查$testid$useremail$newsletter$ip$country 的值。

在任何情况下,正如其他人所建议的那样,无论哪种方式,您都希望使用准备好的语句。查看PDO objects

【讨论】:

  • 变量的值很好。我尝试了不同的方法,从将值用单引号括起来到根本没有 in 引号。脚本工作的唯一方法是,如果我将字符串值硬连接到 INSERT 脚本中。
  • 它们是全局定义的还是本地定义的?默认情况下,全局变量的 PHP 范围不会在函数内部扩展。因此,如果它们是全局变量,则需要在函数顶部使用 global 关键字,然后使用要使用的所有全局变量的名称。这声明这些变量名将在函数内全局解析
  • 如果即使这样做也有问题,并且变量肯定是在函数内部定义的,我会借此机会切换到使用带有 PDO 的准备好的语句。到目前为止,在 PHP 中运行 SQL 查询的更好方法
  • 是否有任何指南可以帮助我在 PDO 中重写它,而不是 PHP 文档?有时这些可能很难遵循。使用 PDO 是否有性能优势?
  • 我不确定它是否具有性能优势,但 PDO 更好,因为它将查询抽象化,并允许您以更高级别和更有条理的方式访问数据库。可以这么说,您编写的 SQL 越少,SQL 错误的空间就越小。此外,它还可以防止 SQL 注入。我个人不能保证任何指南,但这个看起来很有希望phpdelusions.net/pdo
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
相关资源
最近更新 更多