gpt4 book ai didi

php - 如何插入到同一个数据库的多个字段

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

我希望将单个选择插入到多个用户的字段中。当做出选择并输入提交时,我有以下代码。我没有收到错误,我收到下一页,该消息发布了 5 次,这是有多少用户不是 weekpicks 表。但数据库中没有插入任何内容。

<?
// This code is to use to place info into the MySQL

$sTeam1 = $_POST['sTeam1'];

// (WHERE username FROM authorize WHERE username not in ( SELECT username FROM weekpicks WHERE whatweek='$totalnoOfWeek' )" .

//$nMemberID = (integer) Query

$sql_events = mysql_query("SELECT username FROM authorize WHERE username not in ( SELECT username FROM weekpicks WHERE whatweek='$totalnoOfWeek' )") or die(mysql_error());

while ($row = mysql_fetch_array($sql_events)) {
$username = $row["username"];

("INSERT INTO weekpicks SET " . "username = '" . ($username) . "'," . "date_create = NOW(), " . "whatweek = '" . ($totalnoOfWeek) . "'," . "team = '" . addslashes($sTeam1) . "'" . 'INSERT');

echo "<HTML>

<BODY>
<h2>Your pick of " . ($sTeam1) . ", for week " . ($totalnoOfWeek) . ", has been added.</h2>

<P align=left><a href=\"nopickpolicy.php\">Return to main page</a></p>
</BODY>
</HTML>";
}
?>

最佳答案

您正在创建用于插入的字符串,但没有运行它。

修复你的代码:

while ($row = mysql_fetch_array($sql_events)) {
$username = $row["username"];
mysql_query("INSERT INTO weekpicks SET " . "username = '" . ($username) . "'," . "date_create = NOW(), " . "whatweek = '" . ($totalnoOfWeek) . "'," . "team = '" . addslashes($sTeam1) . "'");

//echo ...
}

修复字符串语法,你可以这样做,看起来更好。还使用 mysql_real_escape_string() 而不是 addslashes(),因为 addslashes 不如 mysql 的 php native 函数安全。

$sTeam1 = mysql_real_escape_string($sTeam1);
mysql_query("INSERT INTO weekpicks SET username = '$username', date_create = NOW(), whatweek = '$totalnoOfWeek', team = '$sTeam1');

我必须告诉你的另一件事:

停止使用mysql_*,改用mysqli_*

mysql_ 已从 PHP7 中删除,并在 PHP 5.5 后弃用

它不如 mysqli_ 安全,因此请考虑将代码改进为新模型。

Follow this guide in order to change your code properly.

关于php - 如何插入到同一个数据库的多个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34484998/

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