Jump to content

insert data into database from multi select list


mtvaran
 Share

Recommended Posts

<?php

 

 

$con = mysql_connect("localhost","root","");

if (!$con)

  {

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

  }

  mysql_select_db("my_DB_name", $con);

 

 

$result  = mysql_query("SELECT * FROM buy");

 

echo "<select multiple='multiple'>";

 

while($row = mysql_fetch_array($result))

{

echo "<option value=''>";

echo $row['ID'];

 

  echo "</option>";

 

 

 

}

echo "</select>";

mysql_close($con);

 

?>

this is the code im using to display data as select list. but i need a code for select data from that and insert ito another table.

Link to comment
Share on other sites

Each form option needs to have a separate value the text in between the options are just for show:

 

<form action="purchase.php">
<select name="form options">
<option value="1">Cars $1,000</option>
<option value="2">Boats $15,000</option>
<option value="3">Motorcycles $100,000</option>
<option value="4">Airplanes $10,000</option>
</select>

 

 

Now for the PHP you need something simple like this:

 

<b>purchase.php</b>


// include "config.php";//
// include "data.php";//


$con = mysql_connect("localhost","user","database","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("my_DB_name", $con);


if ($_POST) {

// post data

$cars = $_POST["cars"];
$boats = $_POST["boats"];
$airplanes = $_POST["airplanes"];
$motorcycles = $_POST["motocycles"];


// secure data

$cars = uc($cars);
$boats = uc($boats);
$airplanes = uc($airplanes);
$motorcycles = securedata($motorcycles);

}   //   I might have gotten parenthesis happy.
}   //   These two lines might not be needed.

// insert data into database

$query = "INSERT INTO table_purchase (cars, boats, motorcycles, airplanes) VALUES('$cars','$boats','$motorcycles','$airplanes')";
mysql_query($query) or die(mysql_error());

echo 'Thanks for your purchase!';
showFooter();
exit();


}   //   I might have gotten parenthesis happy.
}   //   These two lines might not be needed.

?>
<form action="purchase.php" method="POST">

<select name="select">
<option value="<?=$_POST["cars"];?>">Cars $1,000</option>
<option value="<?=$_POST["boats"];?>">Boats $15,000</option>
<option value="<?=$_POST["motorcycles"];?>">Motorcycles $100,000</option>
<option value="<?=$_POST["airplanes"];?>">Airplanes $10,000</option>
</select>
<select name="select">
<option value="<?=$_POST["cars"];?>">Cars $1,000</option>
<option value="<?=$_POST["boats"];?>">Boats $15,000</option>
<option value="<?=$_POST["motorcycles"];?>">Motorcycles $100,000</option>
<option value="<?=$_POST["airplanes"];?>">Airplanes $10,000</option>
</select>
<select name="select">
<option value="<?=$_POST["cars"];?>">Cars $1,000</option>
<option value="<?=$_POST["boats"];?>">Boats $15,000</option>
<option value="<?=$_POST["motorcycles"];?>">Motorcycles $100,000</option>
<option value="<?=$_POST["airplanes"];?>">Airplanes $10,000</option>
</select>
<button name="submit">SUBMIT</button>
</form>

 

 

I'm not sure how to pull data and set it up in a form by row, I've never done it. But you can create individual selections and options. Hopefully this helps you. The form should post to this same (class) page, without error. I'm not a pro coder but I think this should work.

Link to comment
Share on other sites

<?php
//connect to database

if (!empty($_POST['postArrayKey']))
{
   $values = array_map("mysql_real_escape_string", $_POST['postArrayKey']);
   $values = implode("', '", $values);

   $query  = "INSERT INTO myTable VALUES ( '" . $values . "' )";
   $result = mysql_query($query)or trigger_error(mysql_error(), E_USER_ERROR);

   if ($result)
      echo 'Database sucessfully updated!';
}
?>
<form action="<?php echo stripslashes(htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES)); ?>" method="POST">
   <select name="postArrayKey[]" multiple="multiple">
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
      <option value="4">Four</option>
      <option value="5">Five</option>
   </select>
</form>

 

array_map

mysql_real_escape_string

implode

trigger_error

Link to comment
Share on other sites

sorry guys, I COULD NOT GET IT. i know im bit stupid. all i want is i have already displayed data from two table one is drop-down list another one multi select list, now i need to select one data from drop-down list and one or more data from multi select list then send to another table. hope i made this clear for you guys. plss if you can help me...

Link to comment
Share on other sites

<?php
//connect to database
$query  = "SELECT * FROM buy ORDER BY ID DESC";
$result = mysql_query($query)or trigger_error(mysql_error(), E_USER_ERROR);
?>
<form action="<?php echo stripslashes(htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES)); ?>" method="POST">
   <select name="postArrayKey[]" multiple="multiple">
<?php // posted data will be stored in an array ($_POST['postArrayKey'])
   while ($row = mysql_fetch_assoc($result))
   {
      echo "\t\t" . '<option value="' . $row['ID'] . '">' . $row['ID'] . '</option>' . "\r\n";
   }
?>
   </select>
</form>

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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