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

Re: Need some serious help, wedding RSVP script

Post by RafaelT »

We started discussing it 9 posts above this one.

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

Code: Select all

 
$data = mysql_query("SELECT * FROM Invites WHERE num = " . $_POST['moo'] . "") or die(mysql_error());
 
It allows the script to work and update the db but then it errors out. I tried everything to change the query ever so slightly to make it happy.

Pickle said it is supposed to display the options they selected, I would be fine with that or if after the submitted if it just took them to another thank you page. Anything so the error stops.

Thanks
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 »

This is completely un-tested but I think I have understood your objective.

Here you go:

Code: Select all

 
<!--PAGE 1 -->
<form method="post" action="page2.php">
<input type=text name='moo' size=60 maxlength=255>
<input type="submit" name="submit_moo" value="Enter">
</form>
 
<!--PAGE 2 -->
<?
mysql_connect("localhost", "xxxxx", "xxxxxx") or die(mysql_error());
mysql_select_db("wedding") or die(mysql_error());
 
if(isset($_POST['submit_moo']) || isset($_POST['moo'])){
//check if empty else
$moo = trim(mysql_real_escape_string($_POST['moo'])); //get user input
$query = mysql_query("SELECT * FROM Invites WHERE num = '$moo'");
 
//must keep form and table opening tag outside of loop
echo'<form method="post" action="page3.php">';
echo'<table cellspacing='16' cellpadding='0' border='0'  >';
 
while($row = mysql_fetch_array( $query ))
{
echo"<tr>
    <td class='form_field' valign='top' align='right'>" . $row['name'] . " </td> 
    <td>
        <select name='attend[]'>
            <option value=''>- Select -</option>
            <option  value='1'> Will be attending</option>
            <option  value='0'>Will not be attending</option>
        </select>
        <input type='hidden' name='id[]' value='".$row['id']."' />
    </td>
    </tr>"; 
}
?>
 
<tr><td colspan=3 align='center'><input type='submit_attend' value='Submit'> &nbsp;&nbsp; <input type='button' value='Cancel' onclick="location.href='/';"></td></tr>
</table>
 
</form>
 
 
<!--PaGE 3 -->
<?
if(isset($_POST['submit_attend']))
{
    //loop through posted selections
    for($i=0; $i<count($_POST['attend']); $i++)
    {
        $attend = $_POST['attend'][$i];
        $id = $_POST['id'][$i];
        
        if($attend == 1)
        {
            //update db
        }
    }
}
?>
 
Like i said, its un-tested but it should give you some insight.
RafaelT
Forum Newbie
Posts: 16
Joined: Mon Aug 10, 2009 10:31 am

Re: Need some serious help, wedding RSVP script

Post by RafaelT »

Thanks for the reply. Yes it appears you also understood what I am looking for. After a couple minor changes I got the first two parts of the script working. Unfortunately I can't quite figure out what I need to do with that third part. I messed around with it but nothing I tried came out right.

Pickle, on your script since that one item seems t be causing the problem, would it be possible to make it send the user to another page instead of trying to display the updated info?

This is so frustrating. While I do enjoy this and want to learn a lot more I know I am in over my head on this project. I had a friend helping me on this and he bailed at the last minute. Now I have my wedding coming up and I have to spend my time struggling with this (The invites are already printed and mention RSVP'ing online). I really do appreciate all the help you guys are giving me.
Last edited by RafaelT on Tue Aug 11, 2009 12:07 am, edited 1 time in total.
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 are you stuck with? The UPDATE query?
RafaelT
Forum Newbie
Posts: 16
Joined: Mon Aug 10, 2009 10:31 am

Re: Need some serious help, wedding RSVP script

Post by RafaelT »

Yes, I am just not quite sure how to get that query working properly, I can't make sense of all the stuff there 100%.

I know the query needs to be along the lines of UPDATE Invites SET attend = XXXXX WHERE id = XXXX but I just don't know what to put where or how to get it to do it for each person.

Also I had to change the code for the submit button on page two, it wasnt coming up as a button. I changed it to

Code: Select all

<input type='submit' name='submit_attend' value='Submit'>
But I don't know if that broke anything.

Thanks
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 »

Changing the button should be fine.

Ok, so the page 3 section uses a FOR loop which loops the POSTED values of the drop-down lists. The number of loops is determined by the number of selected lists - that's where count() is useful.

So this is how you UPDATE query should look:

Code: Select all

 
<!--PaGE 3 -->
<?
if(isset($_POST['submit_attend']))
{
    //loop through posted selections
    for($i=0; $i<count($_POST['attend']); $i++)
    {
        $attend = $_POST['attend'][$i]; //attending or not (1 or 0 respectively)
        $id = $_POST['id'][$i]; //the persons id
       
        if($attend == 1) //this line determines whether a person is attending
        {
            //update db
            //change table_name to your own table's name
            //I used field_name as an example and I set it to 1 to indicate that this person is attending
            mysql_query("UPDATE table_name SET field_name=1 WHERE id='$id' LIMIT 1");
        }
    }
}
?>
 
Is this what you're after?

If you need to update multiple fields then just insert a comma after each value except the last one.
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 »

~pickle went home from work ;)
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 »

That worked, thank you so much. I made a couple little modifications and it is perfect.

I posted a copy of the working version in case anyone wanted to see it. Obviously I still have to make it all look pretty.

http://rustedjunk.net/test1.php

You can use 000003 for the number you must enter.

I want to thank both of you for all the time you put in, I learned a lot.
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 »

You're welcome.
Post Reply