最初の名前の姓と電話番号を挿入するフォームを作成しましたが、このエラーが発生し、どこにあるのかわかりません
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 という名前の 2 番目のファイル
<?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>
私のデータベース名「ペット」テーブル名「データ」。テーブルには first_name、last_name、および phone の 3 つの部分が含まれ、すべて varchar 型です