Jump to content

[SOLVED] Listbox not working


webweever

Recommended Posts

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?

 

Link to comment
https://forums.phpfreaks.com/topic/160277-solved-listbox-not-working/
Share on other sites

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']

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!

 

 

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

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 :)

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
}
?>

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>

 

Archived

This topic is now archived and is closed to further replies.

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