mtvaran Posted November 1, 2010 Share Posted November 1, 2010 could anyone please help me with the code which is i have already displayed data as a multi select list but now i need to select one or more from them and insert into another database table. would be appreciate your help. thanx Link to comment https://forums.phpfreaks.com/topic/217399-insert-data-into-database-from-multi-select-list/ Share on other sites More sharing options...
Pikachu2000 Posted November 1, 2010 Share Posted November 1, 2010 We can only help you with the code if you actually post it . . . Link to comment https://forums.phpfreaks.com/topic/217399-insert-data-into-database-from-multi-select-list/#findComment-1128801 Share on other sites More sharing options...
mtvaran Posted November 1, 2010 Author Share Posted November 1, 2010 <?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 https://forums.phpfreaks.com/topic/217399-insert-data-into-database-from-multi-select-list/#findComment-1128805 Share on other sites More sharing options...
A1SURF.us Posted November 1, 2010 Share Posted November 1, 2010 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 https://forums.phpfreaks.com/topic/217399-insert-data-into-database-from-multi-select-list/#findComment-1128860 Share on other sites More sharing options...
Andy-H Posted November 1, 2010 Share Posted November 1, 2010 <?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 https://forums.phpfreaks.com/topic/217399-insert-data-into-database-from-multi-select-list/#findComment-1128868 Share on other sites More sharing options...
mtvaran Posted November 2, 2010 Author Share Posted November 2, 2010 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 https://forums.phpfreaks.com/topic/217399-insert-data-into-database-from-multi-select-list/#findComment-1129346 Share on other sites More sharing options...
Andy-H Posted November 2, 2010 Share Posted November 2, 2010 <?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 https://forums.phpfreaks.com/topic/217399-insert-data-into-database-from-multi-select-list/#findComment-1129374 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.