mysql_insert_id(); question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
reverend_ink
Forum Contributor
Posts: 151
Joined: Sun Apr 20, 2003 1:18 am
Location: Las Vegas | London

mysql_insert_id(); question

Post by reverend_ink »

I have a script that is using mysql_insert_id(); but my problem is it is performing this on load and on submit....

how can I get the script to perform this function but once?

Here is the code....

Code: Select all

sqlquery = "INSERT INTO $table
VALUES('$id','$update','$directory','$description')";

mysql_query($sqlquery);

$id = mysql_insert_id(); 

if ($submit){
Then it goes on to display the info updated....

Should I post to another page?

As for the post here is the form action....

Code: Select all

<form method=post enctype="multipart/form-data">
<input=hidden name="id" value="NULL">
File Name:<br>
<input type=text name="update"><br>
<input type=hidden name="directory" value="<?php echo $dir?>">
Image:<br>
<input type=file name=pic><br>

Description:<br>

<textarea cols="25" rows="5" name="description"></textarea><br>
<input type=submit name=submit value="upload image">
Thanks all...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if ($submit){
maybe you should test that before putting something into the database.
reverend_ink
Forum Contributor
Posts: 151
Joined: Sun Apr 20, 2003 1:18 am
Location: Las Vegas | London

Post by reverend_ink »

volka wrote:
if ($submit){
maybe you should test that before putting something into the database.
The "if ($submit){ " does a display after the submit.....think this could be the reason I am getting dual mysql posts?
reverend_ink
Forum Contributor
Posts: 151
Joined: Sun Apr 20, 2003 1:18 am
Location: Las Vegas | London

Post by reverend_ink »

ok here is the php code...

is the insert in table $id causing me the problem?

can't fuguyre out why this is creating dual ID submissions, the first one being blank....

Code: Select all

<?php
$sqlquery = "INSERT INTO $dir
VALUES('$id','$update','$directory','$description')";

mysql_query($sqlquery);

if ($submit)&#123;

$id = mysql_insert_id();

exec("cp $pic /home/httpd/vhosts/site1/httpdocs/pics/$directory/$id.jpg");
exec("cp $pic /home/httpd/vhosts/site1/httpdocs/pics/$directory/thumbs/tn_$id.jpg");

    echo "<font face=arial,verdana,helvetica size=2 color=yellow>\n";
	echo "You have successfully updated with the following information<br>\n";
	echo "Update ID: $id<br>\n";
	echo "file name: $id.jpg<br>\n"; 
    echo "Update Type: $directory<br>\n"; 
    echo "Description: $description<br>\n"; 
    echo "<br>\n"; 
    echo "img: <img src=/pics/$directory/$id.jpg><br>\n";
    echo "thumbnail: <img src=/pics/$directory/thumbs/tn_$id.jpg width=60 height=60><br>\n";
	echo "To upload more files, please use the form below<br><br><br><br>\n";
&#125;


?>
reverend_ink
Forum Contributor
Posts: 151
Joined: Sun Apr 20, 2003 1:18 am
Location: Las Vegas | London

Post by reverend_ink »

Found the solution....

Code: Select all

if ($submit)&#123;

$sqlquery = "INSERT INTO $dir
VALUES('$id','$update','$directory','$description')";

mysql_query($sqlquery);

$id = mysql_insert_id();
Had to move the $sqlquery INTO the if statement.....

Then worked like I planned.....
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

mysql_query($sqlquery);
was the important part to move into the if-block, but I was absolutely sure you'll figure it out yourself :D
reverend_ink
Forum Contributor
Posts: 151
Joined: Sun Apr 20, 2003 1:18 am
Location: Las Vegas | London

Post by reverend_ink »

Yeah I had to move $id = mysql_insert_id(); to AFTER if ($submit) along with moving INSERT ($id) into table to after ($submit) as well to get it to stop submitting the id twice and messing with the system output.....
Post Reply