View Full Version : if else statements in a php query


hdshngout
08-21-2005, 10:01 PM
ok, heres probably a dumb question, but can you use if..else statements in a query to insert data into a mysql database? if so how do you do it. Like say i have a set of radio buttons and if yes is selected, it will insert a code, and if no is selected, it will insert different data.

also i have a set of drop down boxes and textareas. i want to make it so if a value is selected from the drop down it takes the data from that, and if its blank, it takes the data from the textbox.

please help. :)

Nick

Douglas
08-22-2005, 12:38 AM
ok so you have the form and stuff say the values of the radio buttons were yes and no and the group was choice...


<?
$servername="YOUR SERVERNAME";
$dbusername="YOUR DBUSERNAME";
$dbpassword="YOUR DBPASSWORD";
$dbname="YOUR DBNAME";
mysql_connect("$servername","$dbusername","$dbpassword") or die(mysql_error());
mysql_select_db("$dbname");
$choice=$_POST['choice'];
if ($choice == "yes") {
mysql_query("INSERT INTO table (col1,col2) VALUES('$choice','somethingelse')") or die(mysql_error());
echo "Successful";
}
else {
mysql_query("INSERT INTO table (col1,col2) VALUES('$choice','somethingelse')") or die(mysql_error());
echo "Successful";
}
?>


something like that