gpt4 book ai didi

php - 不能mysqli报错

转载 作者:行者123 更新时间:2023-11-30 23:13:43 24 4
gpt4 key购买 nike

我创建了一个表格来插入名字姓氏和电话但是我有这个错误并且无法弄清楚它在哪里

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 类型

最佳答案

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

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

关于php - 不能mysqli报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18803688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com