Jump to content

PHP Update Form


bobbyD

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/286819-php-update-form/
Share on other sites

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'.

Link to comment
https://forums.phpfreaks.com/topic/286819-php-update-form/#findComment-1471876
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.