当运行此代码时,我得到此错误,我使用它向数据库添加特定的过滤器。
<?php
//Connect to Databaseeee
$con=mysqli_connect("127.0.0.1","root","Toom13371!","filter_database_test");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// sql insertationerino
$sql = INSERT INTO filterlist ('type_code', 'connector_type', 'construction_type', 'author', 'estimate_lead_time', 'last_update', 'confidence_level', 'passtype', 'start_passband', 'stop_passband', 'low_stopband', 'high_stopband', 'low_passband', 'high_passband', 'start_stopband', 'stop_stopband') VALUES ('".$_POST['type_code']."', '".$_POST['connector_type']."', '".$_POST['construction_type']."', '".$_POST['author']."', '".$_POST['estimate_lead_time']."', '".$_POST['last_update']."', '".$_POST['confidence_level']."', '".$_POST['passtype']."', '".$_POST['start_passband']."', '".$_POST['stop_passband`']."', '".$_POST['low_stopband']."', '".$_POST['high_stopband']."', '".$_POST['low_passband']."', '".$_POST['high_passband']."', '".$_POST['start_stopband']."', '".$_POST['start_stopband']."');
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 Filter Added";
mysqli_close($con);
?>
完整的错误是:
解析错误:语法错误,意外的'INTO' (T_STRING)在C:'xampp'htdocs'html'addprocess.php第7行
首先你需要学习基本的php。
- 任何字符串都应该在引号内。
- 您需要在使用mysqli_real_escape_string() 将值放入查询之前进行转义
试试:
$sql = "INSERT INTO filterlist (`type_code`, `connector_type`, `construction_type`,
`author`, `estimate_lead_time`, `last_update`, `confidence_level`, `passtype`,
`start_passband`, `stop_passband`, `low_stopband`, `high_stopband`, `low_passband`,
`high_passband`, `start_stopband`, `stop_stopband`) VALUES
('".mysqli_real_escape_string($con,$_POST['type_code'])."',
'".mysqli_real_escape_string($con,$_POST['connector_type'])."',
'".mysqli_real_escape_string($con,$_POST['construction_type'])."',
'".mysqli_real_escape_string($con,$_POST['author'])."',
'".mysqli_real_escape_string($con,$_POST['estimate_lead_time'])."',
'".mysqli_real_escape_string($con,$_POST['last_update'])."',
'".mysqli_real_escape_string($con,$_POST['confidence_level'])."',
'".mysqli_real_escape_string($con,$_POST['passtype'])."',
'".mysqli_real_escape_string($con,$_POST['start_passband'])."',
'".mysqli_real_escape_string($con,$_POST['stop_passband`'])."',
'".mysqli_real_escape_string($con,$_POST['low_stopband'])."',
'".mysqli_real_escape_string($con,$_POST['high_stopband'])."',
'".mysqli_real_escape_string($con,$_POST['low_passband'])."',
'".mysqli_real_escape_string($con,$_POST['high_passband'])."',
'".mysqli_real_escape_string($con,$_POST['start_stopband'])."',
'".mysqli_real_escape_string($con,$_POST['start_stopband'])."')";
if (!mysqli_query($con,$sql)){
die('Error: ' . mysqli_error($con));
}
echo "1 Filter Added";
mysqli_close($con);
将$sql语句替换为:
$sql = "INSERT INTO filterlist ('type_code', 'connector_type', 'construction_type', 'author', 'estimate_lead_time', 'last_update', 'confidence_level', 'passtype', 'start_passband', 'stop_passband', 'low_stopband', 'high_stopband', 'low_passband', 'high_passband', 'start_stopband', 'stop_stopband') VALUES ('".$_POST['type_code']."', '".$_POST['connector_type']."', '".$_POST['construction_type']."', '".$_POST['author']."', '".$_POST['estimate_lead_time']."', '".$_POST['last_update']."', '".$_POST['confidence_level']."', '".$_POST['passtype']."', '".$_POST['start_passband']."', '".$_POST['stop_passband`']."', '".$_POST['low_stopband']."', '".$_POST['high_stopband']."', '".$_POST['low_passband']."', '".$_POST['high_passband']."', '".$_POST['start_stopband']."', '".$_POST['start_stopband']."')";
在你的$sql = INSERT INTO
语句中如果你看最后,你有"');
,这应该是"'");
。此外,您的sql查询需要是字符串,您不能将其作为函数调用。把整个查询放到字符串中。所以应该是:
$sql = "INSERT INTO filterlist ('type_code', 'connector_type', 'construction_type', 'author', 'estimate_lead_time', 'last_update', 'confidence_level', 'passtype', 'start_passband', 'stop_passband', 'low_stopband', 'high_stopband', 'low_passband', 'high_passband', 'start_stopband', 'stop_stopband') VALUES ('".$_POST['type_code']."', '".$_POST['connector_type']."', '".$_POST['construction_type']."', '".$_POST['author']."', '".$_POST['estimate_lead_time']."', '".$_POST['last_update']."', '".$_POST['confidence_level']."', '".$_POST['passtype']."', '".$_POST['start_passband']."', '".$_POST['stop_passband`']."', '".$_POST['low_stopband']."', '".$_POST['high_stopband']."', '".$_POST['low_passband']."', '".$_POST['high_passband']."', '".$_POST['start_stopband']."', '".$_POST['start_stopband']."');";