Updating mySQL data using PHP forms.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yenfa
    New Member
    • Apr 2008
    • 2

    Updating mySQL data using PHP forms.

    I've done a simple PHP-thing which adds data into my mySQL database, using that "PHP $_POST" thing, now I want to do editing that data through PHP form again, but I don't know how to do that. I've tried once, but it failed.

    Code:
    <?php
    
    $author=$_POST['author'];
    $date=$_POST['date'];
    $shortstory=$_POST['story'];
    
    mysql_connect ("dbserver", "user", "pass") or die ('Error: ' .mysql_error());
    mysql_select_db ("dbname");
    
    $query = "INSERT INTO blog (author, date, shortstory) VALUES ('".$author."', '".$date."', '".$shortstory."')";
    
    mysql_query($query) or die ('Error updating database');
    
    echo "Database Updated! <br />
    Data Inserted : <br />
    ";
    ?>
    Now, I wanted to do is if I add a "news" then, I'll be able to edit that, just like in blogs or news cms.

    I'm just a newbie in PHP, so I don't know much yet in PHP and mySQL...
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Did you try that with MySQL Update query?

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      ...using that "PHP $_POST" thing...

      I'm just a newbie in PHP, so I don't know much yet in PHP and mySQL...
      When this is the level you want to discuss a PHP problem, you'd better think twice and follow some PHP tutorials before you continue any coding. Start with the w3schools.com tutorials and you'll get there.

      Ronald

      Comment

      • yenfa
        New Member
        • Apr 2008
        • 2

        #4
        Originally posted by hsriat
        Did you try that with MySQL Update query?
        Yeah, I know that, but how could I edit through a PHP form?

        Comment

        • lokie538
          New Member
          • Mar 2008
          • 5

          #5
          Pass the edited info from the form to another page and then use a update quiery to store the info and then use the header function to relocate the browser to what ever page.

          Comment

          • rpnew
            New Member
            • Aug 2007
            • 189

            #6
            Hi,
            It is similar to what you've done so far. Instead of INSERT query you need to use UPDATE query in your code which you've shown as an example. The steps are similar to what you are doing just replace INSERT with PROPER UPDATE statement.

            Regards,
            RP

            Comment

            • hsriat
              Recognized Expert Top Contributor
              • Jan 2008
              • 1653

              #7
              First fetch the same data from database by SELECT query. Then fill that data in the value attribute of the inputs in your form, and then after user clicks submit, run the UPDATE query.[php]$sql = mysql_query("SE LECT * FROM.......") or die(mysql_error ());
              $info = mysql_fetch_arr ay($sql); //if your data is only on one row
              echo "<input type=\"text\" name=\"data\" value=\"".$info['data']."\" />";
              //--- and in the updating portion
              $sql = mysql_query("UP DATE ........") or die(mysql_error ());[/php]

              Hope it helps...

              Comment

              Working...