Help with Arrays



I’m fairly new to php and still am trying to grasp the concept of an array. What I’m trying to do is create several mysql query’s that will insert data based on user input from a form.

In this case I am letting them select multiple states:


State(s): *

<?php $query2 = mysql_query("SELECT name,short FROM states") or die(myql_error()); while($s = mysql_fetch_array($query2)){ $name = $s['name']; $short = $s['short']; ?>

: <?=$name;?>

<? } ?> [/code]

Once they submit this information it will save their id as well as the states they have selected. However, I can’t get it to work right. The state value remains empty once it is inserted into the database.

I’ve looked high and low for a solution to this, but can’t seem to piece it together. Is there anyone that can help me?


Well, after a few hours of being away from the computer I came back and figured it out.

Here’s what I did for anyone out there looking to do something similar:

Starting with a multiple select I pulled values from my states table in my database. And associated the input with the state array by using “name=state[]”.

<p><label>State(s): *</label><br />
<select multiple name="state[]">
$query2 = mysql_query("SELECT name,short FROM states") or die(myql_error());
while($row = mysql_fetch_array($query2)){
<option value="<?=$row['short'];?>"><?php print $row['name'].' ('.$row['short'].')'; ?></option>
<? } ?>

When the form is posted I did this:

$state = $_POST['state'];
  foreach($state as $key => $value){
      mysql_query("INSERT INTO partner_filters (id, active_flag, partid, product, cpl, min_loan_amount, min_p_price, min_p_value, max_ltv, state, ex_credit, go_credit, fa_credit, po_credit) VALUES ('', '$active', '$partid', '$product', '$cpl', '$minloan', '$minpprice', '$minpval', '$maxltv', '$value', '$excellent', '$good', '$fair', '$poor')") or die(mysql_error());

This will insert a new row into the database partner_filters for each selected state on the form.


Okay, so I figured out how to store the data from the first form in the database in multiple rows.

Now, I would like to have the option of selecting multiple states from a form and viewing the selected states information.

When I first started trying this I was just getting the last state selected in the multiple selection box. I started toying around with implode and came up with this:

$state = implode(",",$_POST['state']); // Line 15
$state = str_replace(",","','",$state);

  $query = mysql_query("SELECT * FROM `partner_filters` WHERE `product` = '$product' AND `partid` = '$partner' AND `state` IN ('$state') ORDER BY state") or die(mysql_error());

This I thought for sure would work, but it gives me the following error:

PHP Warning:  implode() [<a href='function.implode'>function.implode</a>]: Bad arguments. in [site] on line 15

Someone’s help would be so greatly appreciated in fixing this. I’ve never seen this kind of error before.

Thanks in advance!


What’s the value of $_POST[‘state’]?
Try this code: print_r($_POST[‘state’]);

Also, watch out for SQL Injection, using a ‘user input’ directly into a SQL query could pose a serious security issue.