0

我的一些编码有一个小问题。出于某种原因,我的条目没有放入我的数据库中。任何建议将不胜感激!这是我的代码...

<?php 
    $dbhost="localhost"; 
    $dbname="DBNAME"; 
    $dbuser="USER"; 
    $dbpasswd="PASSWORD"; // connect to the db 

    $dbcxn = mysqli_connect($dbhost, $dbuser, $dbpasswd); 
    if (!$dbcxn) { 
        die('Could not connect: ' . mysql_error()); 
    } 

    $db_selected = mysqli_select_db($dbcxn, $dbname); 
    if (!$db_selected) { 
        die ('Can\'t use dbreviews : ' . mysql_error()); 
    } 

    $query = "INSERT INTO entries ( submitterFirstName, submitterLastName, submitterPhone, submitterEmail, referredFirstName, referredLastName, referredPhone, referredEmail, referredReason)
      VALUES ('$submitterFirstName', '$submitterLastName', '$submitterPhone', '$submitterEmail', '$referredFirstName', '$referredLastName', '$referredPhone', '$referredEmail', '$referredProject')";

    $result=mysqli_query($dbcxn, $query);

?>

4

3 回答 3

0

首先应该是代码格式化,它将帮助您阅读代码,从而更容易地发现您的错误。

$query = "

INSERT INTO 
    entries 
    (
    submitterFirstName, 
    submitterLastName, 
    submitterPhone,        
    submitterEmail, 
    referredFirstName, 
    " . 
       "referredLastName, 
    referredPhone, 
    referredEmail, 
    referredReason
    )
        " .
    " VALUES 
    (
    '$submitterFirstName', 
    '$submitterLastName', 
    '$submitterPhone', 
    '    $submitterEmail', 
    '$referredFirstName'," .
    "'$referredLastName', 
    '$referredPhone', 
    '$referredEmail',    
    '$referredProject'
    );
"

以上是将您的查询字符串拆分为几行,有一些错误应该立即显而易见吗?格式化后,我会echo $query查看 $query 的输出。

还可以尝试查看是否可以在不使用 php 的情况下进行插入(使用 mysql workbench、php admin 等),然后将其与您设置为 $query 的字符串值进行比较。

// less errors, please note that inside "" you can include php $vars without needing to escape.  

$query = "

INSERT INTO 
    entries 
    (
    submitterFirstName, 
    submitterLastName, 
    submitterPhone,        
    submitterEmail, 
    referredFirstName, 
    referredLastName, 
    referredPhone, 
    referredEmail, 
    referredReason
    )
 VALUES 
    (
    '$submitterFirstName', 
    '$submitterLastName', 
    '$submitterPhone', 
    '$submitterEmail', 
    '$referredFirstName',
    '$referredLastName', 
    '$referredPhone', 
    '$referredEmail',    
    '$referredProject'
    );
";
于 2012-06-01T15:40:23.883 回答
0

您要检查的第一件事是将查询回显给自己并仔细阅读。

其次,检查表结构。确保列名都拼写正确并且所有字段都存在于您的表中(我之前不小心忘记添加一列)。

第三,根据您的配置,您可能会或可能不会收到错误消息。但是,您可以手动检查。

if (!$result) {
  echo mysqli_error($dbcxn);
}
于 2012-06-01T15:15:14.270 回答
0

将您的查询变量更改为:

$query = "INSERT INTO entries " .
 "( submitterFirstName, submitterLastName, submitterPhone, submitterEmail, referredFirstName, " .
 " referredLastName, referredPhone, referredEmail, referredReason )" .
 " VALUES ('" .
    $submitterFirstName . "', '" .
    $submitterLastName  . "', '" .
    $submitterPhone     . "', '" .
    $submitterEmail     . "', '" .
    $referredFirstName  . "', '" .
    $referredLastName   . "', '" .
    $referredPhone      . "', '" .
    $referredEmail      . "', '" .
    $referredProject    . "')";

它应该可以工作。

建议使用mysqli prepare

于 2012-06-01T15:19:26.750 回答