I need some help with radio button data and how to code it in PHP to insert the data into MySQL.

Here is my Form Code:

<form method="post" action="update.php">

First Name: <br />

<input type="text" name="fname" size="35" /><br />

Last Name: <br />

<input type="text" name="lname" size="35" /><br />

Phone Number: <br />

<input type="text" name="phone" size="12"  /><br />
Company:<br />
<input type="text" name="Company" size="30" /><br />
Email:<br />
<input type="text" name="Email" size="30" /> <br />

<input type="radio" name="field" value="SPM" />Sr. Project Manager<br />
<input type="radio" name="field" value="PM" />Project Manager<br />
<input type="radio" name="field" value="CEST" />Chief Estimator<br />
<input type="radio" name="field" value="EST" />Estimator<br />
<input type="radio" name="field" value="PE" />Project Engineer<br />
<input type="radio" name="field" value="SI" />Superintendent<br />
<input type="radio" name="field" value="OTH" />Other<br />

<input type="submit" value="Submit!" />


</form>

And here is my PHP:

<?php

$con = mysql_connect("server.mysql.com","user","********");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("database", $con);

$sql="INSERT INTO ContactList (ID, fname, lname, phone, Company, Email) VALUES ('NULL', '$_POST[fname]','$_POST[lname]','$_POST[phone]', '$_POST[Company]', '$_POST[Email]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)

?>

I tried a few times already but I have not been successful in using correct code to make the radio buttons work. The table is already built for the data, I just need help with inserting.

This code works--I need to know what to add for the radio buttons.

Thank you in advance.

And if it isn't too much to ask, I also need to know how to retrieve the information based on which radio button was selected. For example selecting "Project Managers" will retrieve all and only those that selected that radio button. Thanks again..:)

I cant tell you how to retrieve the info becuase im looking for the same thing with the DATE/TIME field lol im kinda new to PHP myself but from the looks of it your missing the ELSE statement on line 16. Maybe its not necessary but just in case. And i learned a trick with forms and arrays the other day try using 'field[]' to create an array from the radio buttons. Create a variable to catch the result of that array and then insert it into your form. I hope this helps!

I cant tell you how to retrieve the info becuase im looking for the same thing with the DATE/TIME field lol im kinda new to PHP myself but from the looks of it your missing the ELSE statement on line 16. Maybe its not necessary but just in case. And i learned a trick with forms and arrays the other day try using 'field[]' to create an array from the radio buttons. Create a variable to catch the result of that array and then insert it into your form. I hope this helps!

Thanks Red Rain for your input. I can't say I understood everything you said lol...but I will look up the info you suggested, thanks, hope you get your solution.

Sorry lets try this again lol instead of ....

<input type="radio" name="field" value="SPM" />Sr. Project Manager<br />

Try this

<input type="radio" name="field[]" value="PM" />Project Manager<br />
<input type="radio" name="field[]" value="CEST" />Chief Estimator<br />
<input type="radio" name="field[]" value="EST" />Estimator<br />
<input type="radio" name="field[]" value="PE" />Project Engineer<br />
<input type="radio" name="field[]" value="SI" />Superintendent<br />
<input type="radio" name="field[]" value="OTH" />Other<br />

<input type="submit" value="Submit!" />


</form>

And i would guess your $_POST would catch that here

<?php

$sql="INSERT INTO ContactList (ID, fname, lname, phone, Company, Email) VALUES ('NULL', '$_POST[fname]','$_POST[lname]','$_POST[phone]', '$_POST[Company]', '$_POST[Email]', '[B]$_POST['field']'[/B])";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)

?>

Give that a shot, no guarantees but its worth a try i think

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.