dongzhuo1930 2017-09-21 21:44
浏览 190

插入具有相同数据库但共享相同ID的两个表

I want to create a php code that will insert value to to tables but i want table one "id" and table two "product_id" to be the same thing. this is my below code that insert values to the database but table one "id" do not correspond to table two "product_id"

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

$name = $_POST['name'];
$image = $_POST['image'];
$price = $_POST['price'];
$stock = $_POST['stock'];
$description = $_POST['description'];
$status = $_POST['status'];

require_once('dbConnect.php');

$sql ="SELECT id FROM product ORDER BY id ASC";

$res = mysqli_query($con,$sql);

$id = 0;

while($row = mysqli_fetch_array($res)){
    $id = $row['id'];
}

$imagename = "$id.png";

$path = "uploads/$id.png";
$storage = "$id.png";

$actualpath = "http://localhost/markeet/$path";


$sql = "INSERT INTO product (name,image,price,stock,draft,description,status,created_at,last_update) VALUES ('$name','$storage','$price','$stock','0','$description','$status','','');";

$sql .= "INSERT INTO product_category (product_id, category_id)
VALUES ('', '$stock');";

if ($con->multi_query($sql) === TRUE) {
    echo "New records created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

if(mysqli_query($con,$sql)){
    file_put_contents($path,base64_decode($image));
    echo "Successfully Uploaded";
}

mysqli_close($con);
}else{
    echo "Error";
}
  • 写回答

3条回答 默认 最新

  • dqnk57224 2017-09-21 21:47
    关注

    You need to get that inserted ID by using function mysqli_insert_id

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探