Jump to content

Inserting multiple rows into mysql with PHP checkboxs


wabash

Recommended Posts

Hi there,

 

I am trying to insert selections from check boxes into new rows of a mysql table. The idea is to submit a new plant to a database and include it's various flower colors. For example if a plant is submitted with three plant colors from the check box selections, three new rows in the database would be created for that plant.

 

 

plantcolorlinktable

 

plant_id color_id

1   14

1        15

2        11

2        15 

2        16

 

My html code:

<html>
<head>
<title>Add Plant to Database</title>
</head>

<body>
<form method="post" action="addPlant.php">
<b><big>Plant Insert Admin tool: </b></big><br /><br />
<b> General Plant Info </b> <br />
Enter Plant's Scientific Name: <br />
<input type ="text" name="scientific" size"30" /><br />

Plant Common Name: <br />
<input type ="text" name="common" size"30" /><br />

Plant Family Name: <br />
<input type ="text" name="family" size"30" /><br />

Plant Genus Name: <br />
<input type ="text" name="genus" size"30" /><br />

<b> Plant Flowering Colors: </b> <br />
Red:<input type="checkbox" name="color[]" value="11" >:<br />
Yellow:<input type="checkbox" name="color[]" value="14">:<br />
White:<input type="checkbox" name="color[]" value="15">:<br />
<br />

<input type ="submit" value="Update Database" />

</form>
</body>
</html>

 

And my php code:

<?php    

$scientific = $_POST['scientific'];
$common = $_POST['common'];
$family = $_POST['family'];
$genus = $_POST['genus'];
$color=$_POST['color'];  //?????

//connect to db
require_once 'login.php';

$db_server = mysql_connect($db_hostname,$db_username,$db_password);
if (!$db_server) die("Unable to connect to MySQL:" . mysql_error());

mysql_select_db($db_database)
    or die("Unable to select database:" .mysql_error());

//insert plant data into db

$queryplant="INSERT INTO actualplanttable (id, scientific, common, family, genus) 
VALUES ('NULL', '$_POST[scientific]', '$_POST[common]', '$_POST[family]', '$_POST[genus]')";

mysql_query($queryplant) 
or die ('Error updating actualplanttable');

// finding last Id inserted
$inserted_id = mysql_insert_id();

//insert color data into plantcolorlinktable......I'm not sure  what to do here with multiple selections from checkboxes??

$queryid="INSERT INTO plantcolorlinktable (plant_id, color_id) 
VALUES ('$inserted_id','$color' )";

mysql_query($queryid) 
or die ('Error updating the plant_id in plantcolorlinktable');


echo "Database Updated With: " .$scientific. " ".$common. " ".$family. " ".$genus. " ".$description;

?>

 

As you can see I'm not sure what to do when it comes to inserting the check box section in my php code :)  Any help would be greatly appreciated.

 

Thanks!

 

Bill....

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.