Page 1 of 1

mysql_insert_id(); question

Posted: Sat Apr 26, 2003 1:31 am
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...

Posted: Sat Apr 26, 2003 5:21 am
by volka
if ($submit){
maybe you should test that before putting something into the database.

Posted: Sat Apr 26, 2003 1:49 pm
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?

Posted: Sun Apr 27, 2003 3:22 am
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;


?>

Posted: Sun Apr 27, 2003 4:54 pm
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.....

Posted: Mon Apr 28, 2003 2:02 am
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

Posted: Mon Apr 28, 2003 3:19 am
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.....