Jump to content

Archived

This topic is now archived and is closed to further replies.

phpapprentice

[SOLVED] radio buttons to store data in mysql database

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

 

Share this post


Link to post
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

Share this post


Link to post
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)

?>

Share this post


Link to post
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.

Share this post


Link to post
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]')";

Share this post


Link to post
Share on other sites

I guess that's just how it returns it, or that's how MySQL does Boolean. I don't use MySQL very often, so I'm not sure. In any case, you should be able to just convert it on extraction.

Share this post


Link to post
Share on other sites

×
×
  • 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.