webweever Posted May 30, 2009 Share Posted May 30, 2009 My code is below. I'm basically trying to pull the values from the cat_name field and display them in the listbox(this part works fine) then push multiple selections into the DB to the marker_cat field. The selection works but it will only insert one value, not multiple <?php $sql="SELECT * FROM category"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $catname=$row["cat_name"]; //$cat_id=$row["cat_id"]; $options.="<OPTION VALUE=\"$catname\">" .$catname."</option>"; } while (isset($_POST['list']) && is_array($_POST['list'])) { $items = implode(", ", $_POST['list']); } ?> <SELECT NAME=items multiple>Category<?php echo $options?></SELECT> SQL statement: $sql="INSERT INTO markers (name, street, city, state, zip, url, lat, lng, marker_cat) VALUES('$_POST[name]','$_POST[street]','$_POST[city]', '$_POST[state]', '$_POST[zip]', '$_POST[url]', '$_POST[lat]', '$_POST[lng]', '$_POST[items]')"; Anyone have any ideas what I'm doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/160277-solved-listbox-not-working/ Share on other sites More sharing options...
KevinM1 Posted May 30, 2009 Share Posted May 30, 2009 Because of the way PHP's double-quotes work, you can't directly access array values in the manner you're attempting. You need to do one of two things: 1. Leave the value in the string and put curly braces around them. 2. Exit the string and concatenate the values to them. 1: $sql="INSERT INTO markers (name, street, city, state, zip, url, lat, lng, marker_cat) VALUES('{$_POST[name]}','{$_POST[street]}','{$_POST[city]}', '{$_POST[state]}', '{$_POST[zip]}', '{$_POST[url]}', '{$_POST[lat]}', '{$_POST[lng]}', '{$_POST[items]')}"; 2: $sql="INSERT INTO markers (name, street, city, state, zip, url, lat, lng, marker_cat) VALUES('" . $_POST[name] . "','" . $_POST[street] . "','" . $_POST[city] . "', '" . $_POST[state] . "', '" . $_POST[zip] . "', '" . $_POST[url] . "', '" . $_POST[lat] . "', '" . $_POST[lng] . "', '" . $_POST[items] . "')"; If the problem persists, try removing the single-quotes from around the values. Also, you should put single-quotes around your array keys, i.e.: $_POST['city'] Quote Link to comment https://forums.phpfreaks.com/topic/160277-solved-listbox-not-working/#findComment-845851 Share on other sites More sharing options...
webweever Posted May 31, 2009 Author Share Posted May 31, 2009 I've tried quotes, no quotes, single quotes, no single quotes, Brackets, no brackets. About a 1000 different combinations and still can't get it to work. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/160277-solved-listbox-not-working/#findComment-846161 Share on other sites More sharing options...
anupamsaha Posted May 31, 2009 Share Posted May 31, 2009 I've tried quotes, no quotes, single quotes, no single quotes, Brackets, no brackets. About a 1000 different combinations and still can't get it to work. Any other ideas? Change this: <SELECT NAME=items multiple>Category<?php echo $options?></SELECT> To <SELECT NAME="list[]" multiple>Category<?php echo $options?></SELECT> The above HTML will enable PHP to collect $list as an Array in the POST. Your code will work now. Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/160277-solved-listbox-not-working/#findComment-846263 Share on other sites More sharing options...
webweever Posted June 1, 2009 Author Share Posted June 1, 2009 When I use <SELECT NAME="list[]" multiple>Category<?php echo $options?></SELECT> all it puts in the DB is the word Array not the actual selected values of the Listbox. Quote Link to comment https://forums.phpfreaks.com/topic/160277-solved-listbox-not-working/#findComment-846663 Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 When I use <SELECT NAME="list[]" multiple>Category<?php echo $options?></SELECT> all it puts in the DB is the word Array not the actual selected values of the Listbox. Please change '$_POST[items]' To: $items In the INSERT query. Quote Link to comment https://forums.phpfreaks.com/topic/160277-solved-listbox-not-working/#findComment-846671 Share on other sites More sharing options...
webweever Posted June 1, 2009 Author Share Posted June 1, 2009 Still doesnt work, here's what I have: $sql="INSERT INTO markers (name, street, city, state, zip, url, lat, lng, marker_cat) VALUES('$_POST[name]','$_POST[street]','$_POST[city]', '$_POST[state]', '$_POST[zip]', '$_POST[url]', '$_POST[lat]', '$_POST[lng]', $items)"; <?php $sql="SELECT * FROM category"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $catname=$row['cat_name']; //$cat_id=$row["cat_id"]; $options.="<OPTION VALUE=\"$catname\">$catname</option>"; } while (isset($_POST['list']) && is_array($_POST['list'])) { $items = implode(", ", $_POST['list']); } ?> <SELECT NAME=items multiple >Category<?php echo $options?></SELECT> I get this error: Notice: Undefined variable: items in C:\wamp\www\_maps\admin\insert.php on line 100 Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 2 Quote Link to comment https://forums.phpfreaks.com/topic/160277-solved-listbox-not-working/#findComment-846686 Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 Please try this. <?php $sql="SELECT * FROM category"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $catname=$row['cat_name']; //$cat_id=$row["cat_id"]; $options.="<OPTION VALUE=\"$catname\">$catname</option>"; } if (isset($_POST['list']) && is_array($_POST['list'])) { $items = implode(", ", $_POST['list']); } ?> Category: <SELECT NAME="list[]" multiple><?php echo $options?></SELECT> And, the INSERT statement will be: $sql="INSERT INTO `markers` (`name`, `street`, `city`, `state`, `zip`, `url`, `lat`, `lng`, `marker_cat`) VALUES ('$_POST[name]', '$_POST[street]', '$_POST[city]', '$_POST[state]', '$_POST[zip]', '$_POST[url]', '$_POST[lat]', '$_POST[lng]', '$items')"; It should work Quote Link to comment https://forums.phpfreaks.com/topic/160277-solved-listbox-not-working/#findComment-846688 Share on other sites More sharing options...
webweever Posted June 1, 2009 Author Share Posted June 1, 2009 Now I get Notice: Undefined variable: items in C:\wamp\www\_maps\admin\insert.php on line 100 Line 100 is the SQL insert statement. Quote Link to comment https://forums.phpfreaks.com/topic/160277-solved-listbox-not-working/#findComment-846690 Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 Now I get Notice: Undefined variable: items in C:\wamp\www\_maps\admin\insert.php on line 100 Line 100 is the SQL insert statement. Please provide full script here. Some other part of the script generating this error. Quote Link to comment https://forums.phpfreaks.com/topic/160277-solved-listbox-not-working/#findComment-846693 Share on other sites More sharing options...
webweever Posted June 1, 2009 Author Share Posted June 1, 2009 This is the entire insert script. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"> <!--<META HTTP-EQUIV="Refresh" CONTENT="2; URL=admin.php">--> <title>Insert Markers</title> </head> <body> <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("maps", $con); $sql="INSERT INTO `markers` (`name`, `street`, `city`, `state`, `zip`, `url`, `lat`, `lng`, `marker_cat`) VALUES ('$_POST[name]', '$_POST[street]', '$_POST[city]', '$_POST[state]', '$_POST[zip]', '$_POST[url]', '$_POST[lat]', '$_POST[lng]', '$items')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "<font face=\"verdana\" size=\"2\"><b><center>Marker Added</center></b></font>"; mysql_close($con) ?> </body> </html> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160277-solved-listbox-not-working/#findComment-846698 Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 Try this code: <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("maps", $con) or die('Failed to select database'); $sql = "SELECT * FROM category"; $result = mysql_query($sql); $options = ""; while ($row = mysql_fetch_array($result)) { $catname = $row['cat_name']; //$cat_id=$row["cat_id"]; $options .= "<OPTION VALUE=\"$catname\">$catname</option>"; } if (isset($_POST['list']) && is_array($_POST['list'])) { $items = implode(", ", $_POST['list']); $sql="INSERT INTO `markers` (`name`, `street`, `city`, `state`, `zip`, `url`, `lat`, `lng`, `marker_cat`) VALUES ('$_POST[name]', '$_POST[street]', '$_POST[city]', '$_POST[state]', '$_POST[zip]', '$_POST[url]', '$_POST[lat]', '$_POST[lng]', '$items')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } $message = "<font face=\"verdana\" size=\"2\"><b><center>Marker Added</center></b></font>"; mysql_close($con); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"> <!--<META HTTP-EQUIV="Refresh" CONTENT="2; URL=admin.php">--> <title>Insert Markers</title> </head> <body> <?php if (isset($message)) { echo "<p>" . $message . "</p>"; } ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']?>"> Category: <SELECT NAME="list[]" multiple><?php echo $options?></SELECT> <br> <input type="submit" name="btnSubmit" value="Submit"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/160277-solved-listbox-not-working/#findComment-846704 Share on other sites More sharing options...
webweever Posted June 1, 2009 Author Share Posted June 1, 2009 That worked anupamsaha, thanks for your help I really appreciate it. Now I have to decipher what you changed so I don't have to ask this question again. Quote Link to comment https://forums.phpfreaks.com/topic/160277-solved-listbox-not-working/#findComment-846713 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.