当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Insert image links corresponding to the right product id on MYSQL

问题描述:

I want some help, and I hope you could help me on this.

I'm working in a gallery script, where users create a certain product

and the upload product's images. Now the MySQL part. Here's how I proceed.

-1: Add a new product on the products table: (productid, userid, productname)

$insertproduct="insert into products(userid, productname) values

('3', 'Leather Jacker')";

db->query($insertproduct);

Then I get the productid in this way:

$getproductid = $db->query("SELECT max(productid) from products where userid=3");

while($row = mysqli_fetch_row($getproductid))

{

$pid=$row[0];

}

To later use productid when inserting imagelinks corresponding to that product

$query = "insert into images(imagelink, productid) values

('".$imagelink."', '".$pid."')";

$insert = $db->query($query);

BUT when I check the database everything's fine except for 'productid=0'

so its like:

imgid imagelink productid

166 203572012_1547_17_1.jpg 0

And when I replace $pid with some static number, productid seems to get saved correctly

imgid imagelink productid

166 203572012_1547_17_1.jpg 541

So I'm thinking maybe the problem is here:

while($row = mysqli_fetch_row($getproductid))

{

$pid=$row[0];

}

Help me please. This problem is driving me crazy.

PS: I'm a beginner so please dont judge me :)

Thank you.

网友答案:

replace this $row = mysqli_fetch_row($getproductid) with $row = $getproductid->fetch_row() and i think this too db->query($insertproduct); with $db->query($insertproduct);

分享给朋友:
您可能感兴趣的文章:
随机阅读: