我最近开始编写服务器脚本。虽然这个问题可能看起来很基本,但我无法得到解决方案。
我有两张表,一张是 PRODUCTS,另一张是 DEALS。
PRODUCTS TABLE:
Id product_name product_desc category brand
1 product1 desc1 cat1 brand1
2 product2 desc2 cat2 brand2
DEALS TABLE
Id deal_name productid
1 todayoffer 2
我希望将 id 的值(来自 PRODUCTS 表)放入 productid 并将该值携带到 DEALS 表中,然后将 deal_name 与它一起添加,如上所示。我最近使用的代码是
<?
require_once('config.php'); //connection
$dealname=$_REQUEST['dealname'];
$productid=$_REQUEST['productid'];
$id=$_REQUEST['id'];
$productid=$_REQUEST['productid'];
$inserts = mysql_query("insert into deals (dealname,productid) values ('".$dealname."','".$productid."') SELECT productid FROM products WHERE productid = '".$id."'");
$posts[0]['message'] = 'Deal Registered';
$idd = mysql_insert_id();
$selectt = mysql_query("select * from deals where id = '".$idd."'");
$posts[0]['detail'] = $selectt;
header('Content-type: application/json');
echo json_encode($posts);
?>
如果有人能指导我并告诉我错误,我将不胜感激
最佳答案
您忘记调用 mysql_fetch_assoc
:
$results = mysql_query("SELECT * FROM DEALS WHERE id = LAST_INSERT_ID()") or die(mysql_error());
$selectt = mysql_fetch_assoc($results);
$posts[0]['detail'] = $selectt;
插入查询应该是:
mysql_query("INSERT INTO deals (dealname, productid)
SELECT dealname, productid
FROM products
WHERE productid = '$id'");
关于php - 将表的 id 调用到新表并将值插入到该新表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24838462/