php - 如何在mysql中复制列时插入数据?

标签 php mysql sql laravel

我想在复制列的同时插入数据。

$db->query("INSERT INTO transactions
(order_id,cart_id,full_name,email,phone,street1,street2, 
 city,state,zip_code,country,landmark,sub_total,tax,grand_total,
 description,txn_type,paid,customer_id,items)
 VALUES
('$order_id','$cart_id','$full_name','$email','$phone','$street1','$street2',
 '$city','$state','$zip_code','$country','$landmark','$sub_total','$tax',
 '$grand_total','$description','$txn_type','$paid','$customer_id', items 
  FROM cart WHERE id ='$cart_id')
 ");

在上面的代码中,我在复制的同时插入了所有的值

"items from cart where id=$cart_id".

我做错了什么?

最佳答案

似乎你需要一个插入选择

insert  into  transactions 
(order_id,cart_id,full_name,email,phone,street1,street2, 
   city,state,zip_code,country,landmark,sub_total,tax,grand_total,
   description,txn_type,paid,customer_id,items)
  SELECT 
 '$order_id','$cart_id','$full_name','$email','$phone','$street1','$street2',
 '$city','$state','$zip_code','$country','$landmark','$sub_total','$tax',
 '$grand_total','$description','$txn_type','$paid','$customer_id', items 
  FROM cart WHERE id ='$cart_id'

你不应该在 sql 中使用 php var 你有 sqlinjection 的风险..你应该看看你的 mysql 驱动程序以准备语句和绑定(bind)参数..

关于php - 如何在mysql中复制列时插入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52138803/

相关文章:

php - mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows 等...期望参数 1 是资源

c# - 尝试检查我的 MVC 模型的值在我的数据库中是否等于 NULL

php - 使用 iframe 在 SSL 站点上显示具有绝对 URL 的非 SSL 内容

javascript - javascript onClick 不工作

mysql - 获取每组行的最大值的正确 ID

MySQL查询相同数量的状态返回一组记录

php - Windows 上的 pecl memcached

php - 可变函数与 PHP 7 和 PHP5 的兼容性

Java + MySQL - 更新时覆盖行

sql - 高级 Postgres 的插入/更新速度下降了吗?