Jump to content

[SOLVED] PHP MYSQL Array Update Using Where


iwpg

Recommended Posts

This has got me stumped. I am trying to update a table where a record ID = to the post ID with values. No luck at all.

 

This is what I have so far:

 

$commissions = preg_split('/\r\n|\n/', $_POST['commission'], -1, PREG_SPLIT_NO_EMPTY);
$id = preg_split('/\r\n|\n/', $_POST['id'], -1, PREG_SPLIT_NO_EMPTY);
//Iterate through array
foreach ($data as $val) {
	mysql_query("insert into $table (commissions) values ('$commissions') WHERE id ='$id'");
}

 

My form looks like:

<input type="text" name="id" id="id[]" size="7" value="3">

<input type="text" name="commission" id="commission[]" size="15" value="100.00">

 

Any help will be GREATLY appreciated.

This and this page explain how to use arrays with forms in php.

 

Alternatively you can parse the post data yourself, which I did before I realized that php can do it for me :P  But if you do like you are doing, you will only get the LAST item in $_POST, not all of them.  To get all of them you must name your input as "id[]" and "commission[]"

Hmm, well in that situation you have two arrays, and you need to loop through both at one time.  That's a little more complex than a single array.

 

$commission_arr = $_POST['commission'];
$id_arr = $_POST['id'];
if (!is_array($commission_arr) || !is_array($id_arr)) {
  die("Either commission or id wasn't an array!");
}
foreach ($id_arr as $key => $id) {
  $commission = $commission_arr[$key];
  $sql = "insert into $table (commissions) values ('$commission') WHERE id ='$id'";
  mysql_query($sql) or die("Error in $sql: " . mysql_error());
}

 

Here I'm assuming that the indexes for the id and commission arrays will match up, which they should in your case.

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.