gpt4 book ai didi

PHP Mysql解析错误

转载 作者:行者123 更新时间:2023-11-29 04:21:19 25 4
gpt4 key购买 nike

我正在使用此代码,将通过 POST 发送的数据插入到我的数据库中。

mysql_query("INSERT INTO pending 
(name, alter, mail, kd, steam, spiele)
VALUES
('$_POST["name"]', '$_POST["alter"]', '$_POST["mail"]', '$_POST["kd"]', '$_POST["steam"]', '$_POST["spiele"]')");

但 PHP 不断抛出以下错误:

PHP Parse error: syntax error, unexpected '"', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /var/www/bewerben.php on line 34

我看不到那里的问题,因为我认为那里需要“”?

最佳答案

您的报价排序有误。您可以使用花括号来转义变量:

mysql_query("INSERT INTO pending 
(name, alter, mail, kd, steam, spiele)
VALUES
('{$_POST['name']}', '{$_POST['alter']}', '{$_POST['mail']}', '{$_POST['kd']}', '{$_POST['steam']}', '{$_POST['spiele']}')");

来自docs :

Complex (curly) syntax

This isn't called complex because the syntax is complex, but because it allows for the use of complex expressions.

Any scalar variable, array element or object property with a string representation can be included via this syntax. Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }.

为了进一步解释,我们有两个变量:

$fruit = 'Orange';
$sentence = "$fruits are my favorite fruit";

我想要得到的是:Oranges are my favorite fruit。但是,这是行不通的。 PHP 将转而寻找一个名为 $fruits 的变量,当它找不到时,它会显示一个错误。

因此,为了正确完成任务,我们必须将变量用大括号括起来{ }:

$fruit = 'Orange';
$sentence = "{$fruit}s are my favorite fruit";

太棒了!现在 PHP 将知道变量名的结束位置和字符串的开始位置。

附言我推荐使用 mysqli。

我不太确定我是否有权在此处编写此内容,但我查看了您的代码并发现了一个问题:

You are using mysql_* which is deprecated since PHP 5.5.0. This extension is deprecated as of PHP 5.5.0, and will be removed in the future.

您可以使用 PDOMySQLi 代替 mysql_*

这是一个使用 MySQLi 的简单示例:

$mysqli = mysqli_init();
$mysqli->real_connect($db_host, $db_user, $db_pass, $db_name);

然后您可以准备您的查询:

$stmt = $mysqli->prepare("insert into table values(?, ?)");

绑定(bind)参数:

$stmt->bind_param("is", $param1, $param2);

我们说第一个变量 $param1 是一个 integer$param2 是一个 string

我们需要做的最后一件事是为这些变量分配一些值并执行语句。

$param1 = 1;
$param2 = "somestring";
$stmt->execute();

关于PHP Mysql解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23127136/

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