Hi All,

I have a problem in getting this update form to work. The query to call the record works fine and when clicking submit it redirects correctly and echo's (record updated) but the database is not updated. When i change the value variables in the update script it correctlu updates the record in the database.

Please check what I am doing wrong.

<?php
include("include/session.php");

$id="1";

$username="root";
$password="";
$database="cmms";
mysql_connect(localhost,$username,$password);

$query=" SELECT * FROM menuone WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i < $num) {

$item = mysql_result($result,$i,"item");
$level1 =  mysql_result($result,$i,"level1");
$level2 =  mysql_result($result,$i,"level2");
$level3 =  mysql_result($result,$i,"level3");
$level4 =  mysql_result($result,$i,"level4");
$level5 =  mysql_result($result,$i,"level5");
$level6 =  mysql_result($result,$i,"level6");
$level7 =  mysql_result($result,$i,"level7");
$level8 =  mysql_result($result,$i,"level8");
$level9 =  mysql_result($result,$i,"level9");	 


?>
<form action="up.php" method="post">
Id <input type="text" name="u_id" value="<?php echo $id; ?>">
Item <input type="text" name="u_id" value="<?php echo $item; ?>">
Level1 <input type="text" name="u_level1" value="<?php echo $level1; ?>"><br>
Level2 <input type="text" name="u_level2" value="<?php echo $level2; ?>"><br>
Level3 <input type="text" name="u_level3" value="<?php echo $level3; ?>"><br>
Level4 <input type="text" name="u_level4" value="<?php echo $level4; ?>"><br>
Level5 <input type="text" name="u_level5" value="<?php echo $level5; ?>"><br>
Level6 <input type="text" name="u_level6" value="<?php echo $level6; ?>"><br>
Level7 <input type="text" name="u_level7" value="<?php echo $level7; ?>"><br>
Level8 <input type="text" name="u_level8" value="<?php echo $level8; ?>"><br>
Level9 <input type="text" name="u_level9" value="<?php echo $level9; ?>"><br>
<input type="Submit" value="Update">
</form>

<?php
$p_id = $_POST['$u_id'];
$p_item = $_POST['$u_item'];
$p_level1 = $_POST['$u_level1'];
$p_level2 = $_POST['$u_level2'];
$p_level3 = $_POST['$u_level3'];
$p_level4 = $_POST['$u_level4'];
$p_level5 = $_POST['$u_level5'];
$p_level6 = $_POST['$u_level6'];
$p_level7 = $_POST['$u_level7'];
$p_level8 = $_POST['$u_level8'];
$p_level9 = $_POST['$u_level9'];
++$i;
}
?>

Here is the update script.

up.php

<?php

$username="root";
$password="";
$database="cmms";
mysql_connect(localhost,$username,$password);


$p_id = $_POST['$u_id'];
$p_item = $_POST['$u_item'];
$p_level1 = $_POST['$u_level1'];
$p_level2 = $_POST['$u_level2'];
$p_level3 = $_POST['$u_level3'];
$p_level4 = $_POST['$u_level4'];
$p_level5 = $_POST['$u_level5'];
$p_level6 = $_POST['$u_level6'];
$p_level7 = $_POST['$u_level7'];
$p_level8 = $_POST['$u_level8'];
$p_level9 = $_POST['$u_level9'];


$query="UPDATE `cmms`.`menuone` SET `level9` = '$p_level9',
`level8` = '$p_level8',
`level7` = '$p_level7',
`level6` = '$p_level6',
`level5` = '$p_level5',
`level4` = '$p_level4',
`level3` = '$p_level3',
`level2` = '$p_level2',
`level1` = '$p_level1' WHERE `menuone`.`id` =$p_id;";


mysql_query($query);
echo "Record Updated";
mysql_close();


?>

Thank you

Max

First off, is the form being populated?

Yes, if I execute p.php it brings up the form populated.

Max

Ok, well in your up.php you have variables in your post

$p_level9 = $_POST['$u_level9'];

Should be

$p_level9 = $_POST['u_level9'];

Thank you, Fobos

It is submiting correctly. What needs to change when using checkboxes instead of text boxes to insert 1 or 0 as ticked or unticked.

Regards,

Max

try using the if else statement.
ex.
if level = 0 use unchecked box else use checked box

Hi All,

I got the update to work for text fields, but when changing them to checkbox fields the update works but the values are 0 for checked and null for unchecked.

Hoe can I change the values to 1 for checked and 0 for unchecked.

Here is the code that I am currently using.


Thank you

Max

$query=" SELECT * FROM menu_1 WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);

while($row = mysql_fetch_array($result))
  {

$item = $row['item'];
$level1 = $row['level1'];
$level2 =  $row['level2'];
$level3 =  $row['level3'];
$level4 =  $row['level4'];
$level5 =  $row['level5'];
$level6 =  $row['level6'];
$level7 =  $row['level7'];
$level8 =  $row['level8'];
$level9 =  $row['level9'];	

  
echo"<table style='font-size:12px' align='center' border='1' cellspacing='0' cellpadding='5'>";
echo "<tr><td>ID</td><td>ITEM</td><td>Level 1</td><td>Level 2</td><td>Level 3</td><td>Level 4</td><td>Level 5</td><td>Level 6</td><td>Level 7</td><td>Level 8</td><td>Level 9</td></tr>";


echo"<tr align='center'>";	

	echo"<td><input size='10' type='text' name='u_id' value='$id'></td>";
	echo"<td><input size='10' type='text' name='u_item' value='$item'></td>";

	if($level1=="1") {
		echo "<td><input type='checkbox' name='u_level1' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level1' value='0' /></td>\n";    
	}
	
	if($level2=="1") {
		echo "<td><input type='checkbox' name='u_level2' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level2' value='0' /></td>\n";    
	}
	
	if($level3=="1") {
		echo "<td><input type='checkbox' name='u_level3' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level3' value='0' /></td>\n";    
	}
	
	if($level4=="1") {
		echo "<td><input type='checkbox' name='u_level4' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level4' value='0' /></td>\n";    
	}
	
	if($level5=="1") {
		echo "<td><input type='checkbox' name='u_level5' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level5' value='0' /></td>\n";    
	}
	
	if($level6=="1") {
		echo "<td><input type='checkbox' name='u_level6' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level6' value='0' /></td>\n";    
	}
	
	if($level7=="1") {
		echo "<td><input type='checkbox' name='u_level7' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level7' value='0' /></td>\n";    
	}
	
	if($level8=="1") {
		echo "<td><input type='checkbox' name='u_level8' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level8' value='0' /></td>\n";    
	}
	
	if($level9=="1") {
		echo "<td><input type='checkbox' name='u_level9' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level9' value='0' /></td>\n";    
	}
echo "</tr>";	
}

echo "</table>";





$p_item=$_POST['item'];
$p_level9=$_POST{'u_level9'};
$p_level8=$_POST{'u_level8'};
$p_level7=$_POST['u_level7'];
$p_level6=$_POST['u_level6'];
$p_level5=$_POST['u_level5'];
$p_level4=$_POST['u_level4'];
$p_level3=$_POST['u_level3'];
$p_level2=$_POST['u_level2'];
$p_level1=$_POST['u_level1'];
if(isset($_POST['submit'])) {