gpt4 book ai didi

php - 插入MySQL表错误

转载 作者:行者123 更新时间:2023-11-29 14:43:12 25 4
gpt4 key购买 nike

我想知道是否有人可以帮助我解决这个恼人的问题。尝试将一些数据插入表中。同时,我想省略一些字段,而不是在那里插入一些内容。由于某种原因,我收到解析错误:语法错误,意外的 T_CONSTANT_ENCAPSED_STRING 错误。我下面做错了什么吗?

我们将不胜感激您的帮助。

     <?php

function sqlEscape($string){
return "'".mysql_real_escape_string($string)."'";
}

if( $_GET['location'] == ''
|| $_GET['name'] == ''
|| $_GET['school'] == ''
|| $_GET['reason'] == ''
|| $_GET['address'] == ''
|| $_GET['postcode'] == ''
|| $_GET['email'] == ''
|| $_GET['telephone'] == '') {

exit('You missed a value');
}
include('theConfig.php');
$con = mysql_connect($host, $username, $password) or die(mysql_error()) ;


if (!$con){
die('Could not connect: ' . mysql_error());
} mysql_select_db($db, $con); //$description = mysql_real_escape_string($_GET[description]);


$sql = sprintf('INSERT INTO applications (location, name, school, reason, address, postcode, email, telephone, town, county, state, country)
VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,'','')',

sqlEscape($_GET['location']),
sqlEscape($_GET['name']),
sqlEscape($_GET['school']),
sqlEscape($_GET['reason']),

sqlEscape($_GET['address']),
sqlEscape($_GET['postcode']),
sqlEscape($_GET['email']),
sqlEscape($_GET['telephone']));

if (!mysql_query($sql,$con)){
die('Error: ' . mysql_error());
}
header('Location: thankyou.php');
mysql_close($con)

?>

最佳答案

您应该为城镇和县设置值 - 或设置默认值(与其他值一样的空字符串):

$sql = sprintf("INSERT INTO applications (location, name, school, reason, address, postcode, email, telephone, town, county, state, country)
VALUES(%s, %s, %s, %s, %s, %s, %s, %s, '','','','')", ... )

编辑:另外 - 使用双引号将第一个 sprintf 参数括起来,因为单引号用于...

关于php - 插入MySQL表错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7563070/

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