简体   繁体   English

我正在尝试使用 php 和 mysql 同时添加 2 个查询

[英]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.

相关问题 我正在尝试向此 PHP 代码添加时间变量,但它不起作用 - I am trying to add a time variable to this PHP code and it is not working PHP/MySQL - 同时进行多个查询 - PHP/MySQL - Multiple queries at the same time 我正在尝试使用 php 在 mysql 中动态插入数据,但出现语法错误 - I am trying to dynamically insert data in mysql using php and i am getting syntax error 我正在尝试使用PHP添加图像 - I am trying to add and image with PHP 当我尝试使用带有PHP的date函数显示时间时,为什么会得到“ 4:00”? - why am I getting '4:00' when I'm trying to display time using the date function with php? 我正在尝试在htaccess中使用掩码的网址,现在我无法在php中获取搜索查询 - I am trying to use masked url in htaccess and now i am not able to get search queries in php 我正在尝试创建一个按钮,以删除使用PHP和AJAX从MySQL中选择的一行数据。 - I am trying to create a button to delete a row of data selected from MySQL using PHP and AJAX. 尝试使用php和html将记录添加到mysql - Trying to Add record to mysql using php and html 我正在尝试使用PHP,mySQL和EXTjs为吉他老师编写一个简单的Web应用程序 - I am trying to write a simple Web App for guitar teachers using PHP, mySQL, and EXTjs 使用 php 并从 mySQL 调用值我试图调用每 16 行包含 METAL GOLD - Using php and calling values from mySQL I am trying to call every 16 rows that contain the METAL GOLD
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM