2

I am trying to update the data from my database but they don't show up when I go to the edit page. Any help? Thank you.

index.php

<?php
    include_once('db.php');
    if(isset($_POST['description']))
    {
        $description = $_POST['description'];

        if(mysql_query("INSERT INTO about VALUES('','$description')"))
            echo "Sucacessful Update";
        else
            echo "Please try again";
    }
            $res = mysql_query("SELECT * FROM about");
?>

<form action = "." method="POST">
Name <input type = "text" name="description"><br/>

<input type="submit" value="Save">
</form>

<?php
    while( $row = mysql_fetch_array($res) )
        echo "$row[id]. $row[description] <a href='edit.php?edit=$row[description]'>edit</a><br />";
?>

edit.php

<?php
    include_once('db.php');

    if( isset($_GET['edit']) )
    {
        $id = $_GET['edit'];
        $res= mysql_query("SELECT * FROM about WHERE id='$id'");
        $row= mysql_fetch_array($res);
    }

    if( isset($_POST['newDescription']) )
    {
        $newDescription = $_POST['newDescription'];
        $id      = $_POST['id'];
        $sql     = "UPDATE about SET description='$newDescription' WHERE id='$id'";
        $res     = mysql_query($sql) 
                                    or die("Could not update".mysql_error());
        echo "<meta http-equiv='refresh' content='0;url=index.php'>";
    }

?>
<form action="edit.php" method="POST">
Name: <input type="text" name="newDescription" value="<?php echo $row[1]; ?>"><br />
<input type="hidden" name="id" value="<?php echo $row[0]; ?>">
<input type="submit" value=" Update "/>
</form>

The name from my table is about and I have two columns which are id and description. The one that I'm trying to do here is to edit the description column through php.

Louie
  • 219
  • 4
  • 13

1 Answers1

0

Pass the row's id and not the description to the edit.php script and all will work better

<?php
    while( $row = mysql_fetch_array($res) )
        echo "$row[id]. $row[description] <a href='edit.php?edit=$row[id]'>edit</a><br />";
?>

Please dont use the mysql_ database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions, and here is some help to decide which to use

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • my problem was solved. thank you sir! And thanks for the link, I am just starting to learn PHP. – Louie Jan 16 '16 at 18:00