Jump to content

Form for update of MySQL Data


sunziun

Recommended Posts

I am trying to make tutorial work:1337 Labs PHP form TutorialThe "select.php" I get only one line of Data, which looks like this:- Edit

<?php//SOURCE// http://www.tutorialized.com/view/tutorial/Editing-data-in-MySQL-database-table-over-form/64242// SELECT PHP//include configinclude("config.php");$query	  = "SELECT * FROM $table";$result	 = mysql_query ($query,$con);// Check resultif(!$result){   echo "Unable to select table";} else {while ($row  = mysql_fetch_array($result));{echo $row['Name'] . "-" . $row['Date']; ?> <a href="edit.php?id=<?php echo $row['id']; ?>">Edit</a> <br /><?php}} ?>

EDIT.PHP looks like this:

<?php//SOURCE// http://www.tutorialized.com/view/tutorial/Editing-data-in-MySQL-database-table-over-form/64242//include("config.php");$host	= "127.0.0.1";$user	= "root";$pass	= "";$db	  = "learn_sql";$table   = "customers";$con	 = mysql_connect($host, $user, $pass) or die(mysql_error());mysql_select_db("learn_sql", $con) or die("DB not selectable!");//Query$query   = "SELECT * FROM $table";$result  = mysql_query($query, $con);// Checkif(!$result){   echo "Unable to select data from Table";} else {   while($row = mysql_fetch_array($result));}?> <form action="update.php?id=<?php echo $id; ?>" method="post"> <p>ID<br /> <input readonly="readonly" type="text" value="<?php echo $row['$id']; ?>" /></p> <p>Name<br /> <input type="text" id="name" value="<?php echo $row['Name']; ?>" /></p> <p>Date<br /> <input tyep="text" id="date" value="<?php echo $row['Date']; ?>" /></p> <input type="submit" name="update" id="update" value="Update" /> </form>

and the UPDATE.PHP

<?php//SOURCE// http://www.tutorialized.com/view/tutorial/Editing-data-in-MySQL-database-table-over-form/64242// if form button has been pressed then do the followingif(isset($_POST['update'])){// Get id of post$id	  = $_GET['id'];$name	= $_POST['name'];$date	= $_POST['date'];// include config fileinclude("config.php");// update db table$query	  = "UPDATE $table SET Name = $name, Date = $date WHERE ID = $id ";$result	 = mysql_query($query);if($result){   echo "Successfully edited entry";} else {   echo "Failed to edit table";}} ?>

Link to comment
Share on other sites

so whats problem you are facing?

Link to comment
Share on other sites

i am assuming that your table is not empty.

while($row = mysql_fetch_array($result));

here it loops through untill it evaluate false..so at the end of it it will probably evaluating FALSE so in your form the $row data is not showing you up.you may do some var_dump($row) to ensure what the data are comingand this will get all the data from table..probably you want any particular row..so you may want to add a where clause

$query   = "SELECT * FROM $table";

i guess you can ommit while here if your db result is one row..(i guess it should be one row)..

$row = mysql_fetch_array($result)

genarly i knew while loops to use with braces

Link to comment
Share on other sites

Now next issue.

<?php$host	   = "127.0.0.1";$user	   = "root";$pass	   = "";$db		 = "learn_sql";$table	  = "customers";//$name = $_POST['Name'];//$date = $_POST['Date'];// Connect MySQL   $con = mysql_connect($host, $user, $pass) or die("Unable to connect");   $select_db = mysql_select_db($db) or die("Unable to select DB");// Perform the update   $sql = "update $table set name='$name', date='$date' where id='$id'";	  $query = mysql_query($sql) or die("Unable to update");		 echo "Updated" . " " . "<br />" . "<a href='select_to_update.php'>View records</a>";?>

THE FORM

<?php$host	   = "127.0.0.1";$user	   = "root";$pass	   = "";$db		 = "learn_sql";$table	  = "customers";// Connect MySQL mysql_connect($host, $user, $pass) or die("Unable to connect"); mysql_select_db($db) or die("Unable to select DB"); // get id from address bar   $id = $_GET['id'];// Query   $sql = "SELECT * FROM $table";   $result = mysql_query($sql) or die("Unable to select table");// Retrieve data from DB using ID   $sql = "SELECT * FROM $table WHERE id = '$id'";   $result = mysql_query($sql) or die("Unable to select table");   $rows = mysql_fetch_array($result);?><form method="post" action="update_account.php" name="form1"><p>Name<br />   <input type="text" name="name" id="name" value="<? echo $rows['Name']; ?>" /></p><p>Date<br />   <input type="text" name="date" id="date" value="<? echo $rows['Date']; ?>" /></p><p><input type="text" readonly="readonly" name="id" id="id" value="<? echo $rows['ID']; ?>"<input type="submit" name="submit" /></form><? mysql_close(); ?>

Output updated, but no actual update at DB.Any ideas?

Link to comment
Share on other sites

<input type="text" name="name" id="name" value="<? echo $rows['Name']; ?>

$name = $_POST['Name'];

name case are not same here Name and name..

//$name = $_POST['Name'];//$date = $_POST['Date'];

i belive its uncommented in your actual codeyou should check the arrays that what are coming from pages using var_dump()

echo "Updated" . " " . "<br />" . "<a href='select_to_update.php'>View records</a>";

it will always show you updated you need to check it is realy updating or not by checking the return value of mysql_query or more acurately by mysql_affetced_rows

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...