Handling a radio button array in php
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 foreign 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 input 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 "' > ";
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='accommod开发者_开发知识库ation_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 prepped the names from my post as follows:
$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 proceeded to write insert statements to insert the data 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, however the first query 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 ?
why not try reviewing the code from scratch and see if there are any syntax errors and then move on.
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"];
精彩评论