View Full Version : [PHP] What's wrong with this....
adamFTW
13-12-2007, 12:38 AM
I have the following code, whatit's supposed to do is display the data from the mySQL database and display in a text box, but it isn't.
<?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") {
$name = $_POST["name"];
# $field = clean($_POST["field"]);
$grade = $_POST["grade"];
$comment = $_POST["comment"];
mysql_query("UPDATE petition SET name='$name', grade='$grade', comment='$comment' 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" method="post">
Name: <input name="name" type="text" value="<?php
$get_current = mysql_fetch_array(mysql_query("select name from petition where id = '$id'"));
echo('$get_current["name"]');
?>"/><br />
Grade: <input name="grade" type="text" value="<?php
$get_current = mysql_fetch_array(mysql_query("select grade from petition where id = '$id'"));
echo('$get_current["grade"]');
?>"/><br />
Comment:<br />
<input name="comment" type="text" value="<?php
$get_current = mysql_fetch_array(mysql_query("select comment from petition where id = '$id'"));
echo('$get_current["comment"]');
?>"/><br />
<input type="submit" value="Edit Comment!" />
</form>
Moved by Agesilaus (Forum Moderator) from Design and Development: Please post in the correct forum in future.
adamFTW
13-12-2007, 01:05 AM
Please merge.
Heres an image of what that code looks like on a webpage:
http://www.fovq.com/site/december 2007/err.gif
Lets take a look at the actual form:
<form action="?action=edit" method="post">
Name: <input name="name" type="text" value="<?php
$get_current = mysql_fetch_array(mysql_query("select name from petition where id = '$id'"));
echo('$get_current["name"]');
?>"/><br />
Grade: <input name="grade" type="text" value="<?php
$get_current = mysql_fetch_array(mysql_query("select grade from petition where id = '$id'"));
echo('$get_current["grade"]');
?>"/><br />
Comment:<br />
<input name="comment" type="text" value="<?php
$get_current = mysql_fetch_array(mysql_query("select comment from petition where id = '$id'"));
echo('$get_current["comment"]');
?>"/><br />
<input type="submit" value="Edit Comment!" />
</form>We have three identical queries. Kind of pointless, considering we can do it all from one query. Much better to go something like:
<?PHP
$query = mysql_query("SELECT * FROM petition WHERE id='$id'");
$get_current = mysql_fetch_assoc($query);
?>
<form action="?action=edit" method="post">
Name: <input name="name" type="text" value="<?php echo $get_current["name"]; ?>"/><br />
Grade: <input name="grade" type="text" value="<?php echo $get_current["grade"]; ?>"/><br />
Comment:<br />
<input name="comment" type="text" value="<?php echo $get_current["comment"]; ?>"/><br />
<input type="submit" value="Edit Comment!" />
</form>You don't need quotation marks when echoing variables. You CAN use double quotes ("), however single quotes (') will just echo the variable in a literal sense.
ie.
$var = 'lol';
echo $var; // Will echo 'lol'
echo "$var"; // Will echo 'lol'
echo '$var'; // Will echo '$var'
Going back to your script, this line looks a little weird:
$get = mysql_query("SELECT * FROM table WHERE id='$id' LIMIT 0,1");
Does the table 'table' actually exist, or was it just a dummy text from an example? :P
adamFTW
13-12-2007, 11:49 AM
Okay, thanks. Now, the only problem is the fact it doesn't update the name, grade and comment, nor does it fetch the old name, grade and comment and put them in the text box.
Heres the 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") {
$name = $_POST["name"];
# $field = clean($_POST["field"]);
$grade = $_POST["grade"];
$comment = $_POST["comment"];
$query = mysql_query("SELECT * FROM petition WHERE id='$id'");
$get_current = mysql_fetch_assoc($query);
mysql_query("UPDATE petition SET name='$name', grade='$grade', comment='$comment' WHERE id='$id'") or die(mysql_error());
echo "Record updated!";
exit;
}
?>
<form action="?action=edit" method="post">
Name: <input name="name" type="text" value="<?php echo $get_current["name"]; ?>"/><br />
Grade: <input name="grade" type="text" value="<?php echo $get_current["grade"]; ?>"/><br />
Comment:<br />
<input name="comment" type="text" value="<?php echo $get_current["comment"]; ?>"/><br />
<input type="submit" value="Edit Comment!" />
</form>
adamFTW
13-12-2007, 08:53 PM
Bump. ^^
Edited by Agesilaus (Forum Moderator): Please do not bump threads, wait for a reply instead.
Invent
13-12-2007, 08:59 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?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"]);
$query = mysql_query("SELECT * FROM `petition` WHERE `id` = '".$id."'") or die("Error: ".mysql_error());
$get_current = mysql_fetch_assoc($query) or die("Error: ".mysql_error());
if($_POST["action"] == "edit") {
$name = $_POST["name"];
# $field = clean($_POST["field"]);
$grade = $_POST["grade"];
$comment = $_POST["comment"];
$handle = mysql_query("UPDATE `petition` SET `name`= '".$name."', `grade` = '".$grade."', `comment` = '".$comment."' WHERE `id` = '".$id."'") or die(mysql_error());
echo "Record updated!";
exit;
}
?>
<form action="" method="post">
<input type="hidden" name="action" value="edit" />
Name: <input name="name" type="text" value="<?php echo $get_current["name"]; ?>"/><br />
Grade: <input name="grade" type="text" value="<?php echo $get_current["grade"]; ?>"/><br />
Comment:<br />
<input name="comment" type="text" value="<?php echo $get_current["comment"]; ?>"/><br />
<input type="submit" value="Edit Comment!" />
</form>
</body>
</html>
Want to hide these adverts? Register an account for free!
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.