<form method="POST">


Color: <select name="Color">
<option> </option>
<option name="red"> Red</option>
<option name="royal"> Royal Blue</option>
<option name="black"> Black</option>
<option name="white"> White</option>
<option name="yellow"> Yellow</option>
<option name="navy"> Navy</option>
<option name="green"> Green</option>
<option name="gray"> Gray</option>
<option name="blue"> Blue</option>
    </select><br>


<input type="Submit" name="Submit"value="Save">

<?php        
   if (isset($_POST['Submit']))
{      $con=mysqli_connect("localhost","root","","inventory");
$result = mysqli_query($con,"SELECT * FROM color");
    $red=$_POST['red'];
    $royal=$_POST['royal'];
    $black=$_POST['black'];
    $white=$_POST['white'];
    $yellow=$_POST['yellow'];
    $navy=$_POST['navy'];
    $green=$_POST['green'];
    $gray=$_POST['gray'];
    $blue=$_POST['blue'];
mysql_query("INSERT INTO `inventory`.`color`(red,royal,black,white,yellow,navy,green,gray,blue)
     VALUES ('$red','$royal','$black','$white','$yellow','$navy','$green','$gray','$blue')");


                        }?>

Dani AI

Generated

Quick diagnosis and direction (short): the form uses option attributes incorrectly and the PHP reads keys that are never posted. The connection code mixes APIs and the INSERT strategy (one DB column per color) is fragile. As noted, multiple selections must be handled as an array; as asked, first decide whether you want to store a color name or a color id — prefer a normalized design.

Example: single-choice — use a value on each option and insert one value with a prepared statement.

<form method="post">
  <select name="color">
    <option value="">Choose a color</option>
    <option value="red">Red</option>
    <option value="royal">Royal Blue</option>
    <option value="black">Black</option>
  </select>

  <input type="submit" name="save" value="Save">
</form>
<?php
$mysqli = new mysqli('localhost','root','','inventory');
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['color'])) {
    $color = $_POST['color'];              // validate this value!
    $stmt = $mysqli->prepare('INSERT INTO selected_colors (color_name) VALUES (?)');
    $stmt->bind_param('s', $color);
    $stmt->execute();
    $stmt->close();
}
?>

If you need multiple colors per item, normalize the schema (colors table + pivot table) and insert one row per selection inside a transaction. Validate submitted values against the canonical list (or use color IDs) to prevent invalid data and SQL injection. Troubleshooting tips: inspect var_dump($_POST) to see what was posted, avoid option name="..." (use value), make the select name match what PHP reads, and stop mixing mysql_* with mysqli/PDO — use prepared statements for safety.

Recommended Answers

All 2 Replies

That will not work.. you will need to set the select multiple property and the name attribute to some array.

for example

<select name="color[]" multiple>
<option value="red">
<!-- add more options here -->

</select>

for the php processor, you need to iterate throught the color array(), by using foreach..

example 2

if(isset($_POST['submit']){
foreach($_POST['color'] as $colors){

    echo $colors.'<br/>';
    }
    }

Just to remind you that I am only using the echo for demonstration. YOu can easily pass the submitted value for your database query.

The above codes shown is for multiple selection of colors. For single color selection, remove the array designator.

Member Avatar for Member #120589

What values are you expecting to insert into the DB? From this it looks like you're inserting the literal colour name into, so if you echoed out your sql and if all values were chosen in the 'multiple' select box...

INSERT INTO `inventory`.`color`(red,royal,black,white,yellow,navy,green,gray,blue)
     VALUES ('red','royal','black','white','yellow','navy','green','gray','$blue')

Is that right?
Perhaps if you give an idea of what you're trying to do. What does this data actually mean?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.