Jump to content

Date not getting deleted


monkeybidz
 Share

Recommended Posts

I have this code to update mysql db. It deletes and updates everything in the row except for the date. The date remains the same. In my db i have it under "prices" and it is named "date". Here is the code I am using.

 

if($user_id !="" && $name !="" && $update=="yes"){
    mysql_query("DELETE FROM prices WHERE id='".$_POST['id']."'");
    $resultado = "You successfully updated this posting!<br>Thank you for participating."; 
$date=getdate();
$insert=mysql_query("INSERT INTO prices VALUES ('', '$user_id', '$name', '$bus_type', '$street', '$city', '$state', '$zip_code', '$brand', '$quantity', '$price1', '$price2', now())");

} 

Link to comment
Share on other sites

Well, it is to update the prices and it has to display the date it was updated. I just noticed that it is not deleting anything at all, it is just creating a new row with all the information and new date. I tried changing it to this:

if($user_id !="" && $name !="" && $update=="yes"){
    mysql_query("DELETE id, user_id, name, bus_type, street, city, state, zip_code, brand, quantity, price1, price2, date FROM prices WHERE id='".$id."'");
    $resultado = "You successfully updated this posting!<br>Thank you for participating."; 
$date=getdate();
$insert=mysql_query("INSERT INTO prices VALUES ('', '$user_id', '$name', '$bus_type', '$street', '$city', '$state', '$zip_code', '$brand', '$quantity', '$price1', '$price2', now())");

} 

 

I get the same result. How would I make it to just update the row instead of deleting?

Link to comment
Share on other sites

I tried this, but nothing:

 

mysql_query("UPDATE prices SET price1='".$price1."', price2='".$price2."', date='".$date."' WHERE id='".$id."' ");

 

Would I have to declare each value or just the ones I need updated?

 

 

You would declare only the field values that you want to update.

If this is not working for you, add a little debugging.

 

$sql = "UPDATE prices SET price1='$price1', price2='$price2', date='$date' WHERE id='$id'";
mysql_query($sql) or die($sql . "<br />" . mysql_error());

Link to comment
Share on other sites

Ok, with a bit of info from all you guys, It is now resolved. I went with a full delete and new insert. This was required as the person that updates the record will now be assigned to be the person that edited the post. Only the id, price1, price2 and date are really changed by the user. The rest of the vars are echoed on a form and are not editable by the user. Here is what I went with.:

 

if($user_id !="" && $name !="" && $update=="yes"){
    
$query = "DELETE FROM prices WHERE id='$id'";
    mysql_query($query);
echo mysql_error();	
    $resultado = "You successfully updated this posting!<br>Thank you for participating."; 
$insert=mysql_query("INSERT INTO prices VALUES ('', '$user_id', '$name', '$bus_type', '$street', '$city', '$state', '$zip_code', '$brand', '$quantity', '$price1', '$price2', now())");

}

 

Thanks a bunch people. ;D

Link to comment
Share on other sites

The problem with deleting a record and reinserting a new one when all you need to do is update an existing record is that you end up with a new, different primary key, thereby possibly destroying the referential integrity of the database. Take the time to make the update query work.

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.