【问题标题】:can not the mysqli error不能出现mysqli错误
【发布时间】:2013-09-14 16:11:47
【问题描述】:

我创建了一个插入拳头姓氏和电话的表单,但我有这个错误,无法弄清楚它在哪里

can't execute query.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

这是我的 php 代码 第一个名为 displayPhone.php 的文件

<!DOCTYPE html>
<html>
<?php
$labels=array("first_name"=>"First Name",
                "last_name"=>"Last Name",
                "phone"=>"Phone");
?>
<body>
<h3>please enter your phone number below</h3>
<form action='savePhone.php' method='POST'>
<?php
//loop that displays the form field
foreach($labels as $field =>$value)
{
    echo "$field <input type='text' name='$field' size='65' maxlenghth='65'/>";
}
echo "<input type='submit' value='submit phone number'/>";
?>

名为 savePhone.php 的第二个文件

<?php
$labels=array("first_name"=>"First Name",
                "last_name"=>"Last Name",
                "phone"=>"Phone");
?>
<body>
<?php
foreach($_POST as $field =>$value)
{
    if(empty($value))
    {
        $blank_array[]=$field;
    }
    elseif(preg_match("/name/i",$field))
    {
        if(!preg_match("/^[A-Za-z' -]{1,50}$/",$value))
        {
            $bad_format[]=$field;
        }
    }
    elseif($field=="phone")
    {
        if(!preg_match("/^[0-9)( -]{7,20}(([xX]|(ext)|(ex))?[ -]?[0-9]{1,7})?$/",$value))
        {
            $bad_format[]=$field;
        }
    }
}

if(@sizeof($blank_array)>0 or  @sizeof($bad_format)>0)
{
    if(@sizeof($blank_array)>0)
    {
        echo "<p>input";
        foreach($blank_array as $value)
        {
        echo "$labels[$value]";
        }
        echo "</p>";
    }

    if(@sizeof($bad_format)>0)
    {
        echo "<p>invalid format";
        foreach($bad_format as $value)
        {
            echo $labels[$value];
        }
        echo "</p>";
    }

//redisplay form
    echo "<hr/>";
    echo "enter phone number";
    echo "<form action='$_SERVER[PHP_SELF]' method='POST'>";

    foreach($labels as $field =>$label)
    {
        $good_data[$field]=strip_tags(trim($_POST[$field]));
        echo "$label <input type='text' name='$field' size='65' maxlength='65' value='$good_data[$field]'/>";   
    }
    echo "<input type='submit' value='submit phone number'/>";
exit();
}
else
{
    $user='root';
    $host='localhost';
    $password='root';
    $dbname='pet';
    $cxn=mysqli_connect($host,$user,$password,$dbname) or die("can't connect to server");
    foreach($labels as $field =>$value)
    {
        $good_data[$field]=strip_tags(trim($_POST[$field]));
            if($field=="phone")
            {
                $good_data[$field]=preg_replace("/[)( .-]/","",$good_data[$field]);
            }
        $good_data[$field]=mysqli_real_escape_string($cxn,$good_data[$field]);
    }
    $query="INSERT INTO data (";
    foreach($good_data as $field =>$value)
    {
        $query.="$field,";
    }
    $query.= ") VALUES (";
    $query=preg_replace("/,\)/",")",$query);
    $result=mysqli_query($cxn,$query) or die ("can't execute query.".mysqli_error($cxn));
    echo "<h4>member inserted </h4>";

}
?>
</body>

我的数据库名 'pet' 表名 'data'。该表包含 3 部分 first_name、last_name 和 phone 都是 varchar 类型

【问题讨论】:

  • 尝试在$result 之前回显$query 变量,然后对其进行调试。

标签: php mysql


【解决方案1】:

您永远不会在查询中的VALUES ( 之后添加任何内容。尝试这种将字段名称和值放入查询的方式:

$fields = implode(',', array_keys($good_data));
$values = implode(',', array_map(function($x) { return "'$x'"; }, $good_data));
$query = "INSERT INTO data ($fields) VALUES ($values)";

【讨论】:

    【解决方案2】:

    您没有在VALUES ( 之后插入任何内容。这是 MySQL 语法错误,因此查询没有被执行。

    正确的语法是。

    $query = "INSERT INTO data (column_names) VALUES (corresponding_values)";

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-21
      • 2015-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      • 2012-08-21
      • 2020-04-04
      相关资源
      最近更新 更多