MySQL Record Update Script

  • steve101
  • Beginner
  • Beginner
  • steve101
  • Posts: 35

Post 3+ Months Ago

Ok, i made a script, which appears to be correct, however it wont update the database, instead it just does nothing. It is split into two parts - the form page (update.php), and the updateprocess.php page

Here is the script around the form on the form page:

PHP Code: [ Select ]
  <?php   //connect to Database!
 
    include("dbconnect.php");
 
   
 
    //Get Values Query to display in form fields
 
   
 
   $query2='SELECT * FROM news WHERE newsid='.$newsid;
 
   $result2=mysql_query($query2,$db);
 
   
 
   //Condition
 
   
 
   if (mysql_num_rows($result2)==0) {
 
   
 
   // Part 1
 
  $newstitle2='Error';
 
  $newscontent2='This news item does not exist.';
 
  $maincontent2='blank';
 
  $image2='';
 
 
 
  // Part 2
 
 
 
  } else {
 
 
 
 
 
   $newstitle2=mysql_result($result2,0,'newstitle');
 
   $newscontent2=mysql_result($result2,0,'newscontent');
 
   $maincontent2=mysql_result($result2,0,'maincontent');
 
   $image2=mysql_result($result2,0,'image');
 
}
 
?>
  1.   <?php   //connect to Database!
  2.  
  3.     include("dbconnect.php");
  4.  
  5.    
  6.  
  7.     //Get Values Query to display in form fields
  8.  
  9.    
  10.  
  11.    $query2='SELECT * FROM news WHERE newsid='.$newsid;
  12.  
  13.    $result2=mysql_query($query2,$db);
  14.  
  15.    
  16.  
  17.    //Condition
  18.  
  19.    
  20.  
  21.    if (mysql_num_rows($result2)==0) {
  22.  
  23.    
  24.  
  25.    // Part 1
  26.  
  27.   $newstitle2='Error';
  28.  
  29.   $newscontent2='This news item does not exist.';
  30.  
  31.   $maincontent2='blank';
  32.  
  33.   $image2='';
  34.  
  35.  
  36.  
  37.   // Part 2
  38.  
  39.  
  40.  
  41.   } else {
  42.  
  43.  
  44.  
  45.  
  46.  
  47.    $newstitle2=mysql_result($result2,0,'newstitle');
  48.  
  49.    $newscontent2=mysql_result($result2,0,'newscontent');
  50.  
  51.    $maincontent2=mysql_result($result2,0,'maincontent');
  52.  
  53.    $image2=mysql_result($result2,0,'image');
  54.  
  55. }
  56.  
  57. ?>


Code: [ Select ]
<form action="updateprocess.php" method="post">
    <table width="629" border="0" cellspacing="0" cellpadding="00">
     <tr>
      <td width="104" height="27" valign="top"><strong>Title of Article </strong></td>
      <td valign="top"><input name="newstitle" type="text" class="form" id="newstitle" value="<?php echo $newstitle2;?>" size="30"></td>
      <td valign="top">This is what will appear as the Article Title </td>
     </tr>
     <tr>
      <td height="26" valign="top"><strong>Image URL <br>
      </strong></td>
      <td width="190" valign="top"><input name="image" type="text" class="form" id="image" value="<?php echo $image2;?>" size="30"> </td>
      <td width="335" valign="top">Leave this Field as it is (do not change the Image URL) <?php echo '<input type="hidden" name="action" value="update">';?>
          </td>
     </tr>
     <tr>
      <td height="84" valign="top"><strong>Brief Description</strong></td>
      <td valign="top"><textarea name="maincontent" cols="30" rows="4" wrap="VIRTUAL" class="form" id="maincontent"><?php echo $maincontent2;?>
</textarea> </td>
      <td valign="top">This is the leader text which appears on the home page. An idea would be type out the <strong>Full Content first </strong>(below), and then copy the first few lines of it into this text box here. </td>
     </tr>
     <tr>
      <td height="86" valign="top"><strong>Full Content</strong></td>
      <td valign="top"><textarea name="newscontent" cols="30" rows="4" wrap="VIRTUAL" class="form" id="newscontent"><?php echo $newscontent2;?>
</textarea></td>
      <td valign="top">This what will appear on the news page related to this article, for example news.php?newsid=5. In this box enter the <strong>FULL </strong>Content. If you ever want to break sections of content up, i.e adding paragraphs add this code before the break &lt;br&gt;&lt;br&gt;. </td>
     </tr>
     <tr>
      <td>&nbsp;</td>
      <td><input name="Submit" type="submit" class="form" value="Update"></td>
      <td valign="top"><strong>[<a href="delete.php">Delete This Article!</a>] </strong></td>
     </tr>
    </table>
   </form> 
  1. <form action="updateprocess.php" method="post">
  2.     <table width="629" border="0" cellspacing="0" cellpadding="00">
  3.      <tr>
  4.       <td width="104" height="27" valign="top"><strong>Title of Article </strong></td>
  5.       <td valign="top"><input name="newstitle" type="text" class="form" id="newstitle" value="<?php echo $newstitle2;?>" size="30"></td>
  6.       <td valign="top">This is what will appear as the Article Title </td>
  7.      </tr>
  8.      <tr>
  9.       <td height="26" valign="top"><strong>Image URL <br>
  10.       </strong></td>
  11.       <td width="190" valign="top"><input name="image" type="text" class="form" id="image" value="<?php echo $image2;?>" size="30"> </td>
  12.       <td width="335" valign="top">Leave this Field as it is (do not change the Image URL) <?php echo '<input type="hidden" name="action" value="update">';?>
  13.           </td>
  14.      </tr>
  15.      <tr>
  16.       <td height="84" valign="top"><strong>Brief Description</strong></td>
  17.       <td valign="top"><textarea name="maincontent" cols="30" rows="4" wrap="VIRTUAL" class="form" id="maincontent"><?php echo $maincontent2;?>
  18. </textarea> </td>
  19.       <td valign="top">This is the leader text which appears on the home page. An idea would be type out the <strong>Full Content first </strong>(below), and then copy the first few lines of it into this text box here. </td>
  20.      </tr>
  21.      <tr>
  22.       <td height="86" valign="top"><strong>Full Content</strong></td>
  23.       <td valign="top"><textarea name="newscontent" cols="30" rows="4" wrap="VIRTUAL" class="form" id="newscontent"><?php echo $newscontent2;?>
  24. </textarea></td>
  25.       <td valign="top">This what will appear on the news page related to this article, for example news.php?newsid=5. In this box enter the <strong>FULL </strong>Content. If you ever want to break sections of content up, i.e adding paragraphs add this code before the break &lt;br&gt;&lt;br&gt;. </td>
  26.      </tr>
  27.      <tr>
  28.       <td>&nbsp;</td>
  29.       <td><input name="Submit" type="submit" class="form" value="Update"></td>
  30.       <td valign="top"><strong>[<a href="delete.php">Delete This Article!</a>] </strong></td>
  31.      </tr>
  32.     </table>
  33.    </form> 


That is the code which covers the form.

Here is the code on the updateprocess page:

PHP Code: [ Select ]
<?php include("dbconnect.php");
 
 
 
//get the ID of the News Article
 
 
 
$newsid=$_GET["newsid"];
 
?>
 
<?php
 
//Get the Action from the form
 
 
 
    $action=$_POST["action"];
 
  if ($action=='update') {
 
 
 
   # Comment has been saved, so add to the database
 
   
 
   //Variables
 
   
 
        $newstitle=$_POST["newstitle"];
 
   $image=$_POST["image"];
 
    $maincontent=$_POST["maincontent"];
 
   $newscontent=$_POST["newscontent"];
 
   
 
   //update Query Start
 
   
 
   $query='UPDATE news
 
      SET newstitle = "'.$newstitle.'"
 
      newscontent="'.$newscontent.'"
 
      image ="'.$image.'"
 
      maincontent="'.$maincontent.'"
 
      where newsid='.$newsid.' LIMIT 1'; 
 
 
 
  // result
 
 
 
   $result=mysql_query($query,$db);
 
       
 
      // update saved to database, refresh home page!!
 
     
 
   echo '<meta HTTP-EQUIV="REFRESH" content="0; url=index.php">';
 
  die;
 
  }
 
?>
  1. <?php include("dbconnect.php");
  2.  
  3.  
  4.  
  5. //get the ID of the News Article
  6.  
  7.  
  8.  
  9. $newsid=$_GET["newsid"];
  10.  
  11. ?>
  12.  
  13. <?php
  14.  
  15. //Get the Action from the form
  16.  
  17.  
  18.  
  19.     $action=$_POST["action"];
  20.  
  21.   if ($action=='update') {
  22.  
  23.  
  24.  
  25.    # Comment has been saved, so add to the database
  26.  
  27.    
  28.  
  29.    //Variables
  30.  
  31.    
  32.  
  33.         $newstitle=$_POST["newstitle"];
  34.  
  35.    $image=$_POST["image"];
  36.  
  37.     $maincontent=$_POST["maincontent"];
  38.  
  39.    $newscontent=$_POST["newscontent"];
  40.  
  41.    
  42.  
  43.    //update Query Start
  44.  
  45.    
  46.  
  47.    $query='UPDATE news
  48.  
  49.       SET newstitle = "'.$newstitle.'"
  50.  
  51.       newscontent="'.$newscontent.'"
  52.  
  53.       image ="'.$image.'"
  54.  
  55.       maincontent="'.$maincontent.'"
  56.  
  57.       where newsid='.$newsid.' LIMIT 1'; 
  58.  
  59.  
  60.  
  61.   // result
  62.  
  63.  
  64.  
  65.    $result=mysql_query($query,$db);
  66.  
  67.        
  68.  
  69.       // update saved to database, refresh home page!!
  70.  
  71.      
  72.  
  73.    echo '<meta HTTP-EQUIV="REFRESH" content="0; url=index.php">';
  74.  
  75.   die;
  76.  
  77.   }
  78.  
  79. ?>



Any ideas why it wont actually update the fields?

Thanks,


Steve
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

First of all, try with this:
PHP Code: [ Select ]
 
$query="UPDATE news SET newstitle='$newstitle', newscontent='$newscontent', image='$image', maincontent='$maincontent' WHERE newsid='$newsid' LIMIT 1";
 
 
  1.  
  2. $query="UPDATE news SET newstitle='$newstitle', newscontent='$newscontent', image='$image', maincontent='$maincontent' WHERE newsid='$newsid' LIMIT 1";
  3.  
  4.  


Sometimes, I use to print the query variable in the PHP so I can copy and paste it to the MySQL client and test the query for any syntax error.
  • steve101
  • Beginner
  • Beginner
  • steve101
  • Posts: 35

Post 3+ Months Ago

thanks, but its still not wanting to work... don't know what it could be...
  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

Did you try printing the SQL query as I told?
  • steve101
  • Beginner
  • Beginner
  • steve101
  • Posts: 35

Post 3+ Months Ago

yep, just looked at something... got it working now, thanks!
  • Tannu4u
  • Proficient
  • Proficient
  • User avatar
  • Posts: 480
  • Loc: India

Post 3+ Months Ago

Try Using this query:

$query='UPDATE news
SET newstitle = '".$newstitle."'
newscontent='".$newscontent."'
image ='".$image."'
maincontent='".$maincontent."'
where newsid='.$newsid.' LIMIT 1';
  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

Tannu4u wrote:
Try Using this query:

$query='UPDATE news
SET newstitle = '".$newstitle."'
newscontent='".$newscontent."'
image ='".$image."'
maincontent='".$maincontent."'
where newsid='.$newsid.' LIMIT 1';


That query won't work as it has syntax errors.
  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Posts: 401
  • Loc: Scorpion Hole

Post 3+ Months Ago

I helped him out a while ago, I don't know which problem it was, I just rewrote his whole page, but it works now.

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 65 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.