gpt4 book ai didi

php - MYSQL_INSERT 不工作

转载 作者:可可西里 更新时间:2023-11-01 08:51:25 25 4
gpt4 key购买 nike

我已经多次尝试插入数据库。这些值包含一个单引号 - 魔术引号被关闭,addslashes() 和 mysql_real_escape_string() 都转义了字符,但脚本没有添加到数据库就死了。我也手动逃脱了,但这也失败了。但是,即使删除撇号,脚本仍然会死掉。

错误是:Could not insert staff: 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 2 行的“11,Hazel,Blonde,从未错过一天的工作,毕业于伯克利,服务”附近使用的正确语法有人看到任何问题吗?

<?php
include('header.php');

$amount = 1;
$staffnum = '0101';
$height = array("5'11", "5'4", "6'2","5'5", "6'4");
$eye = array("Blue","Green","Hazel","Brown");
$hair = array("Brown", "Black", "Blonde", "Red");
$about1 = "Has never missed a day of work";
$about2 = "Graduated from Berkley";
$positions = array('Server, Bartender', 'Bartender, Host', 'Sever, Host, Bartender', 'Cocktail Server, Bartender, Server');
$img = "none";
// arrays
$times = 1;


while($times <= 50) {
$staffnum ++;
$heighta = mysql_real_escape_string($height[array_rand($height)]);
$eyea = mysql_real_escape_string($eye[array_rand($eye)]);
$haira = mysql_real_escape_string($hair[array_rand($hair)]);
$positionsa = mysql_real_escape_string($positions[array_rand($positions)]);
$about1 = mysql_real_escape_string($about1);
$about2 = mysql_real_escape_string($about2);
$img = mysql_real_escape_string($img);
$staffnum = mysql_real_escape_string($staffnum);

$insert_staff = "INSERT INTO staff (staffnum, img_link, height, eye, hair, abt1, abt2, titles)
VALUES ($staffnum, $img, $heighta, $eyea, $haira, $about1, $about2, $positionsa)";

$insert_query = mysql_query($insert_staff);

if($insert_query) {
?>

<center>
Member # <?php echo $staffnum; ?> has been added to the database.<br />
<?php
} else {

die('Could not insert staff: ' . mysql_error());

}

$times ++;
}

include('footer.php');
?>
<a href="staff_insert.php?page=1">Return To Staff Insert</a>
</center>

最佳答案

您需要在要插入的字符串变量周围加上引号:

$insert_staff = "INSERT INTO staff (staffnum, img_link, height, eye, hair, abt1, abt2, titles)
VALUES ('$staffnum', '$img', '$heighta', '$eyea', '$haira', '$about1', '$about2', '$positionsa')";

关于php - MYSQL_INSERT 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13502496/

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