bobbyD Posted March 8, 2014 Share Posted March 8, 2014 (edited) Hi! I'm trying to create a form that recalls rows from a table and then updates a column in those rows. I have been able to successfully recall the information and tie it to the new form, but I did something wrong because the UPDATE does not write back to the database. My code looks like : <?php // connect to the database and select the correct database mysql_connect("localhost","","!"); mysql_select_db("") or die("Unable to select database"); if ( isset( $_POST['approve'])) { echo "<pre>"; print_r($_POST); print_r($update); echo "</pre>"; foreach($_POST['Index'] AS $id) { $approve = $_POST['Approval']; $update = "UPDATE TimeOff SET Approval='" . $approve . "' WHERE Index='" . $id . "'"; mysql_query($update) or die (mysql_error("Not Updated")); } } $sql = "SELECT * FROM TimeOff WHERE Approval=''"; $res = mysql_query($sql); if (mysql_num_rows ($res) > 0) { echo "<form action ='test.php' method='post'>"; while ($row = mysql_fetch_assoc($res)) { echo "ID: " . $row['Index'] . " <br>\n"; echo "Name: " . $row['Name'] . " <br>\n"; echo "sDate: " . $row['StartDate'] . " <br>\n"; echo "eDate: " . $row['EndDate'] . " <br>\n"; echo "Reason: " . $row['Reason'] . " <br>\n"; echo "Approval: <select name='approval[".$row['Index']."]' value='" .$row['Approval'] . "'><option value=''></option><option value='Yes'>Yes</option><option value='No'>No</option></select><br>\n"; echo "<input type=hidden name=hidden value=".$row['Index'].">"; echo "<hr>\n"; } echo "<input type='submit' id='approve' name='approve' value ='Approve'>"; echo "</form>"; } ?> My code may be messy (I'm teaching myself and am only on day 3). Any help/suggestions are greatly appreciated! Edited March 8, 2014 by bobbyD Quote Link to comment https://forums.phpfreaks.com/topic/286819-php-update-form/ Share on other sites More sharing options...
mac_gyver Posted March 9, 2014 Share Posted March 9, 2014 when you tried this, your debugging print_r($_POST); statement should have given you a very good clue about what your code needs to loop over - Array ( [approval] => Array ( [1] => Yes [3] => No [4] => Yes [6] => ) ... $_POST['approval'] is an array, where the index is your database table's 'Index' and the value is the Yes, No, or empty value that was selected for each 'Index'. Quote Link to comment https://forums.phpfreaks.com/topic/286819-php-update-form/#findComment-1471876 Share on other sites More sharing options...
bobbyD Posted March 9, 2014 Author Share Posted March 9, 2014 Thank You for the response mac_gyver. I think maybe I'm still too new to PHP to be trying this out because your response (as clear as it is) is Greek to me! I may have to wait to get back to work and ask for help. Quote Link to comment https://forums.phpfreaks.com/topic/286819-php-update-form/#findComment-1471889 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.