Need some serious help, wedding RSVP script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

RafaelT
Forum Newbie
Posts: 16
Joined: Mon Aug 10, 2009 10:31 am

Need some serious help, wedding RSVP script

Post by RafaelT »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Ok, so let me start off by saying I am in way over my head and really have no idea what I am doing. I have managed to piece together mostly by myself and with a little help from others and almost functioning horribly written piece of code. I have been working on this for 14 hours straight and I really need some help.

What I am doing is this... The wedding guest enters a code and it returns a list of the people in there family who were invited. That list has an option for them to attend or not attend. Up to that point everything is working. What I can not get to work is having it take there answer and update the database.
Part of my problem is the code is so butchered I think it is making it harder for things to work properly.

My db has the following fields: id (a unique id number), num ( a group number, everyone in the family has the same one thats who the list of guests is returned to the user), name (name of the guest), and attend (this needs to be updated by the script with the users option the select, whether they will be there or not)

Thank you for anything you can do for me, hopefully I gave all the info you may need.

This is the working code that the user enters the invite number and is sent to the next page that lists the options

Code: Select all

 
<form method="post" action="confirm.php">
<input type=text name='moo' size=60 maxlength=255>
<input type="submit" value="Enter">
</form>
 
This is the mess of code that doesent work that needs to update the db with there answer

Code: Select all

<?
mysql_connect("localhost", "xxxxx", "xxxxxx") or die(mysql_error());
mysql_select_db("wedding") or die(mysql_error());
$data = mysql_query("SELECT * FROM Invites WHERE num = " . $_POST['moo'] . "")
or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
?>
<form method="post" action="send.php">
 
<table cellspacing='16' cellpadding='0' border='0'  >
 
<?
print ("   
<tr>
   <td class='form_field' valign='top' align='right'>" . $info['name'] . " </td>
 
 
      <td>
<select name='ATTEND" . $info['id'] . "'>
<option value=''>- Select -</option>
 <option  value='1'   > Will be attending
<option  value='0'   >Will not be attending
</select>
 
      </td>
   </tr>
");
 
}
?>
 
<tr><td colspan=3 align='center'><input type='submit' value='Submit'> &nbsp;&nbsp; <input type='button' value='Cancel' onclick="location.href='/';"></td></tr>
</table>
 
</form>
This is the code someone else suggested to get it to update the db but i have no idea what to do with it. i don't know if it will help out.

Code: Select all

 
foreach ($_POST['attend'] as $pk => $willornot) {
    if (true == $willornot) {
        //assuming attending BOOLEAN NOT NULL DEFAULT FALSE
        $query = 'UPDATE invites SET attending = TRUE WHERE yourUniqueColumn = ' . $pk;
        //..
    }
}
 

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Need some serious help, wedding RSVP script

Post by pickle »

- The second bit of code - is that in send.php?
- That second bit of code also looks like you're allowing someone to set the attendance status of anyone in the family? Is that what you want or do you want a single invitee to only see the attendance status of people in their family, and set their own attendance status?
- The third bit of code will be needed in some format - you need something to update the database.

This is pretty simple (for someone who's head is above water ;)) - I should be able to help.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
RafaelT
Forum Newbie
Posts: 16
Joined: Mon Aug 10, 2009 10:31 am

Re: Need some serious help, wedding RSVP script

Post by RafaelT »

Thank you, and sorry, I will be sure to use the correct tags in the future.

The second bit of code is in confirm.php. Users are sent there directly from the first piece of code. send.php is left over from when i was going to have it email me the results instead of updating the DB, I believe send.php is no longer needed, all the updating of the db I would imagine can be done from confirm.php

That is correct, i am allowing anyone in the family to set the attendance for anyone in the family. For the most part it will be a husband and wife or husband, wife and kid, so I think it should be fine for whoever gets the invite to rsvp for all of them.

I really appreciate the help.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Need some serious help, wedding RSVP script

Post by aceconcepts »

What you'll probably want to do is create a hidden field containing the id of the person that is responding (from the list). Use this ID to update your DB.
RafaelT
Forum Newbie
Posts: 16
Joined: Mon Aug 10, 2009 10:31 am

Re: Need some serious help, wedding RSVP script

Post by RafaelT »

I was messing around with that earlier, it seems like every time I try to change anything in the script it just breaks it now. I think I just have to many different scripts pieced together.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Need some serious help, wedding RSVP script

Post by pickle »

As I suspected (and I imagine you did as well), your code was all messed up - your form & table were being created multiple times inside the loop.

Here's what I came up with:

Code: Select all

<?
mysql_connect("localhost", "xxxxx", "xxxxxx") or die(mysql_error());
mysql_select_db("wedding") or die(mysql_error());
 
 
# Do attendance updating
if(is_array($_POST['attend']))
{
    foreach($_POST['attend'] as $id=>$value)
    {
        $query = 'UPDATE Invites SET attending = "'.mysql_real_escape_string($value).'" WHERE id = "'.mysql_real_escape_string($id).'"';
        $result = mysql_query($query);
    }
}
 
 
 
$data = mysql_query("SELECT * FROM Invites WHERE num = " . $_POST['moo'] . "")
or die(mysql_error());
 
?>
<form method="post" action="">
<table cellspacing='16' cellpadding='0' border='0'  >
 
<?php
while($info = mysql_fetch_array( $data ))
{
?>
    
        <tr>
            <td class='form_field' valign='top' align='right'>
                <?php echo $info['name']; ?>
            </td>
            <td>
                <select name="attend[<?php echo $info['id']; ?>]">
                    <option value=''>- Select -</option>
                    <option  value='1'   > Will be attending
                    <option  value='0'   >Will not be attending
                </select>
            </td>
        </tr>  
<?php 
}
?>
        <tr>
            <td colspan=3 align='center'>
                <input type='submit' value='Submit' name = "submit"> &nbsp;&nbsp; 
                <input type='button' value='Cancel' onclick="location.href='/';">
            </td>
        </tr>
    </table>
</form>
The most notable change (aside from making the page output code that made sense) is how the select box is named. The way I've got it, $_POST['attend'] will be an array containing the attendance status of all family members.

Of course, there is still lots to do on this page (how to handle the attendance status not being a 1 or 0, pre-populating the select fields with the family member's existing attendance status, probably some other stuff), but I think this script will solve your problem. I haven't tested it obviously.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
RafaelT
Forum Newbie
Posts: 16
Joined: Mon Aug 10, 2009 10:31 am

Re: Need some serious help, wedding RSVP script

Post by RafaelT »

Wow, that is a lot prettier then that mess I had.

I am having a problem though, I am getting there error "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"

I can't figure out why it is giving that error.

Thanks
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Need some serious help, wedding RSVP script

Post by pickle »

Store the query in a string, then output that string. It might not be what you expect.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
RafaelT
Forum Newbie
Posts: 16
Joined: Mon Aug 10, 2009 10:31 am

Re: Need some serious help, wedding RSVP script

Post by RafaelT »

I don't know what you mean by that, I tried looking it up but couldn't get a clear answer.

Everything on or around line one though matches some of the scripts I have been messing with all day though and they are connecting fine so I am lost again.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Need some serious help, wedding RSVP script

Post by pickle »

The error is with the query, not with PHP.

Change this line:

Code: Select all

$data = mysql_query("SELECT * FROM Invites WHERE num = " . $_POST['moo'] . "") or die(mysql_error());
with these:

Code: Select all

$query = "SELECT * FROM Invites WHERE num = " . $_POST['moo'];
echo $query;
$data = mysql_query($query) or die(mysql_error());
Examine what is output by that echo statement - the query might not be completely what you're expecting.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
RafaelT
Forum Newbie
Posts: 16
Joined: Mon Aug 10, 2009 10:31 am

Re: Need some serious help, wedding RSVP script

Post by RafaelT »

ok... so it looks like it is still trying to select stuff instead of updating stuff when you click submit... the query toward the top that says UPDATE looks more like what needs to be happening.. I am just not quite sure how to make the connection
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Need some serious help, wedding RSVP script

Post by pickle »

The page will be doing both. First, it will be updating the attendance flag, then it retrieves all the attendance information for the family.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
RafaelT
Forum Newbie
Posts: 16
Joined: Mon Aug 10, 2009 10:31 am

Re: Need some serious help, wedding RSVP script

Post by RafaelT »

I took a look at the db and it is updating correctly, so the error is happening after that when it comes to displaying the status for the user. I know it says it has to do with the sql so I tried just about everything to change the command so it might work, I just kept getting other errors instead, I am out of ideas on that one.
RafaelT
Forum Newbie
Posts: 16
Joined: Mon Aug 10, 2009 10:31 am

Re: Need some serious help, wedding RSVP script

Post by RafaelT »

So it looks like pickle disappeared for a while.... anyone else have any ideas about that error? *points a few posts up*
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Need some serious help, wedding RSVP script

Post by aceconcepts »

So what error(s) are you getting now?
Post Reply