Jump to content

Multiple Row Update with Checkbox Not Working


ransy23

Recommended Posts

I have three pages, the first selects rows to update, the second allows a user to modify multiple rows, the third applies the changes. When I click submit on the second page, I get the following error: "Incorrect integer value: '' for column 'include' at row 1"

 

Here is the code for page 2:

 

<? php
//start a session
if (!$_POST[cert]) {
     header( "Location: pick_test2.php");
     exit;
} else {
     session_start();
}
if ($_SESSION[valid] != "yes") {
header("Location: login.php");
exit;
}
?>
<!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>

<link rel="stylesheet" type="text/css" href="css/rounded.css" />

</head>
<body>

<?

//Connect to DB 
require_once('external_setup.php');
mysql_select_db($database, $db_connect);


$qry_result = mysql_query("SELECT * FROM fdic WHERE cert = '$_POST[cert]'") or die(mysql_error());

?>
<div id="content-box">
    <div class="padding">
        <div id="element-box" class="login">
            <div class="t">
                <div class="t">
                    <div class="t"></div>
                </div>
            </div>
            <div class="m">
                <h1>SaveNOW Administration- Include or Exclude Bank Branches</h1>
                <table style="font-size:12px" border="0" width="100%" cellpadding="3" cellspacing="0">
                    <tr style="background-color:#00335A; color:#FFFFFF">
                        <td align="center"><strong>Name</strong></td>
                        <td align="center"><strong>Address</strong></td>
                        <td align="center"><strong>City</strong></td>
                        <td align="center"><strong>Included</strong></td>
                    </tr>
                        
<?                

while($row = mysql_fetch_array($qry_result))
{
$userid = $row['id'];
$name = $row['name'];
$address = $row['address'];
$city = $row['city'];
?>

                <form name="form1" method="post" action="do_test2.php">
                <input name="ids[]" type="hidden" id="id" value="<? echo $row['id']; ?>">
                <input name="cert" type="hidden" id="cert" value="<? echo $_POST['cert']; ?>">
                    <tr>
                        <td><? echo $name ?></td>
                        <td align="center"><? echo $address; ?></td>
                        <td align="center"><? echo $city; ?></td>
                        <td align="center"><input name="include<? echo $userid; ?>" type="checkbox" id="include" value="1" 
<? if ($row['include'] ==1) { echo "checked";} else {} ?> ></td>
                    </tr>
<?
}
?>

                    <tr>
                        <td colspan="4" align="center"><input type="submit" name="submit" value="Submit"></td>
                    </tr>
                </table>
                </form>
                <p><a href="savenow_menu.php">Return to Main Menu</a></p>
            </div>
            <div class="b">
                <div class="b">
                    <div class="b"></div>
                </div>
            </div>
        </div>
    </div>
</div>

</body>
</html>

 

Page 3

 

<?php

//start a session
session_start();

//check validity of user
if ($_SESSION[valid] != "yes") {
     header("Location: login.php");
     exit;
}

//Connect to DB 
require_once('external_setup.php');
mysql_select_db($database, $db_connect);


if ($_POST['submit'])
{
foreach ($_POST['ids'] as $id)
{
$id = intval($id);

$include = mysql_real_escape_string($_POST['include'][$id]);
$cert = mysql_real_escape_string($_POST['cert'][$id]);

$qry_result = mysql_query("UPDATE fdic SET include = '$include' WHERE id = '$id'") or die(mysql_error());

}
}
?>

 

Why is this not working?

 

I appreciate the help.

Also, I can make the update work without a checkbox when I change page 2 to the following:

 

<?php

//start a session
if (!$_POST[cert]) {
     header( "Location: pick_test2.php");
     exit;
} else {
     session_start();
}
if ($_SESSION[valid] != "yes") {
header("Location: login.php");
exit;
}

//Connect to DB 
require_once('external_setup.php');
mysql_select_db($database, $db_connect);


$qry_result = mysql_query("SELECT * FROM fdic WHERE cert = '$_POST[cert]'") or die(mysql_error());


echo("
<div id='content-box'>
	<div class='padding'>
		<div id='element-box' class='login'>
			<div class='t'>
				<div class='t'>
					<div class='t'></div>
				</div>
			</div>
			<div class='m'>
				<h1>SaveNOW Administration- Include or Exclude Bank Branches</h1>
				<table style='font-size:12px' border='0' width='100%' cellpadding='3' cellspacing='0'>
					<tr style='background-color:#00335A; color:#FFFFFF'>
						<td align='center'><strong>Name</strong></td>
						<td align='center'><strong>Address</strong></td>
						<td align='center'><strong>City</strong></td>
						<td align='center'><strong>Included</strong></td>
					</tr>
");

while($row = mysql_fetch_array($qry_result))
{
$userid = $row['id'];
$name = $row['name'];
$address = $row['address'];
$city = $row['city'];

echo("
				<form name='form1' method='post' action='do_test2.php'>
				<input name='ids[]' type='hidden' id='id' value='$row[id]'>
				<input name='cert' type='hidden' id='cert' value='$_POST[cert]'>
					<tr>
						<td>$name</td>
						<td align='center'>$address</td>
						<td align='center'>$city</td>
						<td align='center'><input name='include[$userid]' size='2' type='text' id='include' value='$row[include]'></td>
					</tr>
");
}

echo("

					<tr>
						<td colspan='4' align='center'><input type='submit' name='submit' value='Submit'></td>
					</tr>
				</table>
				</form>
                    <p><a href='savenow_menu.php'>Return to Main Menu</a></p>
			</div>
			<div class='b'>
				<div class='b'>
					<div class='b'></div>
				</div>
			</div>
		</div>
	</div>
</div>

");

?>

<!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>

<link rel="stylesheet" type="text/css" href="css/rounded.css" />

</head>
<body>

</body>
</html>

 

But I need a check box and the code seems sloppy. (Though I'm really not sure which is better: using PHP within the HTML body or using HTML within in PHP.)

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.