Jump to content

Form arrays, and updating them into multiple rows in MySQL


warptwist

Recommended Posts

I'm having trouble getting the first part of my coding to work properly.

 

At the moment my coding is like this:

 

administrators overview:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?
session_start();
if(!session_is_registered(myusername)){
header("location:404.php");
}
$title = $_GET['title'];
echo "
	<html>
		<head>
			<title>
				".$title."
			</title>
			<link href='content.css' rel='stylesheet' type='text/css' />
		</head>
		<body>
			<table width='93%' border='0' align='center' cellpadding='0' cellspacing='0'>
				<tr>
					<td colspan='5' height='55' background='img/mainbox_red.png' id='title'>".$title."</td>
				</tr>
				<tr>
					<td height='10' colspan='5'> </td>
				</tr>
				<tr bgcolor='cc0000'>
					<th id='header' width='290'><center><FONT COLOR='#f6f6f6'>Holdnavn</font></center></th>
					<th id='header' width='399'><center><FONT COLOR='#f6f6f6'>Kontaktperson</font></center></th>
					<th id='header' width='178'><center><FONT COLOR='#f6f6f6'>Telefon</font></center></th>
					<th id='header' width='357'><center><FONT COLOR='#f6f6f6'>E-mail</font></center></th>
					<th id='header' width='91'><center><FONT COLOR='#f6f6f6'>Betalt</font></center></th>                  
				</tr>
				<form action='adminupdate.php' method='POST'>
";

include 'db_config.php';
include 'db_open.php';

$result = mysql_query("SELECT * FROM aabsupportbrugere");
$color="1";

while($row = mysql_fetch_array($result)){
$betalt = "";
if ($row[betalt] == 1){
$betalt = "'checked'";
$value = "value='1'";
}
else{
$betalt = "";
$value = "value='0'";
}

if($color==1){

echo "
				<tr bgcolor='#CCCCCC'>
					<td>
						<center>
							" . $row['holdnavn'] . "
						</center>								
					</td>
					<td>
						<center>
							" . $row['kontakt'] . "
						</center>								
					</td>
					<td>
						<center>
							" . $row['tlf'] . "
						</center>								
					</td>
					<td>
						<center>
							" . $row['mail'] . "
						</center>								
					</td>
					<td>
						<center>
							<input type='checkbox' name='betalt[]' ".$value." ".$betalt.">
							<br />
							<input type='hidden' name='id[]' value='". $row['id'] ."'>
						</center>								
					</td>
				</tr>
";

$color="2";}

else {

echo "
				<tr bgcolor='#F7F7F7'>
					<td>
						<center>
							" . $row['holdnavn'] . "
						</center>								
					</td>
					<td>
						<center>
							" . $row['kontakt'] . "
						</center>								
					</td>
					<td>
						<center>
							" . $row['tlf'] . "
						</center>								
					</td>
					<td>
						<center>
							" . $row['mail'] . "
						</center>								
					</td>
					<td>
						<center>
							<input type='checkbox' name='betalt[]' ".$value." ".$betalt.">
							<br />
							<input type='hidden' name='id[]' value='". $row['id'] ."'>
						</center>								
					</td>
				</tr>
";

$color="1";}

}

echo "
				<tr id='clear'>
					<td>
						 
					</td>
					<td>
						<input type='submit' value='Godkend'>
						</form>
					</td>
					<td>
						<a href='logout.php' target='_self'>
						Log ud
						</a>
					</td>
					<td>
						<a href='ny.php' target='_self'>
						Tilføj bruger
						</a>
					</td>
					<td>
						 
					</td>
				</tr>
			</table>
		</body>
	</html>
";
?>

 

The page updating my DB and echoing results (needs styling though  ;) ):

<?
include 'db_config.php';
include 'db_open.php';

$number=count($_POST['id']);
$i = 0;

while($i < $number){
	$update="UPDATE `aabsupportbrugere` SET `betalt` = '" . mysql_real_escape_string($_POST['betalt'][$i] . " WHERE `aabsupportbrugere`.`id` = " . $_POST['id'][$i] . "";
	mysql_query($update);
	echo "id:   ". $_POST['id'][$i]."  -  ";
	echo "betalt:   ". $_POST['betalt'][$i]."<br />";
	$i++;
}
?>

 

The form shows up nicely, with the correct checkboxes checked, but when I submit, the updating page just turns up blank.

 

I know for a fact, that it is the

$update="UPDATE `aabsupportbrugere` SET `betalt` = '" . mysql_real_escape_string($_POST['betalt'][$i] . " WHERE `aabsupportbrugere`.`id` = " . $_POST['id'][$i] . "";
mysql_query($update);

that has errors.

 

When i remove the update coding, and echo the result to the page, it shows up exactly as it should.

I'm not that into PHP to find a solution, so any help would be appreciated  :)

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.