madmindz Posted July 11, 2007 Share Posted July 11, 2007 Can anyone help me with this code? here is what I want to do. This block of code works well to insert the data in my table, but what I want it to do is actually update the data that is already there and if no data is there to insert new data. What it keeps doing is adding another row of data instead of replacing the data with the new info. Here is the code. <?php if(isset($_POST['save'])) { $heading = $_POST['heading']; $updates = $_POST['updates']; if(!get_magic_quotes_gpc()) { $heading = addslashes($heading); $updates = addslashes($updates); } $query = " INSERT INTO news (heading, updates) ". " VALUES ('$heading', '$updates')"; mysql_query($query) or die('Error ,query failed'); include 'library/closedb.php'; echo "Update '$heading' added"; } ?> Quote Link to comment Share on other sites More sharing options...
gloveny Posted July 11, 2007 Share Posted July 11, 2007 Cant belive your asking this but...... Are you intending to have only one record in the database, if so then you can simply use the following... $query = " UPDATE news SET heading='$heading', updates='$updates'"; But surly you will be adding more records as time goes by in which case you'll need to use the ID of thr recod you want to update. like the following... $query = " UPDATE news SET heading='$heading', updates='$updates' WHERE table_name_id='$ID'"; Graham Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 11, 2007 Share Posted July 11, 2007 Your table needs to have a unique field. I'm not sure if heading is unique in your table, but I will assume it is for example purposes: <?php if(isset($_POST['save'])) { $heading = $_POST['heading']; $updates = $_POST['updates']; if(!get_magic_quotes_gpc()) { $heading = addslashes($heading); $updates = addslashes($updates); } $query = "SELECT heading FROM news WHERE heading = '$heading'"; $result = mysql_query($query) or DIE ('Error ,query failed<br>'.mysql_error()); if (mysql_num_rows($result)==0) { //New record $query = "INSERT INTO news (heading, updates) VALUES ('$heading', '$updates')"; mysql_query($query) or DIE ('Error ,query failed<br>'.mysql_error()); echo "'$heading' added"; } elseif (mysql_num_rows($result)) { //Update record $query = "UPDATE news SET heading = '$heading', updates='$updates' WHERE heading = '$heading'"; mysql_query($query) or DIE ('Error ,query failed<br>'.mysql_error()); echo "'$heading' updated"; } else { //Error more than 1 match echo "Error: there were more than 1 records that matched this entry."; } include 'library/closedb.php'; } ?> Quote Link to comment Share on other sites More sharing options...
mpharo Posted July 11, 2007 Share Posted July 11, 2007 What he is saying is that he wants to do a check to see if it is already set, if it is then update if not then insert a new record. You can use the following as some test code. <?php if(isset($_POST['save'])) { $heading = $_POST['heading']; $updates = $_POST['updates']; if(!get_magic_quotes_gpc()) { $heading = addslashes($heading); $updates = addslashes($updates); } $select=mysql_query("SELECT * FROM news WHERE heading='$heading' AND updates='$updates'"); $rows=mysql_num_rows($select); If($rows==0){ $query = " INSERT INTO news (heading, updates) ". " VALUES ('$heading', '$updates')"; mysql_query($query) or die('Error ,query failed'); include 'library/closedb.php'; echo "Inserted '$heading' added as a new record"; }Else{ $query = " UPDATE news updates='$updates' WHERE heading='$heading'"; mysql_query($query) or die('Error ,query failed'); include 'library/closedb.php'; echo "'$heading' Updated with the current information"; } } ?> I am not sure what you key is on that table so you WILL need to modify this code. Quote Link to comment Share on other sites More sharing options...
madmindz Posted July 11, 2007 Author Share Posted July 11, 2007 Thanks guys my primary key is heading and there will be only one entry in that table. Everytime there is a new entry it should replace the older one. Quote Link to comment Share on other sites More sharing options...
madmindz Posted July 11, 2007 Author Share Posted July 11, 2007 GUYS I know you are trying to help me and its annoying to keep asking. Anyway, I am only a week old where php is concerned so if I seem a little confused please bear with me. The last 3 reply to my question didn't help much .The first from gloveny didn't do anything when i inserted it...the second from mjdamato was good but it kept inserting a new record instead of replacing the one thats already there. And the third from mpharo kept giving me a Parse error, unexpected T_VARIABLE in... Ok guys let me explain again what i want to do. The table should contain one record. The query should check to see if there is a record. If there is one then its updated, if not then one is added. The key for that table is heading. Here is the original code again guys. (Oh by the way I did not write this code i am only trying to modify it) :-) thanks. <?php if(isset($_POST['save'])) { $heading = $_POST['heading']; $updates = $_POST['updates']; if(!get_magic_quotes_gpc()) { $heading = addslashes($heading); $updates = addslashes($updates); } $query = " INSERT INTO news (heading, updates) ". " VALUES ('$heading', '$updates')"; mysql_query($query) or die('Error ,query failed'); include 'library/closedb.php'; echo "Update '$heading' added"; } ?> Quote Link to comment Share on other sites More sharing options...
per1os Posted July 11, 2007 Share Posted July 11, 2007 You have been helped dude. The last 2 posts are exactly what you want. If mpharos gives an error POST the whole error message, including the line. Understand that they have helped, whether you can get it to work or not is another question. Their reply's are valid and the logic is correct. With a reply like that I would doubt anyone would want to help you anymore, you may have just lost all chances by doing that crap. Quote Link to comment Share on other sites More sharing options...
madmindz Posted July 11, 2007 Author Share Posted July 11, 2007 hi frost what did i do wrong? I was just trying to explain myself. I tested each code and trying to explain what happened when i did. I can understand that the fact that you know it makes for a little arrogance but the whole essence of asking a question is to learn the answer. I am not trying to offend dear sir if that is what you are instigating. Why dont we have dialog where instead of telling me how dumb I am why dont you try to help me? (i did say I started a week ago) Quote Link to comment Share on other sites More sharing options...
simcoweb Posted July 11, 2007 Share Posted July 11, 2007 Just use UPDATE instead of SELECT. Basically if you have just one record it will over write it (meeting one of your conditions as you've stated above 'The query should check to see if there is a record. If there is one then its updated,') and if one doesn't exist it will insert it (meeting your second condition 'if not then one is added.'). http://dev.mysql.com/doc/refman/5.0/en/update.html Quote Link to comment Share on other sites More sharing options...
madmindz Posted July 11, 2007 Author Share Posted July 11, 2007 Thanks a million mjdamato your script worked. I changed the primary to id and it worked. I am just not sure why I couldn't keep using heading as the key. Thanks a lot all you guys. Your table needs to have a unique field. I'm not sure if heading is unique in your table, but I will assume it is for example purposes: <?php if(isset($_POST['save'])) { $heading = $_POST['heading']; $updates = $_POST['updates']; if(!get_magic_quotes_gpc()) { $heading = addslashes($heading); $updates = addslashes($updates); } $query = "SELECT heading FROM news WHERE heading = '$heading'"; $result = mysql_query($query) or DIE ('Error ,query failed<br>'.mysql_error()); if (mysql_num_rows($result)==0) { //New record $query = "INSERT INTO news (heading, updates) VALUES ('$heading', '$updates')"; mysql_query($query) or DIE ('Error ,query failed<br>'.mysql_error()); echo "'$heading' added"; } elseif (mysql_num_rows($result)) { //Update record $query = "UPDATE news SET heading = '$heading', updates='$updates' WHERE heading = '$heading'"; mysql_query($query) or DIE ('Error ,query failed<br>'.mysql_error()); echo "'$heading' updated"; } else { //Error more than 1 match echo "Error: there were more than 1 records that matched this entry."; } include 'library/closedb.php'; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.