php5 mysqli and radio buttons

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

Post Reply
ummhasan
Forum Commoner
Posts: 31
Joined: Sun Jun 13, 2010 7:24 pm
Location: USA

php5 mysqli and radio buttons

Post by ummhasan »

Hi,

I have searched and searched and searched to no avail!

I have developed a site that has a registration form and this registration form has 5 radio buttons that the registrant will choose from to tell the admin what level of competition they want to enter.

I have a mySQL database with 5 corresponding tables.

I need the php program to write the information in the correct table based on the radio button selected. For example, if a user choosed elementary the program will write all form values to the elementary table in the database

thanks a lot
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php5 mysqli and radio buttons

Post by Jonah Bron »

That's not a extensible solution. Unless each table has different columns, you really should put everything into one table, and just add one column to specify the level. If you really need to have five tables, then you will need a switch statement, vaguely like this:

Code: Select all

switch ($_POST['myradiobutton']) {
    case 'level 1':
        // insert any input for level 1 into it's table
        break;
    case 'level 2':
        // insert any input for level 2 into it's table
        break;
    // ...
}
ummhasan
Forum Commoner
Posts: 31
Joined: Sun Jun 13, 2010 7:24 pm
Location: USA

Re: php5 mysqli and radio buttons

Post by ummhasan »

sorry, i forgot to mention that I also want the auto-incremented registrant id and their partner id to be displayed back to the registrant. I would really appreciate a sample code! This site is due to go live tomorrow and I am going to have to already put a notice on the registration page that online registration is not yet available.

All I need is a sample code that will do the following:
1. connect to the database via mySQLi with PHP 5 - (i already have this coded)
2. show errors of connections (already coded)
3. validate the form (already coded)
4. Take these five valuse: radio - preE, radio - elem, radio - inter, radio - junior, radio - senior and..........
if preE is checked write all form values (name, address, ph, dob, email, partner name, partner addy, partner ph, etc.to database table named pre_elementary
then write result of auto incremented id back to user and if values are true for partner also write auto incremented id for partner (table has reg1 as 0 reg2 as 1)

else if elem is checked write all form values to table named elementary and do the above

else if inter is checked do the same

else if junior is checked do the same

else if senior is checked do the same

close db

Below is what I have already that is not writing to the db
$hostname_registration = "my_hosename";
$database_registration = "my_database";
$username_registration = "my_username";
$password_registration = "my_password";
$registration = mysql_pconnect($hostname_registration, $username_registration, $password_registration) or trigger_error(mysql_error(),E_USER_ERROR);

//create short variable names
$name1=$_POST[name1]; $ph1=$_POST[ph1]; $dob1=$_POST[dob1];
$parentName1=$_POST[parentName1]; $email1=$_POST[email1]; $address1=$_POST[address1];
$city1=$_POST[city1]; $state1=$_POST[state1]; $zip1=$_POST[zip1];
$name2=$_POST[name2]; $ph2=$_POST[ph2]; $dob2=$_POST[dob2];
$parentName2=$_POST[parentName2]; $email2=$_POST[email2]; $address2=$_POST[address2];
$city2=$_POST[city2]; $state2=$_POST[state2]; $zip2=$_POST[zip2];
$levels=$_POST[levels]; $submit=$_POST[submit];

//field validation - if field is null user is asked to complete otherwise confirm and show entries
if (!$name1 || !$ph1 || !$dob1 || !$parentName1 || !$email1 || !$address1 || !$city1 || !$state1 || !$zip1) {
echo "You have not entered all the required information. Please go back and fully complete the form.";
} elseif (!$levels) {
echo "You did not specify which level of competition you are entering. Please go back and specify a level.";
} elseif () {
echo "<h2>Jazak Allah Khair for registering, you will receive an email shortly with your registration number in sha Allah.</h2>";
echo "<p><strong>Your registration information is also below.</strong></p>";
echo "<table cellpadding='3'>";
echo "<tr><td>Name:&nbsp;".$name1."</td><td>Phone:&nbsp;".$ph1."</td><td>DOB:&nbsp;".$dob1."</td><td></td></tr>";
echo "<tr><td>Parent:&nbsp;".$parentName1."</td><td>Parent Email:&nbsp;".$email1."</td><td></td><td></td></tr>";
echo "<tr><td>Address:&nbsp;".$address1."</td><td>".$city1."</td><td>".$state1."</td><td>".$zip1."</td></tr></table>";
}

if ($name2 || $ph2 || $dob2 || $parentName2 || $email2 || $address2 || $city2 || $state2 || $zip2) {
echo "<table cellpadding='3'><tr><td>Partners Name:&nbsp;".$name2."</td><td>Partners Phone:&nbsp;".$ph2."</td><td>Partners DOB:&nbsp;".$dob2."</td><td></td></tr>";
echo "<tr><td>Partners Parent:&nbsp;".$parentName2."</td><td>Partners Email:&nbsp;".$email2."</td><td></td><td></td></tr>";
echo "<tr><td>Partners Address:&nbsp;".$address2."</td><td>".$city2."</td><td>".$state2."</td><td>".$zip2."</td></tr>";
} else {
echo "<tr><td colspan='4'>You have choosed to enter without a partner - if you change your mind, please contact us via email.</td></tr></table>";
}

//add slashes before special charachters when input to database
if (!get_magic_quotes_gpc()) {
$name1 = addslashes($name1); $ph1 = addslashes($ph1); $dob1 = addslashes($dob1);
$parentName1 = addslashes($parentName1); $email1 = addslashes($email1); $address1 = addslashes($address1);
$city1 = addslashes($city1); $state1 = addslashes($state1); $zip1 = addslashes($zip1);
$name2 = addslashes($name2); $ph2 = addslashes($ph2); $dob2 = addslashes($dob2);
$parentName2 = addslashes($parentName2); $email2 = addslashes($email2); $address2 = addslashes($address2);
$city2 = addslashes($city2); $state2 = addslashes($state2); $zip2 = addslashes($zip2);
}
switch($levels)
{
case "preE": $checkpe = "checked"; break;
case "elem": $checke = "checked"; break;
case "inter": $checki = "checked"; break;
case "junior": $checkj = "checked"; break;
case "senior": $checks = "checked"; break;

}
//This is where I cant figure out what to put for the if statement

$query="insert into pre_elementary (name, address, city, state, zip, phone, dob, parent, email, pName, pAddress, pCity, pState, pZip, pPhone, pParent, pEmail)
values ('".$name1."','".$ph1."','".$dob1."','".$parentName1."','".$email1."','".$address1."','".$city1."','".$state1."','".$zip1."','".$name2."','".$ph2."','".$dob2."','".$parentName2."','".$email2."','".$address2."','".$city2."','".state2."','".$zip2."'".$pPhone."','".$pParent."','".pEmail."')";
}
//in my code I have copied the above for all 5 radio if statments but I dont know what to write for the if statement

if ($result = $db->query("SELECT * FROM pre_elementary", MYSQLI_USE_RESULT)) {

if (!$db->query("SET @a:='this will not work'")) {
printf("Error: %s\n", $db->error);
}
$result->close();
}
$db->close();
ummhasan
Forum Commoner
Posts: 31
Joined: Sun Jun 13, 2010 7:24 pm
Location: USA

Re: php5 mysqli and radio buttons

Post by ummhasan »

Thanks a lot Jonah, I already have a switch statement as you can see in the long post I posted b4 I saw your reply.

The reason I need 5 tables is because we want to assign the same set of registration numbers to each group.
For example,
pre elementary registratnt 1 will be pre 001 and his/her partner pre 002
elementary registrant 1 will be elem 001 and his/her partner elem 002
and so on.

now on to your example, so if i understand right, I would put the inset into part of my code within each case is that correct?

Thanks again.
Jonah Bron wrote:That's not a extensible solution. Unless each table has different columns, you really should put everything into one table, and just add one column to specify the level. If you really need to have five tables, then you will need a switch statement, vaguely like this:

Code: Select all

switch ($_POST['myradiobutton']) {
    case 'level 1':
        // insert any input for level 1 into it's table
        break;
    case 'level 2':
        // insert any input for level 2 into it's table
        break;
    // ...
}
Post Reply