We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,539 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Inserting Radio Buttons Data

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.

2
Contributors
4
Replies
2 Hours
Discussion Span
1 Year Ago
Last Updated
5
Views
MeeDeggi
Newbie Poster
3 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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..:)

MeeDeggi
Newbie Poster
3 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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!

Red_Rain
Newbie Poster
22 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

MeeDeggi
Newbie Poster
3 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

Red_Rain
Newbie Poster
22 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0960 seconds using 2.73MB