Forum Moderators: coopster
Currently, I have this code that works great for getting the data in the db:
<?php
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "INSERT INTO $tbl_name(companyname, address1, address2, ... ) VALUES('$companyname', '$address1', '$address2', ... )";
$result = mysql_query($sql);
?>
In the DB I have an auto generated ID column as well. I need to somehow have PHP discover what ID is given to the newly inserted row so I can submit the ID number to the payment site in a cookie so that record/row can be reference and updated after the payment.
However, I can't get it to work!
I've tried adding this right after the INSERT command, but it gives me an error: "Unable to jump to row 0 on MySQL result index 4"
$getid = "SELECT id FROM $tbl_name WHERE `companyname` = '$name' AND `sub_tot` = '$sub_tot'";
$result1 = mysql_query($getid);
$id= mysql_result($result1,0,"id");
I've also tried getting an array, but that won't work either. No results are given.
$result1 = mysql_query($getid);
while ($row = mysql_fetch_assoc($result1)) {
$id= $row['id'];
}
What's the best way to get this to work? Is there an easy way to get the automatically generated id immediately after inserting a row?
[php.net...]