Jump to content

[SOLVED] radio buttons to store data in mysql database


phpapprentice

Recommended Posts

Please Help...

 

How can I store data from radio button into mysql database?

I want to store TRUE or FALSE into field1(IHE Personnel), field2(K-12 Personnel) , field3(Scholar).

If IHE Personnel is selected, i want TRUE to be stored in field1, and it will store FALSE for both field2 and field3.

 

This is my form

**********************************************

Who are you?

<input type="radio" name="personnel" value="true" />IHE Personnel

<input type="radio" name="personnel" value="true" /> K-12 Personnel

<input type="radio" name="personnel" value="true" /> Scholar

**********************************************

 

 

phpapprentice

 

Link to comment
Share on other sites

You will need logic before you insert/update the DB table. This logic should assign all variables to FALSE, except the one that was selected. So, you'll need to change the values of each radio button to the field for each selection.

 

Like this:

<?php
$fields['field1'] = false;
$fields['field2'] = false;
$fields['field3'] = false;
$fields[$_POST['personnel']] = true;
// do whatever with your DB...
?>

 

Then radio buttons:

<input type="radio" name="personnel" value="field1" />IHE Personnel
<input type="radio" name="personnel" value="field2" /> K-12 Personnel
<input type="radio" name="personnel" value="field3" /> Scholar

Link to comment
Share on other sites

this is my insert.php. there is an error.

Error: Column count doesn't match value count at row 1

****************************************************

<?php

$con = mysql_connect("localhost","mydatabase","mypassword");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }mysql_select_db("noyce", $con);

 

 

$fields['field1'] = false;

$fields['field2'] = false;

$fields['field3'] = false;

$fields[$_POST['personnel']] = true;

 

 

 

  $sql="INSERT INTO people (FirstName, LastName, field1, field2, field3)

VALUES

('$_POST[FirstName]','$_POST[LastName]',$fields)";if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

echo "1 record added";mysql_close($con)

?>

Link to comment
Share on other sites

That's because SQL doesn't know PHP. You're trying to insert a raw array.

 

Do this:

<?php
$sql="INSERT INTO people (FirstName, LastName, field1, field2, field3) VALUES ('{$_POST['FirstName']}', '{$_POST['LastName']}', {$fields['field1']}, {$fields['field2']}, {$fields['field3']})";
?>

Also, I corrected a little of the syntax.

Link to comment
Share on other sites

thank you very much.

it works.

It stores 0 and 1,

How can I make those 0 and 1 to FALSE and TRUE

 

mysql> select * from people;

+----+----------+-----------+--------+--------+--------+

| id | LastName | FirstName | field1 | field2 | field3 |

+----+----------+-----------+--------+--------+--------+

|  1 | Smith    | Will        |  NULL |  NULL |  NULL |

|  2 |            |              |  NULL |  NULL |  NULL |

|  3 | Poe      | Fernando  |  NULL |  NULL |  NULL |

|  4 | Moore  | Stacey    |      1 |      0  |      0  |

+----+----------+-----------+--------+--------+--------+

 

 

 

 

 

this is the code that works(just the brackets, i removed them.)

************************************************************

$sql="INSERT INTO people (FirstName, LastName, field1, field2, field3) VALUES ('$_POST[FirstName]','$_POST[LastName]','$fields[field1]', '$fields[field2]', '$fields[field3]')";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.