eMonk Posted April 24, 2011 Share Posted April 24, 2011 <?php $host="xxxx"; // Host name $username="xxxx"; // Mysql username $password="xxxx"; // Mysql password $db_name="xxxx"; // Database name $tbl_name="xxxx"; // 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 $model_id=$_GET['model_id']; // Retrieve data from database $sql="SELECT model_id, model_name, location, email_1 FROM $tbl_name WHERE model_id='$model_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="updated.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>Location</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['model_name']; ?>"></td> <td align="center"><input name="location" type="text" id="location" value="<? echo $rows['location']; ?>"></td> <td><input name="email" type="text" id="email" value="<? echo $rows['email_1']; ?>"></td> </tr> <tr> <td> </td> <td><input name="id" type="hidden" id="id" value="<? echo $rows['model_id']; ?>"></td> <td align="center"><input type="submit" name="Submit" value="Submit"></td> <td> </td> </tr> </table> </td> </form> </tr> </table> <? // close connection mysql_close(); ?> This is the part that isn't working <td align="center"><input name="name" type="text" id="name" value="<? echo $rows['model_name']; ?>"></td> <td align="center"><input name="location" type="text" id="location" value="<? echo $rows['location']; ?>"></td> <td><input name="email" type="text" id="email" value="<? echo $rows['email_1']; ?>"></td> The echo values aren't being fetched and left blank when I run the script. Any ideas? Link to comment https://forums.phpfreaks.com/topic/234605-updating-database-via-form/ Share on other sites More sharing options...
Pikachu2000 Posted April 24, 2011 Share Posted April 24, 2011 More likely to be a PHP question. Moving to PHP Coding Help. Link to comment https://forums.phpfreaks.com/topic/234605-updating-database-via-form/#findComment-1205654 Share on other sites More sharing options...
herghost Posted April 24, 2011 Share Posted April 24, 2011 Change all of your <? to <?php Link to comment https://forums.phpfreaks.com/topic/234605-updating-database-via-form/#findComment-1205677 Share on other sites More sharing options...
eMonk Posted April 24, 2011 Author Share Posted April 24, 2011 Done but the problem still remains (echo values aren't being displayed). Link to comment https://forums.phpfreaks.com/topic/234605-updating-database-via-form/#findComment-1205679 Share on other sites More sharing options...
Pikachu2000 Posted April 24, 2011 Share Posted April 24, 2011 You have no logic in there to see if the query succeeded or not. $sql="SELECT model_id, model_name, location, email_1 FROM $tbl_name WHERE model_id='$model_id'"; if( !$result=mysql_query($sql) ) { echo "<br>Query $sql<br>Failed with error: " . mysql_error() . '<br>'; } $rows=mysql_fetch_array($result); // etc . . . Link to comment https://forums.phpfreaks.com/topic/234605-updating-database-via-form/#findComment-1205680 Share on other sites More sharing options...
eMonk Posted April 24, 2011 Author Share Posted April 24, 2011 No errors returned. I'm stumped man. Been searching this site and google for clues for the past few hours. I can't believe how difficult this is just to echo mysql results in a table. Link to comment https://forums.phpfreaks.com/topic/234605-updating-database-via-form/#findComment-1205682 Share on other sites More sharing options...
eMonk Posted April 24, 2011 Author Share Posted April 24, 2011 Here's list.php that works. The code listed above is update.php. <?php $host="xxxxx"; // Host name $username="xxxxx"; // Mysql username $password="xxxxx"; // Mysql password $db_name="xxxxx"; // Database name $tbl_name="model"; // 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 model_id, model_name, location, email_1 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>Location</strong></td> <td align="center"><strong>Email</strong></td> <td align="center"><strong>Update</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td><?php echo $rows['model_name']; ?></td> <td><?php echo $rows['location']; ?></td> <td><?php echo $rows['email_1']; ?></td> // link to update.php and send value of id <td align="center"><a href="update.php?id=<? echo $rows['model_id']; ?>">update</a></td> </tr> <?php } ?> </table> </td> </tr> </table> <?php mysql_close(); ?> Link to comment https://forums.phpfreaks.com/topic/234605-updating-database-via-form/#findComment-1205683 Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2011 Share Posted April 25, 2011 Echo $sql and make sure the variables have the proper values. If there was no error, your most likely getting an empty result set returned as a result of the $model_id variable being an empty string. Link to comment https://forums.phpfreaks.com/topic/234605-updating-database-via-form/#findComment-1205743 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.