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

Link to comment
Share on other sites

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!

 

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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