[英]i am trying to add 2 queries at the same time using php and mysql
i don't know what is the problem with my query i want to add a specific range, then add the id of the range to the table of items within the same form我不知道我的查询有什么问题我想添加一个特定的范围,然后将该范围的 id 添加到同一表单中的项目表中
$range_info = array (
'min_price' => $_POST['min_price'],
'max_price' => $_POST['max_price']
);
$item_info = array (
'item_name' => $_POST['item_name'],
'cat_id' => $cat_id,
'total_quantity' => $_POST['total_quantity'],
'available_for_sale' => $_POST['available_for_sale'],
'description' => $_POST['desc'],
'user_id' => $data['user_id']
);
$range_fields = '`' . implode("`, `", array_keys($range_info)) . '`';
$range_data = '\'' . implode("', '", $range_info) . '\'';
$fields = '`' . implode("`, `", array_keys($item_info)) . '`';
$data = '\'' . implode("', '", $item_info) . '\'';
mysql_query("INSERT INTO range($range_fields) VALUES ($range_data); INSERT
INTO items($fields, `range_id`) VALUES ($data, '".mysql_insert_id()."'");
You need to separate your queries (and you forgot to close a parentheses) here :您需要在此处分开您的查询(并且您忘记关闭括号):
mysql_query("INSERT INTO range($range_fields) VALUES ($range_data)");
mysql_query("INSERT INTO items($fields, `range_id`) VALUES ($data, '".mysql_insert_id()."')");
Note that your queries are insecure !请注意,您的查询是不安全的! try to sanitize the data of your post before using it or use prepared statements with mysqli
or move to PDO instead!尝试在使用之前清理您的帖子的数据,或者使用带有mysqli
准备好的语句,或者改为使用PDO !
Store the insert queries into a separate variable and run将插入查询存储到一个单独的变量中并运行
$range_info = array (
'min_price' => $_POST['min_price'],
'max_price' => $_POST['max_price']
);
$item_info = array (
'item_name' => $_POST['item_name'],
'cat_id' => $cat_id,
'total_quantity' => $_POST['total_quantity'],
'available_for_sale' => $_POST['available_for_sale'],
'description' => $_POST['desc'],
'user_id' => $data['user_id']
);
$range_fields = '`' . implode("`, `", array_keys($range_info)) . '`';
$range_data = '\'' . implode("', '", $range_info) . '\'';
$fields = '`' . implode("`, `", array_keys($item_info)) . '`';
$data = '\'' . implode("', '", $item_info) . '\'';
/*insert queries*/
$qr1 = "INSERT INTO range($range_fields) VALUES ($range_data)";
$qr2 = "INSERT INTO items($fields, `range_id`) VALUES ($data, '".mysql_insert_id()."')";
mysql_query($qr1);
mysql_query($qr2);
Note:笔记:
The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database.原始 MySQL 扩展现已弃用,并且在连接到数据库时会生成 E_DEPRECATED 错误。 Instead, use the MySQLi or PDO_MySQL extensions.相反,使用 MySQLi 或 PDO_MySQL 扩展。
MySQLi tutorial: https://www.w3schools.com/PhP/func_mysqli_connect.asp MySQLi 教程: https ://www.w3schools.com/PhP/func_mysqli_connect.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.