silverglade Posted September 14, 2009 Share Posted September 14, 2009 hi, i cant get my html table to display the new contents of my database table when i update it. i dont think my script is updating the database table either. any help or advice greatly appreciated. thank you. derek here is the db connection script <?php //////////////////////////////////////// /// connect to database //////////////////////////////////////// /// include file $host = "localhost"; $database = "php_lessons"; $username = "root"; $password = ""; mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error()); mysql_select_db($database); ?> here are the entire contents of the php script. at the top it inserts data into the table, or is supposed to, below that it is supposed to output the new contents of the table., and below that is the form i use to update the contents of the table. <?php include("connections.php"); //////////////////////////////////////// //////////////////////////////////////// /// query db and loop through rows example $field2 = $_POST['data2']; $field3 = $_POST['data3']; $field4 = $_POST['data4']; $field5 = $_POST['data5']; $field6 = $_POST['data6']; if($field2 && $field3 && $field4){ mysql_query("INSERT INTO table1 (field2,field3,field4,field5,field6)VALUES('$field2','$field3','$field4','$field5','$field6')"); }else{ } //////////////////////////////////////////////////////////output data into a table/////////////////////////////// $query = mysql_query("SELECT * FROM table1"); $table = "<table width=\"40%\" border=\"1\"> <tr> <td>heading1</td> <td>heading2</td> <td>heading3</td> <td>heading4</td> <td>heading5</td> <td>heading6</td> </tr>"; while($row = mysql_fetch_array($query)){ $table .= " <tr> <td>".$row['record_id']."</td> <td>".$row['field2']."</td> <td>".$row['field3']."</td> <td>".$row['field4']."</td> <td>".$row['field5']."</td> <td>".$row['field6']."</td> </tr>"; } $table .= "</table>"; echo $table; ?> <!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=iso-8859-1" /> <title>Insert Statement</title> </head> <body> <form id="form1" name="form1" method="post" action="lesson8_test.php"> <table width="31%" border="0"> <tr> <td width="32%">Data2</td> <td width="68%"><input name="data2" type="text" id="data2" /></td> </tr> <tr> <td>Data3</td> <td><input name="data3" type="text" id="data3" /></td> </tr> <tr> <td>Data4</td> <td><input name="data4" type="text" id="data4" /></td> </tr> <tr> <td>Data5</td> <td><input name="data5" type="text" id="data5" /></td> </tr> <tr> <td>Data6</td> <td><input name="data6" type="text" id="data6" /></td> </tr> <tr> <td> </td> <td><input type="submit" name="Submit" value="Submit" /></td> </tr> </table> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/174251-solved-updating-and-displaying-the-contents-of-db-table-not-updating-to-html-table/ Share on other sites More sharing options...
silverglade Posted September 14, 2009 Author Share Posted September 14, 2009 nevermind i fixed it by changing the action name to lesson8test.php, not sure why that worked. but it did. here is the working code. <?php include("connections.php"); //////////////////////////////////////// //////////////////////////////////////// /// query db and loop through rows example $field2 = $_POST['data2']; $field3 = $_POST['data3']; $field4 = $_POST['data4']; $field5 = $_POST['data5']; $field6 = $_POST['data6']; if($field2 && $field3 && $field4){ mysql_query("INSERT INTO table1 (field2,field3,field4,field5,field6)VALUES('$field2','$field3','$field4','$field5','$field6')"); }else{ echo "error"; } //////////////////////////////////////////////////////////output data into a table/////////////////////////////// $query = mysql_query("SELECT * FROM table1"); $table = "<table width=\"40%\" border=\"1\"> <tr> <td>heading1</td> <td>heading2</td> <td>heading3</td> <td>heading4</td> <td>heading5</td> <td>heading6</td> </tr>"; while($row = mysql_fetch_array($query)){ $table .= " <tr> <td>".$row['record_id']."</td> <td>".$row['field2']."</td> <td>".$row['field3']."</td> <td>".$row['field4']."</td> <td>".$row['field5']."</td> <td>".$row['field6']."</td> </tr>"; } $table .= "</table>"; echo $table; ?> <!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=iso-8859-1" /> <title>Insert Statement</title> </head> <body> <form id="form1" name="form1" method="post" action="lesson8test.php"> <table width="31%" border="0"> <tr> <td width="32%">Data2</td> <td width="68%"><input name="data2" type="text" id="data2" /></td> </tr> <tr> <td>Data3</td> <td><input name="data3" type="text" id="data3" /></td> </tr> <tr> <td>Data4</td> <td><input name="data4" type="text" id="data4" /></td> </tr> <tr> <td>Data5</td> <td><input name="data5" type="text" id="data5" /></td> </tr> <tr> <td>Data6</td> <td><input name="data6" type="text" id="data6" /></td> </tr> <tr> <td> </td> <td><input type="submit" name="Submit" value="Submit" /></td> </tr> </table> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/174251-solved-updating-and-displaying-the-contents-of-db-table-not-updating-to-html-table/#findComment-918601 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.