1

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

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>

我的数据库名称“宠物”表名“数据”。该表包含 3 部分 first_name、last_name 和 phone 都是 varchar 类型

4

2 回答 2

0

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

$fields = implode(',', array_keys($good_data));
$values = implode(',', array_map(function($x) { return "'$x'"; }, $good_data));
$query = "INSERT INTO data ($fields) VALUES ($values)";
于 2013-09-14T16:40:59.837 回答
0

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

正确的语法是。

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

于 2013-09-14T17:06:05.923 回答