View Full Version : PHP - Edit mySQL entries via webpage?
adamFTW
10-12-2007, 11:50 PM
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.. ><"
adamFTW
11-12-2007, 12:03 AM
I'm pretty sure you can, I think i've seen it done.
tgb-hosting
11-12-2007, 08:13 AM
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
[Oli]
11-12-2007, 10:35 AM
$column = "Hi";
$update = mysql_query("UPDATE dbname SET `columnname` = '$column');
something like that, (more or less)
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
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!");
}
or something
DeejayMachoo$
11-12-2007, 11:40 AM
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>");
}
^^ easier?
[Oli]
11-12-2007, 11:49 AM
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>");
}
^^ easier?
Obviously :p
Just thought he might not know php that well (& not know you have to backslash the "s in the form so I thought id do it with an html page ;) )
adamFTW
11-12-2007, 01:14 PM
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.
[Oli]
11-12-2007, 01:52 PM
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.
Yes it's possible (I have it on TemplateNation's auction comments/bids)
But to be honest.
Make sure you know php first, becouse if you don't you'll get stuck ;l
MrCraig
11-12-2007, 01:58 PM
Yea..
On the edit comment button, make it link to comment.php?id=*comment id*
In comment.php
<?php
function clean($str)
{
$str = strip_tags(addslashes(stripslashes(htmlspecialchar s($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 } ?>
:)
adamFTW
12-12-2007, 01:27 AM
Thanks, but I don't understand how I can get the comment ID?
adamFTW
12-12-2007, 02:22 AM
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.
MrCraig
12-12-2007, 04:07 PM
Add a new field to your database, make it a primary key and auto-increment.
Then do
<?php
$get_id = mysql_fetch_array(mysql_query("select id from comments"));
$id = $get_id[id];
?>
And im presuming your using a while loop to display comments, so it should be in that..
Dentafrice,
12-12-2007, 08:31 PM
Of course it is possible. Not very clean code, not on my PHPDesigner computer.
<?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>";
}
?>
Then edit.php:
<?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>
Florx
12-12-2007, 08:44 PM
Whats special about your PHPDesigner computer?
Good luck with the editing :)
Dentafrice,
12-12-2007, 08:46 PM
Whats special about your PHPDesigner computer?
Good luck with the editing :)
It has phpDesigner 2008 Enterprise edition, and it cleans and formats my code..
adamFTW
12-12-2007, 09:26 PM
Add a new field to your database, make it a primary key and auto-increment.
Then do
<?php
$get_id = mysql_fetch_array(mysql_query("select id from comments"));
$id = $get_id[id];
?>
And im presuming your using a while loop to display comments, so it should be in that..
How do I make it auto increment?
AND, what would the link code be?
Dentafrice,
12-12-2007, 10:20 PM
Read my post.. :p
adamFTW
12-12-2007, 10:25 PM
I have, read my new thread pls. :)
Want to hide these adverts? Register an account for free!
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.