user4690774
user4690774

Reputation:

Edit data from Mysql using php

I'm already displaying the MySQL data in a PHP table with a edit function, but I'm not able to edit them, and I don't know why.

<?php
require("db.php");
$id =$_REQUEST['rid'];

$result = mysql_query("SELECT * FROM replies WHERE rid = '$id'");
$test = mysql_fetch_array($result);
if (!$result) 
        {
        die("Error: Data not found..");
        }
                $trigger1=$test['trigger1'] ;
                $reply2= $test['reply2'] ;              

if (isset($_POST['save']))
{   
    $triggera = $_POST['trigger1'];
    $replyb = $_POST['reply2'];

    mysql_query("UPDATE `replies`(trigger,reply) VALUES ('$triggera','$replyb') WHERE rid = '$id'");  
    echo "Saved!";

}
mysql_close($conn);
?>

And this is MySQL database code:

CREATE TABLE IF NOT EXISTS `replies` (
  `trigger` text NOT NULL,
  `reply` text NOT NULL,
  `usercontrib` tinyint(4) NOT NULL DEFAULT '0',
  `rid` int(10) unsigned NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=332 DEFAULT CHARSET=utf8;

and finally:

    <form method="post">
        <table>
            <tr>
                <td>Title:</td>
                <td><input type="text" name="trigger" value="<?php echo $trigger1 ?>"/></td>
            </tr>
            <tr>
                <td>Author</td>
                <td><input type="text" name="reply" value="<?php echo $reply2 ?>"/></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" name="save" value="save" /></td>
            </tr>
        </table>
    </form>

When I click "edit" in the specifically data to modify, the fields in the "edit" page are empty, I think the "echo" on thme is not function.

Upvotes: 1

Views: 1540

Answers (2)

Paritosh Anand
Paritosh Anand

Reputation: 21

$_POST array will have key exactly same from the value of 'name' attribute in HTML form. So where you are checking if save isset in your php program you need to make following change -

$triggera = $_POST['trigger'];
$replyb = $_POST['reply'];

Also to check you can try printing the POST array on the page using print_r method.

Upvotes: 0

Chip Dean
Chip Dean

Reputation: 4302

Your update SQL is incorrect. Try this:

UPDATE `replies` SET trigger = '$triggera', reply = '$replyb' WHERE rid = '$rid'

Hope that helps!

Upvotes: 2

Related Questions