Php 一次将多个值插入MySQL
有人能解释一下为什么这个PHP/MySQL不起作用吗?基本上,我需要从一个表单中一次插入大量行,因此将有多个名称字段、多个短字段、中字段、长字段等。。我收到这个错误:Php 一次将多个值插入MySQL,php,mysql,database,insert-into,Php,Mysql,Database,Insert Into,有人能解释一下为什么这个PHP/MySQL不起作用吗?基本上,我需要从一个表单中一次插入大量行,因此将有多个名称字段、多个短字段、中字段、长字段等。。我收到这个错误: Notice: Undefined variable: Short1 in C:\xampp\htdocs\process.php on line 95 You have an error in your SQL syntax; check the manual that corresponds to your MySQL ser
Notice: Undefined variable: Short1 in C:\xampp\htdocs\process.php on line 95
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Long, VLong, Extreme, LJump, HJump, Shotputt, Discuss, Javelin, Date, Year) VAL' at line 2
这是我的PHP
<?php
$host = "localhost";
$databasename = "pe_results";
$databaseusername = "root";
$databasepassword = "";
$conn = mysql_connect("$host", "$databaseusername", "$databasepassword");
mysql_select_db("$databasename", $conn);
if (isset($_POST['Name1'])) {
$Name1 = $_POST['Name1'];
}
if (isset($_POST['Short1'])) {
$Short1 = $_POST['Short1'];
}
if (isset($_POST['Med1'])) {
$Med1 = $_POST['Med1'];
}
if (isset($_POST['Long1'])) {
$Long1 = $_POST['Long1'];
}
if (isset($_POST['VLong1'])) {
$VLong1 = $_POST['VLong1'];
}
if (isset($_POST['Extreme1'])) {
$Extreme1 = $_POST['Extreme1'];
}
if (isset($_POST['LJump1'])) {
$LJump1 = $_POST['LJump1'];
}
if (isset($_POST['HJump1'])) {
$HJump1 = $_POST['HJump1'];
}
if (isset($_POST['Shotputt1'])) {
$Shotputt1 = $_POST['Shotputt1'];
}
if (isset($_POST['Discuss1'])) {
$Discuss1 = $_POST['Discuss1'];
}
if (isset($_POST['Javelin1'])) {
$Javelin1 = $_POST['Javelin1'];
}
if (isset($_POST['Date'])) {
$Date = $_POST['Date'];
}
if (isset($_POST['Year'])) {
$Year = $_POST['Year'];
}
// Sector 2 */
if (isset($_POST['Name2'])) {
$Name2 = $_POST['Name2'];
}
if (isset($_POST['Short2'])) {
$Short2 = $_POST['Short2'];
}
if (isset($_POST['Med2'])) {
$Med2 = $_POST['Med2'];
}
if (isset($_POST['Long2'])) {
$Long2 = $_POST['Long2'];
}
if (isset($_POST['VLong2'])) {
$VLong2 = $_POST['VLong2'];
}
if (isset($_POST['Extreme2'])) {
$Extreme2 = $_POST['Extreme2'];
}
if (isset($_POST['LJump2'])) {
$LJump2 = $_POST['LJump2'];
}
if (isset($_POST['HJump2'])) {
$HJump2 = $_POST['HJump2'];
}
if (isset($_POST['Shotputt2'])) {
$Shotputt2 = $_POST['Shotputt2'];
}
if (isset($_POST['Discuss2'])) {
$Discuss2 = $_POST['Discuss2'];
}
if (isset($_POST['Javelin2'])) {
$Javelin2 = $_POST['Javelin2'];
}
if (isset($_POST['Date'])) {
$Date = $_POST['Date'];
}
if (isset($_POST['Year'])) {
$Year = $_POST['Year'];
}
$sql="INSERT INTO results_main
(Name, Short, Med, Long, VLong, Extreme, LJump, HJump, Shotputt, Discuss, Javelin, Date, Year)
VALUES
('$Name1', '$Short1', '$Med1', '$Long1', '$VLong1', '$Extreme1', '$LJump1', '$HJump1', '$Shotputt1', '$Discuss1', '$Javelin1', '$Date', '$Year'),
('$Name2', '$Short2', '$Med2', '$Long2', '$VLong2', '$Extreme2', '$LJump2', '$HJump2', '$Shotputt2', '$Discuss2', '$Javelin2', '$Date', '$Year');
";
$result = mysql_query($sql) or die(mysql_error());
// close connection
mysql_close($conn);
?>
LONG
是一个保留关键字,正好是列的名称。为了避免语法错误,列名应该用反勾号转义
INSERT INTO results_main(Name, Short, Med, `Long`, VLong, ...) VALUES (....)
作为旁注,如果变量的值来自外部,则查询易受攻击。请看下面的文章,了解如何预防它。通过使用
PreparedStatements
可以避免在值周围使用单引号
LONG
是一个保留关键字,正好是您列的名称。为了避免语法错误,列名应该用反勾号转义
INSERT INTO results_main(Name, Short, Med, `Long`, VLong, ...) VALUES (....)
作为旁注,如果变量的值来自外部,则查询易受攻击。请看下面的文章,了解如何预防它。通过使用
PreparedStatements
可以避免在值周围使用单引号
`Long`
检查您的帖子数组,您的$_post['Short1'有问题 也不要使用MYSQL保留关键字,Long是一个保留关键字。如果你使用它,你应该通过
`Long`
我建议你把你的$\u POST数组扔掉看看。根据您的代码,只有在$\u POST数组中有值时才设置变量。我建议转储$\u POST数组并查看它。根据您的代码,只有在$\u POST数组中有值时才设置变量。短变量很好,我不知道为什么会引起骚动,其他一切都很好,唯一不同的是名称。此外,自从发布后,我将所有内容都用反勾号包装起来,因此无需担心保留字。短变量很好,我不知道为什么会引起大惊小怪,其他所有内容都很好,唯一不同的是名称。此外,自从发布后,我将所有内容都包装在了backticks中,因此不必担心保留字hanks,我已经用backticks解决了这个问题,但是现在我得到了这个错误:注意:未定义的变量:C:\xampp\htdocs\process.php中的Short1,在调用
mysql\u query()
之前,第95行“field list”中的未知列“1”,你能发布这个结果吗:echo$sql
?更新了问题的结果,我已经通过调用$Short1解决了问题,但是我仍然收到未知的第1列错误jw我在问题中提供了来自echo$sql的信息,我也在那里发布了答案。谢谢,我已经用backticks解决了这个问题,但是现在我得到了这个错误:注意:未定义的变量:C:\xampp\htdocs\process.php中的Short1,在调用mysql\u query()
之前,在'field list'的第95行未知列'1'上,你能发布这个结果吗:echo$sql
?更新了结果的问题,我已经修复了调用$Short1的问题,但是我仍然得到未知的第1列errorJW,我在问题中提供了来自echo$sql的信息,我也在那里发布了答案。