Jump to content

I need help please


madmindz

Recommended Posts

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";
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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';

}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";

}

?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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';

}

?>

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.