[英]PHP Array only writing 1st record to MySQL database
I have the following code trying to catch up to 15 entries upon submission, however it is only catching the first entry in the database and I am receiving the following error message: 我有以下代码尝试提交时最多捕获15个条目,但是它仅捕获数据库中的第一个条目,并且我收到以下错误消息:
You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1. 查看与您的MySQL服务器版本相对应的手册,以找到在第1行的'1'附近使用的正确语法。
<?php
for($i = 0; $i < 15; $i++)
{
$tournament = $_POST['tournament'];
$agegroup = $_POST['agegroup'];
$teamname = $_POST['teamname'];
$coach = $_POST['coach'];
$coachaau = $_POST['coachaau'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$astcoach = $_POST['astcoach'];
$astno = $_POST['astno'];
$astphone = $_POST['astphone'];
$astemail = $_POST['astemail'];
$manager = $_POST['manager'];
$managerno = $_POST['managerno'];
$managerphone = $_POST['managerphone'];
$manageremail = $_POST['manageremail'];
$name = $_POST['name'][$i];
$grade = $_POST['grade'][$i];
$bday = $_POST['bday'][$i];
$aauno = $_POST['aauno'][$i];
if(empty($name) || empty($grade) || empty ($bday) || empty ($aauno))
{
echo ' ';
}
elseif(
$result = mysql_query("INSERT INTO roster (tournament, agegroup, teamname, coach, coachaau, phone, email, astcoach, astno, astphone, astemail, manager, managerno, managerphone, manageremail, name, grade, bday, aauno)
VALUES (
'". mysql_real_escape_string($tournament) . "',
'". mysql_real_escape_string($agegroup) . "',
'". mysql_real_escape_string($teamname) . "',
'". mysql_real_escape_string($coach) . "',
'". mysql_real_escape_string($coachaau) . "',
'". mysql_real_escape_string($phone) . "',
'". mysql_real_escape_string($email) . "',
'". mysql_real_escape_string($astcoach) . "',
'". mysql_real_escape_string($astno) . "',
'". mysql_real_escape_string($astphone) . "',
'". mysql_real_escape_string($astemail) . "',
'". mysql_real_escape_string($manager) . "',
'". mysql_real_escape_string($managerno) . "',
'". mysql_real_escape_string($managerphone) . "',
'". mysql_real_escape_string($manageremail) . "',
'". mysql_real_escape_string($name) . "',
'". mysql_real_escape_string($grade) . "',
'". mysql_real_escape_string($bday) . "',
'". mysql_real_escape_string($aauno) . "');"));
@mysql_query($result)or die(mysql_error());
};
?>
The problem is that you have two mysql_query
calls here, and while the first one works on the valid query string, the second - @mysql_query($result)
works on its result - ie, string '1'. 问题是您在这里有两个mysql_query
调用,而第一个调用有效的查询字符串,而第二个- @mysql_query($result)
处理其结果-即字符串'1'。 But you actually don't need that call, as the first query should have already sent the data to DB. 但是实际上您不需要该调用,因为第一个查询应该已经将数据发送到了DB。
The quick fix would be checking $result
itself (instead of @mysql_query($result)or die(mysql_error());
line): 快速解决方法是检查$result
本身(而不是@mysql_query($result)or die(mysql_error());
行):
if (!$result) {
die('Invalid query: ' . mysql_error());
}
Said all that, I'd like to remind you that mysql_query
(as whole family of mysql_
functions) is deprecated. 说了这么多,我想提醒您,不推荐使用mysql_query
(作为mysql_
函数的整个家族)。 If you used PDO or MySQLi, you would be able to use a single prepared statement, filled by new data at each iteration. 如果使用PDO或MySQLi,则可以使用单个准备好的语句,并在每次迭代时填充新数据。
Also (kudos to @djot for mentioning that) it's not efficient to extract non-array variables from $_POST again and again, instead of doing it just once - before the loop. 同样(对@djot表示感谢),一次又一次地从$ _POST提取非数组变量,而不是一次循环-循环之前,这样做效率不高。 This way (if you stay with mysql
) you won't have to escape them each time as well. 这样(如果您使用mysql
),您也不必每次都逃脱它们。 Actually, I'd use something like that here: 实际上,我会在这里使用类似的方法:
$fieldsToInsert = array('tournament', 'agegroup', 'teamname', ...);
$valuesToInsert = [];
foreach ($fieldsToInsert as $field) {
if (! isset($_POST[$field])) {
// actually it's not clear what to do here:
// should we signal an error immediately with, or use some fallback value
}
else {
$valuesToInsert[$field] = mysql_real_escape_string($_POST[$field]);
}
}
This way you'll be able to streamline the code that creates a query as well. 这样,您就可以简化创建查询的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.