Jump to content

Updating from a PHP generated form


ironside82

Recommended Posts

Hello,

 

I've hit a stumbling block with the following code, It is generating a form from details held in a database so the user can update each record in one hit, Normally this would be easy but because I cannot have fixed form names I have included the mysql update command inside the loop as you will see.

 

My thinking behind this is each time it loops the variables will change depending on the latest bit of information pulled from the database therefore the update must be performed on each pass in order to update correctly

 

Is this the way to do it or am I getting it wrong.

 

here is the snippet

<?
		$result2 = mysql_query("SELECT * FROM bags");
		while($r2=mysql_fetch_array($result2))

      {

		$bag_id=$r2["id"];
		$bag_model=$r2["model_number"];
		$bag_name=$r2["bag_name"];

		$result3 = mysql_query("SELECT * FROM compat WHERE cam = '$camera_id_1' AND bag = '$bag_id'");
			while($r3=mysql_fetch_array($result3))

       {
			  
			$compat_id=$r3["id"];
			$compat_check=$r3["ok"];

			if ($compat_check == "1") { $check = "selected"; } else { $check = ""; }

?>
                    <tr>
                    <td><? echo $bag_model; ?> <input name="compat_id_2" type="hidden" value="<? echo $compat_id; ?>"/></td>
                    <td><? echo $bag_name; ?><input name="camera_id_1" type="hidden" value="<? echo $camera_id; ?>"/></td>
                    <td>
                        <label>
                      <select name="check" id="check">
                        <option value="0" <? echo $check; ?>>not compatible</option>
                        <option value="1" <? echo $check; ?>>compatible</option>
                      </select>
                        </label>
                    </td>
                    </tr>
<?
                  
	$checkbox=$_POST["check"];
	$compat_id_2=$_POST["compat_id_2"];	  
	mysql_query("UPDATE compat SET ok='$checkbox' WHERE id='$compat_id_2'");

      }
			   
      }

?>

 

Thanks for you help

 

James

Link to comment
https://forums.phpfreaks.com/topic/113321-updating-from-a-php-generated-form/
Share on other sites

lets try tidy this up abit.

 

change this:

while($r2=mysql_fetch_array($result2))

to this:

$r2 = mysql_fetch_array($result2);

and remove the curly braces for the while.

 

---

 

change this:

if ($compat_check == "1") { $check = "selected"; } else { $check = ""; }

to this:

if ($compat_check == "1") { $check = "selected"; }

don't need the else there.

 

---

 

change this:

		$checkbox=$_POST["check"];
	$compat_id_2=$_POST["compat_id_2"];	  

to this:

if(isset($_POST["check"])) { $checkbox = $_POST["check"]; }
if(isset($_POST["compat_id_2"])) { $compat_id_2 = $_POST["compat_id_2"]; }

 

Regards ACE

 

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.