views:

433

answers:

2

Hi all, I have a bit of PHP code that is really taking a toll on me. To simplify, I have a table where three of the columns are forign keys (event_type, accommodation_type and public_user). I am dynamically creating a set of radio buttons and I have set the ID attribute on each imput value correctly.

                        $result2 = mysql_query("SELECT * FROM event_type", $connection);
                        if(!$result2){
                            die("Database query failed: " . mysql_error());
                        }
                        $event = mysql_fetch_array($result2);   

                    echo "<input type='radio' name='event_type' value='";
                    echo $event["id"];
                    echo "' >&nbsp;&nbsp;";
                    echo $event['name'];
                    echo "</input><br /><br />";$result4 = mysql_query("SELECT * FROM accommodation_type", $connection);

                        if(!$result4){
                            die("Database query failed: " . mysql_error());
                        }
                        while($row = mysql_fetch_array($result4)){
                            if($row["id"] == 7 || $row["id"] == 8){
                            echo"<tr><td>";
                            echo $row["name"] ;
                            echo"</td><td>$";
                            echo $row["price"];
                            echo ".00</td><td>";
                            echo "<input type='radio' name='accommodation_type' value='";
           echo $row["id"];
                            echo "' />";
                            echo "</td></tr>";
                            }
                        }

I retrieved the correct id from the query. Thus after submitting the form with POST, I go on to do some minor validation and preped the names from my post as folows.

    $event_type = trim(mysql_prep($_POST['event_type']));
    $accommodation_type= trim(mysql_prep($_POST['accommodation_type']));
    $public_user = trim(mysql_prep($_POST['public_user']));
    $comments = trim(mysql_prep($_POST['comments']));
    $grand_total = trim(mysql_prep($_POST['grand_total']));

I then proceded to write insert statements to insert the dada into the relevant tables. This requires two queries as follows.

        $query = "INSERT INTO event_registration (event_type, accommodation_type, public_user, comments, grand_total) VALUES ('{$event_type}','{$accommodation_type}','{$public_user}','{$comments}','{$grand_total}')";
        $query1 = "INSERT INTO additional_member (first_name, last_name, gender, age_group, public_user) VALUES ('{$first_name}','{$last_name}','{$gender}','{$age_group}','{$public_user}')";
        $result = mysql_query($query, $connection);
        $result1 = mysql_query($query1, $connection);

The query1 works as expected, hower the first querry fails to insert the data. It says undefined index at the lines where I have

    $event_type = trim(mysql_prep($_POST['event_type']));
    $accommodation_type= trim(mysql_prep($_POST['accommodation_type']));

I am not entirely sure where things went wrong. Everything seems to be set up correctly and the data from the form just refuses to be saved.

Any ideas ?

A: 

You are creating accommodation type

 echo "<input type='radio' name='accommodation_type' value='";
       echo $row["id"];

But not echoing event_type anywhere. Try echoing that too:

 echo "<input type='radio' name='accommodation_type' value='";
       echo $row["id"];

 echo "<input type='radio' name='event_type' value='";
       echo $row["whatever"];
Sarfraz
Sorry for omitting this part, I have edited the question above to include this part. It was already done and the error continues to persist.Kind regards.
Binaryrespawn
can you paste down the error you are getting?
Sarfraz
Now i could suggest couple of things: make sure that there is also a FORM tag enclosing these inputs and second make sure that you are using the appropriate form array eg $_POST, to see that's what you have defind in FORM attribute.
Sarfraz
yes this was infact done as follows <form action="eventregistration.php" method="post" id="form1"> I thnk the problem stems from the validation because the both errors also refer to a function used for our validation.
Binaryrespawn
not sure but if those two lines are inside function, put this as first line in your function: global $_POST;
Sarfraz
You are getting those errors due to your POST array, it seems your calling the wrong values, which throws undefined index.
Anthony Forloney
Well actually after the POST I was able to echo the values because I tested if the method from the server was post then show the form else echo the following:echo "The event type" . $_POST['event_type'];echo $_POST['accommodation_type'];This worked perfectly. And this is the confusing part If it is being posted correctly why is it not hitting the dadabase table
Binaryrespawn
ok, see what error mysql gives: $result = mysql_query($query, $connection) or die(mysql_error());
Sarfraz
after the queries i have the following checks implemented.if($result } else{ $message = "The User could not be registered"; $message .= "<br />" . mysql_error();}
Binaryrespawn
Actually, the `accommodation_type` and `event_type` variables are coming empty, that's why you are getting undefined index error, you need to check bit by bit what is the place where they get empty.
Sarfraz
this is quite accurate, you see we have two radio buttons for selecting the event_type...its one or the other. I just tried using the other option while testiing, but an undefined index error was generated. I really think we need to revisit the way we are constructing thr fields on the forn.
Binaryrespawn
+1  A: 

why not try reviewing the code from scratch and see if there are any syntax errors and then move on.

pundit
Syntax errors, flow control have been plaguing the code. We reviewed the code and rebuilt it from the ground up and now it works as expected.
Binaryrespawn