Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法更改mysqli错误_Php_Mysql - Fatal编程技术网

Php 无法更改mysqli错误

Php 无法更改mysqli错误,php,mysql,Php,Mysql,我创建了一个表单,其中插入了名字、姓氏和电话,但我有这个错误,无法确定它在哪里 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的文件 <!DO

我创建了一个表单,其中插入了名字、姓氏和电话,但我有这个错误,无法确定它在哪里

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”。该表包含三个部分:first_name、last_name和phone,它们都是varchar类型

您永远不会在查询中的值后面添加任何内容。尝试以下方法将字段名和值放入查询:

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

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

正确的语法是

$query=插入数据列\u名称值对应的\u值

尝试在$result之前回显$query变量,然后调试它。