gpt4 book ai didi

php - 在 php 和 mysql 中使用占位符来防止注入(inject)

转载 作者:行者123 更新时间:2023-11-29 08:51:09 24 4
gpt4 key购买 nike

我知道 mysql_ 已被弃用,但我只是将它用作从书中学习的工具。

我正在尝试了解占位符,当我单击“添加记录”时出现以下错误:

INSERT failed: EXECUTE statement USING @first,@last,@email,@user 
Unknown prepared statement handler (statement) given to EXECUTE

使用以下代码:

if (isset($_POST['first']) && isset($_POST['last']) && isset($_POST['user_name']) && isset($_POST['email']))
{
$first = get_post('first');
$last = get_post('last');
$email = get_post('email');
$user_name = get_post('user_name');

// begin placeholde code
$query = 'PREPARE statement FROM "INSERT INTO user_master VALUES(?,?,?,?)"';
mysql_query($query);

$query = 'SET @first = "$first",' . 'SET @last = "$last",' . 'SET @email = "$email",' . 'SET @user_name = "$user_name",';
mysql_query($query);

$query = 'EXECUTE statement USING @first,@last,@email,@user';
mysql_query;

// end placeholder code

if(!mysql_query($query, $db_server)) echo "INSERT failed: $query <br />" . mysql_error() . "<br /><br />";
}


echo <<<END
<form action = "willingLog.html" method="post"><fieldset><legend>Sign Up:</legend> <pre>
First <input type="text" name="first" />
Last <input type="text" name="last" />
Email <input type="text" name="email" />
Username <input type="text" name="user_name" />
<input type="submit" value="AD RECORD" />
</pre></fieldset></form>
END;

// also from placeholder code
$query = 'DEALLOCATE PREPARE statement';
mysql_query($query);
// end placeholder code

最佳答案

我认为您最初的 PREPARE statements 查询可能会失败,您还应该更改该行:

$query = 'SET @first = "$first",' . 'SET @last = "$last",' . 'SET @email = "$email",' . 'SET @user_name = "$user_name",';

$query = 'SET @first = "$first",' . ' @last = "$last",' . ' @email = "$email",' . ' @user_name = "$user_name",';

SET 的语法是 SET variable_assignment [, variable_assignment] ... 但您使用的是 SET variable_assignment [, SET variable_assignment] ... code> 这会导致错误。

要查看您的第一个查询是否有错误,请尝试以下操作:

$query = 'PREPARE statement FROM "INSERT INTO user_master VALUES(?,?,?,?)"';
$res = mysql_query($query);
if (!$res) die(mysql_error());

关于php - 在 php 和 mysql 中使用占位符来防止注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11215124/

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