I am quite new to php and am having problems with the following block of code. For some reason when all has been executed i am being sent to: auctionx.php?id=”. When it should be id=1. I am trying to get the “1” from the $new_id variable. Could somebody please point out my error? Thanks a lot in advance. P.S When i’ve got my project sorted, as regards functionality, i will then make what i am told is a sensible translation to mysqli. Thanks again.
Oh, and one last thing, i am conscious there may well be a short cut i am missing. After the INSERT query is there anyway some code could know straightaway what the new id is, if you know what i mean? To save me doing the next query. Is it as simply as having “$new_id” in the first value field. Hope that is clear.
if ($check_1 == 0)
{
//Then need to register the auction...so...
$auc_update = mysql_query("
INSERT INTO auction VALUES ('','$afil','$user','','','','','')
//Should the first be "$new_id" here (above) i mean
");
$row_1 = mysql_query("
SELECT * FROM auction WHERE host = $user AND auc_no = $afil
");
//Find
$new_id = $row_1['id'];
//Id obtained
//Go to:
header ("location:auctionx.php?id='$new_id'");
}
First you miss one
mysql_xxx()function call.mysql_query()simply sends the query to mysql but for aSELECTyou then need to retrieve the results like this :More information on the function used :
mysql_query() mysql_fetch_assoc()
Then you really should ensure that your query works so check the return value and check if it is not false, else display an error :
Then, it seems that what you want is to retrieve the id associated with the last insertion in your database. You don’t need a
SELECTfor this, just use mysql_insert_id() for this.With all this in mind your code would become:
And another thing,
mysql_xxx()are considered deprecated, you really should avoid using it and try mysqli or PDO instead.