I know how to display the data from a mySQL table, but how can I edit it via a webpage? Instead of going into phpMyAdmin you can edit it on a webpage. Is this possible?
Printable View
I know how to display the data from a mySQL table, but how can I edit it via a webpage? Instead of going into phpMyAdmin you can edit it on a webpage. Is this possible?
i dnt think you can.. buh am not sure.. ><"
I'm pretty sure you can, I think i've seen it done.
Just make a script so you can select from a drop down which you want to edit then it inserts the values into input boxes or text areas and on submit you make a mysql_query to update the ID
something like that, (more or less)PHP Code:$column = "Hi";
$update = mysql_query("UPDATE dbname SET `columnname` = '$column');
edit=
so if you have a page "edit.html'
with
<form action="update.php" method="POST">
<input tye="text" name="column1" value="Hi" /><br />
<input type="submit" name="update" value="Update the db" />
</form>
Then your update.php would be something like
or somethingPHP Code:
if($_POST["update"]){ //if update button is clicked
$column = $_POST["column1"]; //the field 'column1's stuff you entered
$update = mysql_query("UPDATE dbname SET `columnname` = '$column'); //update
echo("Db updated!");
}else{
echo("you did not hit the submit button!");
}
^^ easier?PHP Code:
if($_POST["update"]){ //if update button is clicked
$column = $_POST["column1"]; //the field 'column1's stuff you entered
$update = mysql_query("UPDATE dbname SET `columnname` = '$column'"); //update
echo("Db updated");
} else {
echo(" <form method=\"POST\">
<input tye=\"text\" name=\"column1\" value=\"Hi\" /><br />
<input type=\"submit\" name=\"update\" value=\"Update the db\" />
</form>");
}
I want an edit option/link beside each comment, then if you click the link you'll be able to edit whatever commetn was there. Is that possible?
So say I have ten comments, and the edit link beside each comment. Lets say I click the edit link beside comment #4, ai'll be taken to a form where I can edit comment #4.
Yea..
On the edit comment button, make it link to comment.php?id=*comment id*
In comment.php
:)PHP Code:<?php
function clean($str)
{
$str = strip_tags(addslashes(stripslashes(htmlspecialchars($str))));
$str = mysql_real_escape_string($str);
}
$comment = clean($_POST[comment]);
$id = clean($_GET[id]);
if(isset($comment) || !empty($comment))
{
mysql_query("update comments set comment = '$comment' where id = '$id'");
echo("This comment has been edited successfully!");
echo('<meta http-equiv="refresh" content="2;url=index.html" />');
}
else
{
?>
<form action="comment.php" method="post">
<label for="comment">Edit Comment:</label>
<textarea name="comment" cols="40" rows="6"><?php
$get_current = mysql_fetch_array(mysql_query("select comment from comments where id = '$id'"));
echo("$get_current[comment]");
?>
</textarea>
<br /><br />
<input type="submit" value="Edit Comment!" />
</form>
<?php } ?>
Thanks, but I don't understand how I can get the comment ID?
I can't edit, please merge.
select comment from comments where id = '$id'
If my database table was petitio, would I replace comment or comments with petition? I'm pretty sure i'd replace comments but I want to double check.
And, I would really appreciate if someone could tell me how to get the ID, as this is the only thing holding me back now.
Add a new field to your database, make it a primary key and auto-increment.
Then do
And im presuming your using a while loop to display comments, so it should be in that..PHP Code:<?php
$get_id = mysql_fetch_array(mysql_query("select id from comments"));
$id = $get_id[id];
?>
Of course it is possible. Not very clean code, not on my PHPDesigner computer.
Then edit.php:PHP Code:<?php
include "config.php";
$get_query = mysql_query("SELECT * FROM table ORDER BY id DESC");
while($row = mysql_fetch_array($get_query)) {
$field = $row["field"];
$id = $row["id"];
echo "$field - <a href=\"edit.php?id=$id\">Edit</a>";
}
?>
PHP Code:<?php
include "config.php";
$id = $_GET["id"]; // Be sure to use a good clean function to do it with. If you have one uncomment the next line
# $id = Clean($_GET["id"]);
if($_GET["action"] == "edit") {
$field = $_POST["field"];
# $field = clean($_POST["field"]);
mysql_query("UPDATE table SET field='$field' WHERE id='$id'") or die(mysql_error());
echo "Record updated!";
exit;
}
$get = mysql_query("SELECT * FROM table WHERE id='$id' LIMIT 0,1");
$row = mysql_fetch_array($get);
?>
<form action="?action=edit&id=<?php echo $_GET["id"]; ?>" method="post">
<table>
<tr><td>Field:</td> <td><input type="text" name="field" value="<?php echo $row["field"]; ?>"></tr>
<tr><td> </td> <td><input type"submit" name="submit" value="Edit"></td>
</table>
</form>
Whats special about your PHPDesigner computer?
Good luck with the editing :)
Read my post.. :p
I have, read my new thread pls. :)