多个 INSERT 不起作用
我有多个 If 语句,如果为 true,则将数据插入数据库。问题是第二个 INSERT 语句将其插入了 4 次。我如何阻止它添加重复项。另外,返回的 $_POST['tshirt'] 仅包含一个被插入 4 次的值。
if(isset($_POST['distance'])) {
$dist = $_POST['distance'];
$sql="INSERT INTO sportevent_parameters.Distance(value, user_ref, event) VALUES ('$dist', '$id', '$event')";
}
mysql_query($sql, $con)
or die("MySQL Error: ".mysql_error());
if(isset($_POST['tshirt'])) {
$tshrt = $_POST['tshirt'];
$sql="INSERT INTO sportevent_parameters.T_Shirt(value, user_ref, event) VALUES ('$tshrt', '$id', '$event') ON DUPLICATE KEY UPDATE";
}
mysql_query($sql, $con)
or die("MySQL Error: ".mysql_error());
if(isset($_POST['partnerid'])) {
$sql="INSERT INTO sportevent_parameters.Partner_ID(value, user_ref, event) VALUES ('$partnerid', '$id', '$event')";
}
mysql_query($sql, $con)
or die("MySQL Error: ".mysql_error());
if(isset($_POST['racecategory'])) {
$sql="INSERT INTO sportevent_parameters.Race_Category(value, user_ref, event) VALUES ('$category', '$id', '$event')";
}
mysql_query($sql, $con)
or die("MySQL Error: ".mysql_error());
什么会导致它向数据库插入多个副本?
I have multiple If statements that if true, inserts data into the db. The Problem is that the 2nd one of the INSERT statements inserts it 4 times. How would i stop it from adding duplicates. Also the returned $_POST['tshirt'] contains only one value which is being inserted 4 times.
if(isset($_POST['distance'])) {
$dist = $_POST['distance'];
$sql="INSERT INTO sportevent_parameters.Distance(value, user_ref, event) VALUES ('$dist', '$id', '$event')";
}
mysql_query($sql, $con)
or die("MySQL Error: ".mysql_error());
if(isset($_POST['tshirt'])) {
$tshrt = $_POST['tshirt'];
$sql="INSERT INTO sportevent_parameters.T_Shirt(value, user_ref, event) VALUES ('$tshrt', '$id', '$event') ON DUPLICATE KEY UPDATE";
}
mysql_query($sql, $con)
or die("MySQL Error: ".mysql_error());
if(isset($_POST['partnerid'])) {
$sql="INSERT INTO sportevent_parameters.Partner_ID(value, user_ref, event) VALUES ('$partnerid', '$id', '$event')";
}
mysql_query($sql, $con)
or die("MySQL Error: ".mysql_error());
if(isset($_POST['racecategory'])) {
$sql="INSERT INTO sportevent_parameters.Race_Category(value, user_ref, event) VALUES ('$category', '$id', '$event')";
}
mysql_query($sql, $con)
or die("MySQL Error: ".mysql_error());
What would cause it to insert multiple copies into the DB?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,请先清理您的用户输入,然后再直接使用它一个查询!!! - 也阅读此内容。
其次 - 问题是您对
mysql_query()
的调用应该位于if
语句内。根据您所做的操作,mysql_query()
无论如何都会执行,只有 SQL 语句本身被if
更改。这意味着如果第一个if
条件为 true,但其他所有条件都为 false,则第一个语句将执行 4 次。将您的代码更改为:
Firstly, please sanitise your user input before using it directly in a query!!! - also read this.
Secondly - the problem is that your calls to
mysql_query()
should be inside theif
statements. With what you have done,mysql_query()
is executed regardless, only the SQL statement itself is changed by theif
. This means that if the firstif
condition is true, but all the others are false, the first statement will be executed 4 times.Change your code to:
编辑: mysql_query 应该位于 if 语句内,这样如果不调用 if 则不会调用它。我还会检查您传递的变量。
PHP 不支持在一个查询中使用多个 SQL 语句。您必须将每个 SQL 语句拆分为对数据库的单独查询。
PHP 手册链接:http://php.net/manual/en/function。 mysql-query.php
Edit: The mysql_query's should be inside the if statements so it does not get called if the if is not called. I would also check the variables you are passing.
Multiple SQL statements in one query are not supported in PHP. You have to split up each SQL statement into separate queries to the database.
Link to PHP Manual: http://php.net/manual/en/function.mysql-query.php