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.

Link to comment
Share on other sites

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[]"

Link to comment
Share on other sites

Thanks for your help. I looked everywhere for update query examples on arrays in MYSQL, but didn't have any luck. Do I need to loop the $id variable through the array like the $commissions variable? I'm just kindof stuck with this part.  ???

Link to comment
Share on other sites

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.

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.