RE: DB will INSERT wont UPDATE..error in code?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I'm no 'expert' (opening myself up for much criticism here) but try the
following:

(I'm assuming you are setting $fid somewhere)

 if (empty($form_status))
 {
    $staff = $_POST["staff"];
    $ttlpst = $_POST["ttlpst"];
    $mnthpst = $_POST["mnthpst"];
    $ttlicon = $_POST["ttlicon"];
    $mnthicon = $_POST["mnthicon"];

    $exists = "SELECT * FROM badges WHERE fid = $fid";
    $result = mysql_query($exists) or die("Query1 failed: ".mysql_error());
    if (mysql_num_rows($result) == 0)
    {
      $sql = "INSERT INTO badges
             (fid,staff,ttlpst,mnthpst,ttlicon,mnthicon)
      VALUES ('$fid','$staff','$ttlpst','$mnthpst','$ttlicon','$mnthicon')";
      echo "Insert query = $sql";
      mysql_query($sql) or die("Query2 failed: ".mysql_error());
      $form_status = "<span class=\"msgUpdated\">Description
Inserted!</span><br>\n";
    }
    else
    {
      $sql = "UPDATE badges
              SET staff = '$staff'
                  ttlpst ='$ttlpst'
                  mnthpst = '$mnthpst'
                  ttlicon = '$ttlicon'
                  mnthicon = '$mnthicon'
              WHERE fid = $fid";
      echo "Update query = $sql";
      mysql_query($sql) or die("Query3 failed: ".mysql_error());
      $form_status = "<span class=\"msgUpdated\">Description
Updated!</span><br>\n";
    }
 }

The echos will at least show the SQL being executed which could give a clue
to the problem.

You could even just set the $sql within the if...else... and use the same
mysql_query($sql) for both instances:

if (condition1)
{
    $sql = "query statement 1";
}else{
    $sql = "query statement 2";
}
mysql_query($sql);

HTH

Graham

> -----Original Message-----
> From: Matthew Kiehne [mailto:speakez@xxxxxxxxxxxxxxxxx]
> Sent: 12 October 2004 21:09
> To: php-db@xxxxxxxxxxxxx
> Subject:  DB will INSERT wont UPDATE..error in code?
>
>
> forgive the poor structure of my code, im pretty new at this and havent
> figured out exactly what it should be formed like to look its best, anyway
> whenever i run this code it works on inserting new information into the
> database, however when i want to update the SQL query returns an
> output like
> this 'SQL statement = UPDATE badges
> (staff,ttlpst,mnthpst,ttlicon,mnthicon)
> VALUES ('1','1','1','/ribbons','/ribbons') WHERE fid=2' but when i try to
> pull the information from the database it hasnt updated, can
> someone help me
> out with this...im not sure why it wouldnt update even though the
> statement
> looks correct.
>
[snip]

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux