I am trying to have a list of registered people show from my SQL db.
I have a table that each column outputs into but where I am running into a problem is with the checkbox that will be updated to show whether the person has paid or not. I will be checking the box not the customer.
So I have run into 2 issues so far:
1) When I submit the update to save the checked box into it's column in the SQL db, it does not show the box being checked unless I refresh the page where it then goes and gets the updated (newly stored) value of checked.
2) When I am trying to uncheck the box and try to save I get an error that says my paymentStatus array is and undefined index since there is no value assigned to it.
I have been going around in circles on this and I can't imagine it's really this hard so I am thinking there must be a better way of accomplishing what I am trying to do.
Here is my current code: http://pastebin.com/bgAtYqbL
Thanks for any help.
Full code:
<?php
require_once ('functions.php');
require_once ('includes/cred.php');
$query = $conn->prepare("SELECT * FROM sponsors");
//$query->bindParam(":idnum", "1");
$query->execute();
$notPaid = $query->fetchAll();
if (isset($_REQUEST["sponsorUpdate"])) {
//Create Sessions
$array = $_POST;
foreach ($array as $key => $value) {
$_SESSION[$key] = $value;
}
if(isset($_POST['paymentStatus'])) {
$payment = $_POST['paymentStatus'];
foreach($payment as $value) {
$stmt = $conn->prepare("UPDATE sponsors SET paid=1 WHERE id = $value");
$stmt->execute();
}
} else {
$payment = $_POST['id'];
foreach($payment as $value) {
$stmt = $conn->prepare("UPDATE sponsors SET paid=0 WHERE id = $value");
$stmt->execute();
}
}
}
?>
<html>
<head>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<table border="1" cellspacing="0" cellpadding="10">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Company Name</th>
<th>Email Address</th>
<th>Phone Number</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zipcode</th>
<th>Sponsor Level</th>
<th>Payment Received</th>
</tr>
<?php
foreach ($notPaid as $row) {
$phone = preg_replace('/(\d{3})(\d{3})(\d{4})/', '($1) $2-$3', $row['phone']);
?>
<tr>
<td><?php echo $row['fname']; ?></td>
<td><?php echo $row['lname']; ?></td>
<td><?php echo $row['cname']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $phone; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['city']; ?></td>
<td><?php echo $row['state']; ?></td>
<td><?php echo $row['zip']; ?></td>
<td><?php echo $row['sponsorLevel']; ?></td>
<td><input name="paymentStatus[]" type="checkbox" value="<?php echo $row['id']; ?>" <?php if ($row['paid'] == 1) echo 'checked="checked"'; ?></td>
</tr>
<?php
} ?>
</table>
<input type="submit" name="sponsorUpdate" value="Save Changes" />
</form>
</body>
</html>