Jump to content

Check Box to store into MySQL database


phpapprentice

Recommended Posts

I am a beginner in PHP. Need Help...

The form is working fine but the check box is not, when I choose steak, it will add steak into the FOOD field.

when I choose steak and pizza, it will add only pizza.

I want to add both steak and pizza into one field separated by comma.

 

 

 

this is my form

 

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

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

<table cellspacing="0" cellpadding="2" width="98%" border="0">

<tbody><tr><td>Firstname:</td><td><input name="firstname" /></td></tr>

<tr><td>Lastname:</td><td><input name="lastname" /></td></tr>

<tr><td>Age:</td><td><input name="age" /> </td></tr>

<tr><td> </td><td><input type="submit" /> </td></tr></tbody></table>

<p /><p /><p /><pre> </pre><pre>Gender: Male<input type="radio" name="gender"value="male" />

Female<input type="radio" name="gender" value="female" /></pre><br />

Please choose type of residence::<br />Steak:<input type="checkbox" name="food value="steak"/>:<br />

Pizza:<input type="checkbox" name="food" value="pizza"/>:<br />

Chicken:<input type="checkbox" name="food" />:<br /></form>

 

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

 

this is my insert.php

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

<?php

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

if (!$con)

  {

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

  }mysql_select_db("noyce", $con);

 

  $sql="INSERT INTO Persons (FirstName, LastName, Age, gender, food)

VALUES

('$_POST[firstname]','$_POST[lastname]','$_POST[age]','$_POST[gender]','$_POST[food]')";if (!mysql_query($sql,$con))

  {

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

  }

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

?>

 

 

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

my database - SELECT

 

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

 

mysql> select * from Persons;

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

| personID | FirstName | LastName | Age  | gender | food    |

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

|        1 | Rommelito | Santos  |  29 | NULL  | NULL    |

|        2 |              |            |    0 | NULL  | NULL    |

|        3 | Mike        |            |  29 | NULL  | NULL    |

|        4 | Alvin        | Garcia    |  20 | NULL  | NULL    |

|        5 | John        | Berry    |  19 | NULL  | NULL    |

|        6 | Jessica    | Alba      |  30 |        | NULL    |

|        7 | Sharon    | Stone    |  40 | on    | NULL    |

|        8 | Robin      | Padilla    |  45 | on    | NULL    |

|        9 | William    | Martinez  |  45 | on    | on      |

|      10 | Jessa      | Zaragosa |  40 | female | on      |

|      11 | Hanna      | Montana  |  15 | male  | pizza  |

|      12 | Bon        | Jovi        |  40 | male  | steak  |

|      13 | Vilma      | Santos    |  50 | female | chicken |

|

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

13 rows in set (0.01 sec)

 

 

Link to comment
Share on other sites

where will i put this code?

$food = $_POST['food'];

I mark it red in the code.

 

 

Do i have to add it in my insert.php

 

my INSERT.PHP

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

<?php

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

if (!$con)

  {

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

  }mysql_select_db("noyce", $con);

$food = $_POST['food'];

  $sql="INSERT INTO Persons (FirstName, LastName, Age, gender, food)

VALUES

('$_POST[firstname]','$_POST[lastname]','$_POST[age]','$_POST[gender]','$_POST[food]')";if (!mysql_query($sql,$con))

  {

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

  }

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

?>

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

Link to comment
Share on other sites

using barand's code use it like this:

 

<?php
$con = mysql_connect("localhost","mydatabase","mypassword");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("noyce", $con);
[color=red]$food = join (', ', $_POST['food']); [/color]
  $sql="INSERT INTO Persons (FirstName, LastName, Age, gender, food)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]','$_POST[gender]','[color=red]$food[/color]')";if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";mysql_close($con)
?>

Link to comment
Share on other sites

sorry use it like this (forget the tags)

 

<?php
$con = mysql_connect("localhost","mydatabase","mypassword");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("noyce", $con);
$food = join (', ', $_POST['food']);
  $sql="INSERT INTO Persons (FirstName, LastName, Age, gender, food)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]','$_POST[gender]','$food')";if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";mysql_close($con)
?>

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.