Edward Posted April 6, 2007 Share Posted April 6, 2007 Hi, I'm creating a row in a mysql table to store some information about a file being uploaded. If the form isn;t successfully uploaded, I want to delete the row as the row won't be required. I thought this should work but it doesn't delete the row, even though the sql statement echos on the screen correctly. $sql = 'INSERT INTO files VALUES (\'\',\''.$path.'\',\''.$name.'\',\''.$extension.'\',\''.$parent.'\',\''.$parent_id.'\')'; if ($result = mysql_query($sql)) { $sql_2 = 'SELECT * FROM files WHERE name = \''.$name.'\' AND parent_id = \''.$parent_id.'\' ORDER BY name ASC LIMIT 1;'; $result_2 = mysql_query($sql_2); while($row = mysql_fetch_array($result_2, MYSQL_ASSOC)) { $id = $row['id']; } if (move_uploaded_file($_FILES['file']['tmp_name'], "$path$name$extension")) { echo '<p>Thank you '.$uploader.', the file \''.$name.$extension.'\' has been uploaded.</p>'; echo '<p>Would you like to <a href="add_a_file.php">add another file</a>?</p>'; } else { switch ($_FILES['file']['error']) { case 1: $errors = '<li class="errors">Your file \''.$name.$extension.'\' has not been uploaded as the file size is too large.</li>'; break; case 2: $errors = '<li class="errors">Your file \''.$name.$extension.'\' has not been uploaded as the file size is too large.</li>'; break; case 3: $errors = '<li class="errors">Your file \''.$name.$extension.'\' was only partially uploaded.</li>'; break; case 4: $errors = '<li class="errors">Your file \''.$name.$extension.'\' was not uploaded.</li>'; break; } echo '<div>Sorry, the following errors were encountered:'; echo $errors; echo '</div>'; echo '<p><hr width="100%" size="1" color="#CCCCCC" /></p>'; form(); $sql_3 = 'DELETE FROM files WHERE id = \''.$id.'\' LIMIT 1;'; echo $sql_3; mysql_query($sql_3); } } else { $errors .= '<li class="errors">Unfortunately the file '.$name.$extension.' could not be uploaded. Please try again later.</li>'; echo '<div>Sorry, the following errors were encountered:'; echo $errors; echo '</div>'; echo '<p><hr width="100%" size="1" color="#CCCCCC" /></p>'; form(); } Link to comment https://forums.phpfreaks.com/topic/45938-deleting-a-mysql-row-just-after-creating-it/ Share on other sites More sharing options...
brissy_matty Posted April 7, 2007 Share Posted April 7, 2007 Why dont you just move your INSERT INTO statement so it occurs after the following code: if (move_uploaded_file($_FILES['file']['tmp_name'], "$path$name$extension")) { echo '<p>Thank you '.$uploader.', the file \''.$name.$extension.'\' has been uploaded.</p>'; </p>'; That way the INSERT only carries out if the upload is successful. If the Upload is not successful - then no insert is carried out which removes to problem of having to delete something all together Cheers Link to comment https://forums.phpfreaks.com/topic/45938-deleting-a-mysql-row-just-after-creating-it/#findComment-223517 Share on other sites More sharing options...
PC Nerd Posted April 8, 2007 Share Posted April 8, 2007 not sure but the delete should be a drop wuery shouldnt it??? not sure though good luck Link to comment https://forums.phpfreaks.com/topic/45938-deleting-a-mysql-row-just-after-creating-it/#findComment-223964 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.