Update a database table using PHP

Discussion in 'PHP' started by red-x, Jul 17, 2008.

  1. #1
    Hi, I'm trying to update a database with news, but I can't when I hit the submit button the form content gets erase and nothing happens in just a empty form. No errors. I'm getting the $id from the url since is coming from another page.

    Here's the code I'm using ...
    <?php
    $user = '***';
    $pass = '***';
    $host = 'localhost';
    $db = 'test';
    
    // connect to SQL
    $cid = mysql_connect($host,$user,$pass) or die(mysql_error());
    mysql_select_db($db) or die(mysql_error());
        
            //execute SQL statement
            
         if (!isset($_POST["submit"])) {
             $id = $_GET["id"];
            $SQL = "SELECT * FROM news WHERE id = '$id' ";
        $result = mysql_query($SQL) or die(mysql_error().' SQL: '.$SQL);
        $row = mysql_fetch_array($result);
            $title= $row["title"];
            $news = $row["news"];
    
    ?>
    
    <FORM NAME="fa" action="editsave.php" METHOD="POST">
    <INPUT TYPE="hidden" NAME="id" VALUE="<?php echo ("$id"); ?>" />
    <B>Title:</B><br /><INPUT TYPE="text" name="title" VALUE="<?php echo ("$title"); ?>" SIZE=40 /><br />
    <B>News:</B><br /><TEXTAREA name="news" ROWS=5 COLS=40><?php echo ("$news"); ?></TEXTAREA>
    <P><INPUT TYPE="submit" VALUE="Update" /></P>
    </FORM>
    
    <?php 
    } 
    
    if (isset($_POST["$submit"])) {
    
            $title = $_POST["title"];
          $news = $_POST["news"];
    
            //setup SQL statement
            
            $SQL= " UPDATE news SET news='$news', title='$title' WHERE id='$id'";
            
            $result = mysql_db_query($db, "$SQL", $cid);
    
            //check for errors
            if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n");    }
            
            echo "news Updated";
            }
    ?>
    PHP:
    And here's the database table...

    CREATE TABLE news (
      id bigint(11) NOT NULL auto_increment,
      news mediumtext NOT NULL,
      title varchar(255) NOT NULL,
      newsdate varchar(255) NOT NULL,
      PRIMARY KEY  (id)
    );
    Code (markup):
    Hope anyone can help me. Thanks in advance! :)
     
    red-x, Jul 17, 2008 IP
  2. projectWORD

    projectWORD Active Member

    Messages:
    287
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #2
    projectWORD, Jul 17, 2008 IP
  3. tehgamecat

    tehgamecat Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    add
    $id = $_POST["id"];

    after:
    $title = $_POST["title"];
    $news = $_POST["news"];
     
    tehgamecat, Jul 17, 2008 IP
  4. red-x

    red-x Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you for your replies guys.

    Yes, where do I need to add the $_GET['$id']; I thought I was going that already because it works, the only problem is that is not updating the table.

    No it didn't work. :(
     
    red-x, Jul 17, 2008 IP
  5. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #5
    if (isset($_POST["$submit"])) {
    PHP:
    Should that be

    if ($_POST["submit"] == 'Update') {
    PHP:
    Then I would put an extra id = in below that.

            
          $id    = $_POST['id'];
          $title = $_POST["title"];
          $news = $_POST["news"];
    
    PHP:
     
    exodus, Jul 17, 2008 IP
  6. SnoozZz

    SnoozZz Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    leave the $_GET and $_POST

    use this

    $id = $_REQUEST['id'];
     
    SnoozZz, Jul 17, 2008 IP
  7. projectWORD

    projectWORD Active Member

    Messages:
    287
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #7
    Well what I would do in this situation is to "print $SQL;" at the end of your program.
    This will then show you exactly what query is being ran.

    If all variables are printed correctly in the statement, copy the printed SQL statement into your phpMyAdmin or other SQL control panel and see if that paticular query can be passed manually. Normally if it is a bad query, SQL will give you a better error message
     
    projectWORD, Jul 17, 2008 IP
  8. SnoozZz

    SnoozZz Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    your form submit action is looking for page editsave.php

    does it exist?
     
    SnoozZz, Jul 17, 2008 IP