Jump to content

Deleting a mysql row just after creating it?


Edward

Recommended Posts

 

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.