As Mittineague pointed out, the code we’ve been looking at returns an array of guests for the event called, so I can assume each individual guest status will be shown as we’ve defined basically from an “Admin” point of view. As far as any one guest, I would assume you would make a different interface just dealing with their account, where they can make changes or see their status. I wouldn’t think you would make all guest information known to any individual. It’s your call how you design the program.
there s a problem with my inserting the status. right now i have two names and the status only changes for the second name. it totally ignores the radio button for first name.
name|status|
Alex|radio buttons| <-radio button status gets ignored even if i change it default unconfirmed
Piper|radio buttons| <-this is okay, status changes as its suppose to
insert:
if(isset($_SESSION['sess_user_id']))
{
if(isset($_POST['save-rsvp']))
{
require "connection.php";
$name = $_POST['name'];
$radio = $_POST['rsvp'];
$session = $_SESSION['sess_user_id'];
$rsvp = $dbh->prepare("UPDATE guest SET status = ? WHERE guser_id = ? AND guest_name = ?");
$rad = implode(',', $radio);
$rsvp->bindParam(1, $rad, PDO::PARAM_INT);
$rsvp->bindParam(2, $session, PDO::PARAM_INT);
$rsvp->bindParam(3, $name, PDO::PARAM_STR);
$rsvp->execute();
if($rsvp->rowCount() > 0)
{
echo "okay";
}
}
}
btw, I wanted to know if these:
PDO::PARM_INT
PDO::PARAM_STR
are necessary? in some queries i have them and some I don’t.
Can you post form code?
////
How can you have session user id on a bunch of guests?
Why are you imploding $_POST[‘rsvp’]? In my version, the KEY is the guest id and the VALUE is their status selection.
I do not use ? placeholders with PDO. I used names.
$rsvp->bindParam(":rad", $rad);
and in the query I used the name placeholder.
SET status = :rad
i used implode coz when i try to insert i get warning: array to string conversion. so i thought that was the problem an added it and well it got fixed and inserted as it should so i thought i did the right thing.
the form:
<form method="post" action="rsvp.php">
<div class="rsvp-table-container">
<tbody>
<table class="rsvp-table">
<thead>
<tr>
<th>Guests</th>
<th>Status</th>
</tr>
</thead>
<tbody id="rsvp-body">
//the list is on another page called using ajax
</tbody>
</table>
</tbody>
<br>
<input type="submit" name="save-rsvp" value="Save">
</div>
<br>
</form>
the page:
<?php
$t = $_GET['t'];
require "connection.php";
$check = $dbh->prepare("SELECT guest_id,guest_name,status FROM guest WHERE event_id = :t ");
$check->bindParam(':t', $t);
$check->execute();
if ($check->rowCount() > 0)
{
$check->setFetchMode(PDO::FETCH_ASSOC);
$Guests = 0;
$Unconfirmed = 0;
$Attending = 0;
$NotAttending = 0;
while ($row = $check->fetch())
{
$id = $row['guest_id'];
$name = $row['guest_name'];
$status = $row['status'];
$status_array = array("unconfirmed", "attending", "not attending");
//Get counts
$Guests++;
switch ($status) {
case 0:
$Unconfirmed++;
break;
case 1:
$Attending++;
break;
case 2:
$NotAttending++;
break;
}
echo "<tr>";
echo "<td><input type='hidden' name='name' value='$name'><span>$name</span></td>";
foreach($status_array as $k => $v):
$checked = ($status == $k ? ' checked="checked"' : '');
echo "<td><input type='radio' name='rsvp[" . $id . "]' value='" . $k . "'" . $checked . " />" . $v . "</td>";
endforeach;
echo "</tr>";
}
echo "<tr><td><label>Total Guests : </label></td><td><input type='text' name='total-guest' value='" . $Guests . "' readonly></td></tr>
<tr><td><label>Unconfirmed : </label></td><td><input type='text' name='unconfirmed' value='" . $Unconfirmed . "' readonly></td></tr>
<tr><td><label>Attending : </label></td><td><input type='text' name='attending' value='" . $Attending . "' readonly></td></tr>
<tr><td><label>Not Atending : </label></td><td><input type='text' name='not-attending' value='" . $NotAttending . "' readonly></td></tr>\\r";
}
else
{
echo "<tbody><tr><td>No Guests invited.</td><td></td></tr>";
}
?>
OK, great. I’m glad you got it working.
not really im still having the issue of inserting the status for the first row in the table. example table
Name | Status
Alex | radio buttons
Max | radio buttons
ALEX is the first name(first row) in the table. by default all rows show unconfirmed but when i change this names radio button to attending and try to save it it saves/reverts back to its default = unconfirmed
MAX is the next name in the table. as it should by default it shows unconfirmed and when changed to not attending and save it, it saves as it should = attending
even though there are 5 or 10 names(rows), the first name does not read the status change on save. it will stay unchanged = unconfirmed.
lets say there is only one name(one row), the status(radio button) for this name changes accordingly when changed.
Can you post full code you are working with?
Will each person be attending many events?
this is the table:
<form method="post" action="rsvp.php">
<div class="rsvp-table-container">
<tbody>
<table class="rsvp-table">
<thead>
<tr>
<th>Guests</th>
<th>Status</th>
</tr>
</thead>
<tbody id="rsvp-body">
//the list is on another page called using ajax
</tbody>
</table>
</tbody>
<br>
<input type="submit" name="save-rsvp" value="Save">
</div>
<br>
</form>
other page
<?php
$t = $_GET['t'];
require "connection.php";
$check = $dbh->prepare("SELECT guest_id,guest_name,status FROM guest WHERE event_id = :t ");
$check->bindParam(':t', $t);
$check->execute();
if ($check->rowCount() > 0)
{
$check->setFetchMode(PDO::FETCH_ASSOC);
$Guests = 0;
$Unconfirmed = 0;
$Attending = 0;
$NotAttending = 0;
while ($row = $check->fetch())
{
$id = $row['guest_id'];
$name = $row['guest_name'];
$status = $row['status'];
$status_array = array("unconfirmed", "attending", "not attending");
//Get counts
$Guests++;
switch ($status) {
case 0:
$Unconfirmed++;
break;
case 1:
$Attending++;
break;
case 2:
$NotAttending++;
break;
}
echo "<tr>";
echo "<td><input type='hidden' name='name' value='$name'><span>$name</span></td>";
foreach($status_array as $k => $v):
$checked = ($status == $k ? ' checked="checked"' : '');
echo "<td><input type='radio' name='rsvp[" . $id . "]' value='" . $k . "'" . $checked . " />" . $v . "</td>";
endforeach;
echo "</tr>";
}
echo "<tr><td><label>Total Guests : </label></td><td><input type='text' name='total-guest' value='" . $Guests . "' readonly></td></tr>
<tr><td><label>Unconfirmed : </label></td><td><input type='text' name='unconfirmed' value='" . $Unconfirmed . "' readonly></td></tr>
<tr><td><label>Attending : </label></td><td><input type='text' name='attending' value='" . $Attending . "' readonly></td></tr>
<tr><td><label>Not Atending : </label></td><td><input type='text' name='not-attending' value='" . $NotAttending . "' readonly></td></tr>\\r";
}
else
{
echo "<tbody><tr><td>No Guests invited.</td><td></td></tr>";
}
?>
this is the insert. i used implode as i explained because i got array to string conversion warning.
if(isset($_SESSION['sess_user_id']))
{
if(isset($_POST['save-rsvp']))
{
require "connection.php";
$name = $_POST['name'];
$radio = $_POST['rsvp'];
$session = $_SESSION['sess_user_id'];
$rsvp = $dbh->prepare("UPDATE guest SET status = ? WHERE guser_id = ? AND guest_name = ?");
$rad = implode(',', $radio);
$rsvp->bindParam(1, $rad, PDO::PARAM_INT);
$rsvp->bindParam(2, $session, PDO::PARAM_INT);
$rsvp->bindParam(3, $name, PDO::PARAM_STR);
$rsvp->execute();
if($rsvp->rowCount() > 0)
{
echo "okay";
}
}
}
yes each person will be able to attend many different events if chosen.
So the code you posted is used by a visitor/user, correct? So we are dealing with a single user, many events that they will mark their intention of taking part in the event, correct?
I think you should have a separate table for holding their intentions. Something like attending_guests
id
, event_id
, user_id
, status
and date_updated
.
But back to the issue. You are referring to Alex and Max, which leads me to think this is an Admin page where you are making this status change. Problem is you are using a user session id for making this update. Can you please explain who is using this page?
If a user, I understand and code makes sense (though might need some tweaking).
If is is the Admin user, how are you setting the user id to session for each person? (I believe I’ve asked this question already.)
Please explain the goal of this page.
EDIT: Really, the form section is Admin and the Update section is for a guest. So something’s got to give.
Looks like the form should be adjusted to pass the guser_id and then you would use THAT for the update instead of session id.
Well this is my take on what you wish to do.
As ADMIN you are selecting an event via GET then listing all guests that are in the guest table for this event. You are then changing their status for this event as needed.
That being the case, the quest table will have a row for each event a guest is listed for.
So if Drummin is listed for events A B and C there will be a row in the guest DB table for each event A B and C or three rows in the guest table.
Now the form input for name needs to be an array with the guest id as the key just like you added for the rsvp. You also will need the event id, also with the user id key, so all POSTs have the same key.
<input type='hidden' name='event_id[" . $id . "]' value='$t'>
<input type='hidden' name='name[" . $id . "]' value='$name'>
Then for processing, you will use foreach to loop through this POST array again using the KEY to get matching POST values.
I left your $_SESSION[‘sess_user_id’] condition around processing only as a permission check but not for processing. If you are not logged in as ADMIN with this session key, then change the key or remove the condition.
I also made a count of updated records set to message variable to be echoed withing the <body> tag. Here’s my version.
<?php
//Uncomment to view POST
//echo "<pre>";
//print_r($_POST);
//echo "</pre>";
//Assuming $_SESSION['sess_user_id'] is an ADMIN id used here as a permission check otherwise remove condition.
if(isset($_SESSION['sess_user_id']))
{
if(isset($_POST['save-rsvp']))
{
require "connection.php";
$cnt=0;
foreach($_POST['rsvp'] as $guser_id => $rsvp):
$name = $_POST['name'][$guser_id];
$status = $_POST['rsvp'][$guser_id];
$event_id = $_POST['event_id'][$guser_id];
$rsvp = $dbh->prepare("UPDATE guest SET status = :status WHERE guser_id = :guser_id AND guest_name = :name AND event_id = :event_id");
$rsvp->bindParam(':status', $status, PDO::PARAM_INT);
$rsvp->bindParam(':guser_id', $guser_id, PDO::PARAM_INT);
$rsvp->bindParam(':name', $name, PDO::PARAM_STR);
$rsvp->bindParam(':event_id', $event_id, PDO::PARAM_INT);
$rsvp->execute();
if($rsvp->rowCount() > 0)
{
$cnt++;
}
endforeach;
$plural = ($cnt != 1 ? 's were' : ' was');
$message = $cnt . " record" . $plural . " updated";
}
}
?>
<html>
<body>
<?php
if(isset($message)){ echo $message;}
?>
<form method="post" action="">
<div class="rsvp-table-container">
<tbody>
<table class="rsvp-table">
<thead>
<tr>
<th>Guests</th>
<th>Status</th>
</tr>
</thead>
<tbody id="rsvp-body">
<?php
if(isset($_GET['t'])){
$t = $_GET['t'];
require "connection.php";
$check = $dbh->prepare("SELECT guest_id,guest_name,status FROM guest WHERE event_id = :t ");
$check->bindParam(':t', $t);
$check->execute();
if ($check->rowCount() > 0)
{
$check->setFetchMode(PDO::FETCH_ASSOC);
$Guests = 0;
$Unconfirmed = 0;
$Attending = 0;
$NotAttending = 0;
while ($row = $check->fetch())
{
$id = $row['guest_id'];
$name = $row['guest_name'];
$status = $row['status'];
$status_array = array("unconfirmed", "attending", "not attending");
//Get counts
$Guests++;
switch ($status) {
case 0:
$Unconfirmed++;
break;
case 1:
$Attending++;
break;
case 2:
$NotAttending++;
break;
}
echo "<tr>";
echo "<td>
<input type='hidden' name='event_id[" . $id . "]' value='$t'>
<input type='hidden' name='name[" . $id . "]' value='$name'>
<span>$name</span>
</td>";
foreach($status_array as $k => $v):
$checked = ($status == $k ? ' checked="checked"' : '');
echo "<td><input type='radio' name='rsvp[" . $id . "]' value='" . $k . "'" . $checked . " />" . $v . "</td>";
endforeach;
echo "</tr>";
}
echo "<tr><td><label>Total Guests : </label></td><td><input type='text' name='total-guest' value='" . $Guests . "' readonly></td></tr>
<tr><td><label>Unconfirmed : </label></td><td><input type='text' name='unconfirmed' value='" . $Unconfirmed . "' readonly></td></tr>
<tr><td><label>Attending : </label></td><td><input type='text' name='attending' value='" . $Attending . "' readonly></td></tr>
<tr><td><label>Not Atending : </label></td><td><input type='text' name='not-attending' value='" . $NotAttending . "' readonly></td></tr>\\r";
}
else
{
echo "<tbody><tr><td>No Guests invited.</td><td></td></tr>";
}
}else{
echo "<tr><td>No Event Selected.</td><td></td></tr>";
}
?>
</tbody>
</table>
</tbody>
<br>
<input type="submit" name="save-rsvp" value="Save">
</div>
<br>
</form>
</body>
</html>
My uncertainty with the code is the fields guser_id and guest_id. You are using guest_id in the form and guser_id for processing. I would imagine processing should be changed to use guest_id as well. What is the difference of these two fields?