Page 2 of 2

Re: Need some serious help, wedding RSVP script

Posted: Mon Aug 10, 2009 8:09 pm
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

Re: Need some serious help, wedding RSVP script

Posted: Mon Aug 10, 2009 8:40 pm
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.

Re: Need some serious help, wedding RSVP script

Posted: Mon Aug 10, 2009 11:46 pm
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.

Re: Need some serious help, wedding RSVP script

Posted: Mon Aug 10, 2009 11:49 pm
by aceconcepts
What are you stuck with? The UPDATE query?

Re: Need some serious help, wedding RSVP script

Posted: Mon Aug 10, 2009 11:58 pm
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

Re: Need some serious help, wedding RSVP script

Posted: Tue Aug 11, 2009 12:10 am
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.

Re: Need some serious help, wedding RSVP script

Posted: Tue Aug 11, 2009 9:45 am
by pickle
~pickle went home from work ;)

Re: Need some serious help, wedding RSVP script

Posted: Tue Aug 11, 2009 12:21 pm
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.

Re: Need some serious help, wedding RSVP script

Posted: Tue Aug 11, 2009 1:22 pm
by aceconcepts
You're welcome.