Jump to content

Post update value: help for small error.


dakke

Recommended Posts

I'm almost there with my update script. Yet I'm overlooking something, most likely due to the fact that I'm very very new to PHP. I can not see where or what, have very much ni clue, so all help is highly appreciated.I posted a request on another forum but no response.Not that the post is actually 'relevant', but simply to avoid cross-posting:http://forums.devnetwork.net/viewtopic.php?f=1&t=80522Now I tried this one:http://www.phpeasystep.com/mysql/9.html. All files are to be found on that page, but for your convenience, I added the files (all the way down). So all credit goes to the url.It works just fine, except that it does not update the table. It says at the end, successful, but checking up on the data shows no change at all. The update.php works fine (I think) showing the data of the record of your choice. The third (when update.php posts to update_ac.php) seems to work, but actually never receives any data from the update.phpI use Taco to 'code' (if you can call my messy method 'coding') and it shows that the

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

in update.php is not highlighted as a form. Deleting the

value="<?php echo $rows['name']; ?>"

does highlight it, so I thought that there might be a possible issue.Or is there also an issue with (in update_ac.php)

 // update data in mysql database$sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";$result=mysql_query($sql); 

To those who are experienced: I'm learning here, but feel free to laugh with such newbie talk. :wink: OverviewIn this tutorial create 3 files1. list_records.php2. update.php3. update_ac.phpStep1. Create table "test_mysql" in database "test"2. Create file list_records.php3. Create file update.php4. Create file update_ac.phpCreating Table and Dump Data:

  CREATE TABLE `test_mysql` (`id` int(4) NOT NULL auto_increment,`name` varchar(65) NOT NULL default '',`lastname` varchar(65) NOT NULL default '',`email` varchar(65) NOT NULL default '',PRIMARY KEY (`id`)) TYPE=MyISAM AUTO_INCREMENT=7 ; -- -- Dumping data for table `test_mysql`--  INSERT INTO `test_mysql` VALUES (1, 'Billly', 'Blueton', 'bb5@phpeasystep.com');INSERT INTO `test_mysql` VALUES (2, 'Jame', 'Campbell', 'jame@somewhere.com');INSERT INTO `test_mysql` VALUES (3, 'Mark', 'Jackson', 'mark@phpeasystep.com');INSERT INTO `test_mysql` VALUES (4, 'Linda', 'Travor', 'lin65@phpeasystep.com');INSERT INTO `test_mysql` VALUES (5, 'Joey', 'Ford', 'fordloi@somewhere.com');INSERT INTO `test_mysql` VALUES (6, 'Sidney', 'Gibson', 'gibson@phpeasystep.com'); 

list_record.php

 <?php$host="localhost"; // Host name$username=""; // Mysql username$password=""; // Mysql password$db_name="test"; // Database name$tbl_name="test_mysql"; // Table name // Connect to server and select database.mysql_connect("$host", "$username", "$password")or die("cannot connect");mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name";$result=mysql_query($sql);?><table width="400" border="0" cellspacing="1" cellpadding="0"><tr><td><table width="400" border="1" cellspacing="0" cellpadding="3"><tr><td colspan="4"><strong>List data from mysql </strong> </td></tr> <tr><td align="center"><strong>Name</strong></td><td align="center"><strong>Lastname</strong></td><td align="center"><strong>Email</strong></td><td align="center"><strong>Update</strong></td></tr><?phpwhile($rows=mysql_fetch_array($result)){?><tr><td><? echo $rows['name']; ?></td><td><? echo $rows['lastname']; ?></td><td><? echo $rows['email']; ?></td> // link to update.php and send value of id<td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td></tr><?php}?></table></td></tr></table><?phpmysql_close();?>  

update.php

 <?php$host="localhost"; // Host name$username=""; // Mysql username$password=""; // Mysql password$db_name="test"; // Database name$tbl_name="test_mysql"; // Table name // Connect to server and select database.mysql_connect("$host", "$username", "$password")or die("cannot connect");mysql_select_db("$db_name")or die("cannot select DB"); // get value of id that sent from address bar$id=$_GET['id'];  // Retrieve data from database$sql="SELECT * FROM $tbl_name WHERE id='$id'";$result=mysql_query($sql); $rows=mysql_fetch_array($result);?><table width="400" border="0" cellspacing="1" cellpadding="0"><tr><form name="form1" method="post" action="update_ac.php"><td><table width="100%" border="0" cellspacing="1" cellpadding="0"><tr><td> </td><td colspan="3"><strong>Update data in mysql</strong> </td></tr><tr><td align="center"> </td><td align="center"> </td><td align="center"> </td><td align="center"> </td></tr><tr><td align="center"> </td><td align="center"><strong>Name</strong></td><td align="center"><strong>Lastname</strong></td><td align="center"><strong>Email</strong></td></tr><tr><td> </td><td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td><td align="center"><input name="lastname" type="text" id="lastname" value="<? echo $rows['lastname']; ?>" size="15"></td><td><input name="email" type="text" id="email" value="<? echo $rows['email']; ?>" size="15"></td></tr><tr><td> </td><td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td><td align="center"><input type="submit" name="Submit" value="Submit"></td><td> </td></tr></table></td></form></tr></table> <? // close connectionmysql_close(); ?> 

update_ac.php

 <?php$host="localhost"; // Host name$username=""; // Mysql username$password=""; // Mysql password$db_name="test"; // Database name$tbl_name="test_mysql"; // Table name // Connect to server and select database.mysql_connect("$host", "$username", "$password")or die("cannot connect");mysql_select_db("$db_name")or die("cannot select DB"); // update data in mysql database$sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";$result=mysql_query($sql); // if successfully updated.if($result){echo "Successful";echo "<BR>";echo "<a href='list_records.php'>View result</a>";} else {echo "ERROR";} ?> 

Link to comment
Share on other sites

Do you have register_globals enabled? If not, you will have to refer to the values from your form as $_POST['name'], $_POST['lastname'], etc. (this is in update_ac.php)By the way, having register_globals enabled is very bad for security.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...