Hack Forums
nsert multiple select value using PHP and MYSQL - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5)
+---- Thread: nsert multiple select value using PHP and MYSQL (/showthread.php?tid=6092926)



nsert multiple select value using PHP and MYSQL - blackcoder00 - Jul 6, 2020

hey guys i found a problem when i wanna insert multiple values of a select tag in database what is wrong in my code ? i follow the instruction one by one in some book
PHP Code

<?php


$servername 
= "localhost";
$username = "root";
$password = "test00";
$dbname = "test";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die(
"Connection failed: " . mysqli_connect_error());
}
 
$selectop = $_POST['cars'];

if(
$selectop){
    foreach (
$selectop as $key) {
        
    
  
mysqli_query ($conn,"INSERT INTO list VALUES ('$key','')");
}
echo 
"New record created successfully";
}
 else {
  echo 
"_error_: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn); 
PHP Code

<?php 
<div class="container">
<
form action="php/insert-coffe.php">
  <
div  class="containers">
  <
label>Non produit :</label><input class="item" type="text">
  <
label> Prix Produit :</label><input class="item" type="text">
   </
div>

  <
div class="">
  <
label>select zebi</label>
  <
select name="cars" id="cars" multiple size="3">
    <
option value="volvo">Volvo</option>
    <
option value="saab">Saab</option>
    <
option value="opel">Opel</option>
    <
option value="audi">Audi</option>
  </
select>
  <
button>Sell</button>
</
div>
</
form>
</
div>