0

I am trying to update mysql affiliate IDs, I have one update button.

I have something like this.

foreach($_POST['affid'] as $index_id => $value) {
   $key = 'affId_'.$value; 

   $dosql = ("UPDATE dlbprog SET affId='$key' affId =".$value);

   $doupdate = mysql_query($dosql);
}

Any help would be greatly appretiated

I don't quite know how to do this

dlbprog is the database name, affId is the database field, affid is the inputbox name.

There are several inputbox's and only one submit button.

Jesse J.
  • 13
  • 7
  • Well, you definitely need a `WHERE` clause in your statement: `$dosql = ("UPDATE dlbprog SET affId='$key' WHERE affId =".$value);` – Patrick Moore Oct 19 '12 at 18:56
  • Unless you do some basic [SQL escaping](http://bobby-tables.com/php) you are going to get in serious trouble. Is there any reason to not use a simple [ORM](http://stackoverflow.com/questions/108699/good-php-orm-library) to help with simple queries like this? Learning SQL from the ground up is not easy. Having a framework to help get you started is essential. – tadman Oct 19 '12 at 18:56
  • 1
    Can you clarify what you are trying to do? It's unclear to me. What is `$key`? You should stop using `mysql_` functions as they are being [deprecated](http://www.deprecatedphp.com/mysql). Look into `PDO` or `mysqli_` functions. – Kermit Oct 19 '12 at 18:58
  • @SetSailMedia I added where affId = affId and ownerId=$uid – Jesse J. Oct 19 '12 at 19:03
  • @njk I am trying to update multiple affids for however many there are, using one submit button. idk what $key is I could rewrite the whole thing, been trying to splice some things together to see if it works. – Jesse J. Oct 19 '12 at 19:04

1 Answers1

0
<?php

foreach($_POST['affid'] as $id) {

   $id = mysql_real_escape_string(strip_tags($id));
   $aff_id = 'affId_'.$id;

   $sql = "UPDATE dlbprog SET affId='$aff_id' WHERE affId='$aff_id'";

   mysql_query($sql);
}
?>

This works with checkbox or multiple select form tags. But if you are just dealing with just one affiliate ID, no need for loop. ALso check your name attribute if it mismatch with $_POST['affid']

Make sure it is a checkbox and its name attribute is name="affid[]" not name="affid"

Johndave Decano
  • 2,101
  • 2
  • 16
  • 16
  • Invalid arguments supplied foreach. There are multiple affiliate IDs in different boxes (say for instance I want to update 3 programs) – Jesse J. Oct 19 '12 at 19:08
  • '.$row["description"].' To Sign Up Click Here
     Block this programYour Affiliate Id:  

     

    – Jesse J. Oct 19 '12 at 19:10
  • 1
    If that is the problem $_POST['affid'] is not an array but a string – Johndave Decano Oct 19 '12 at 19:10
  • affid is a string, it is the content from the textbox, class=input type=text – Jesse J. Oct 19 '12 at 19:11
  • THere may only be one, or there could be 200, depends on as many programs that are generated, or rather, viewed – Jesse J. Oct 19 '12 at 19:12
  • Yes $_POST['affid'] is intended to be the content or value of the text box that is supposed to Update the database with. For example if I change value from 829 to 998 – Jesse J. Oct 19 '12 at 19:39
  • If `$id` is a string, you're still going to have a SQL injection vulnerability... it'd be safer just to slap an `(int)` in front of it and forget `mysql_real_escape_string`. – mpen Oct 19 '12 at 19:48
  • Also why affId='$aff_id' affId =$id ? – Jesse J. Oct 19 '12 at 19:57
  • There is no WHERE clause that is why your query doent which record you want to update. – Johndave Decano Oct 19 '12 at 20:00
  • What should the WHERE clause be? I added WHERE ownerId=$uid so that it only replaces the ids that member owns – Jesse J. Oct 19 '12 at 20:01
  • Can you post your mysql schema for the table? – Johndave Decano Oct 19 '12 at 20:02
  • If this is what you mean: id (int11 Auto Increment) programId(int11) description(varchar255) progUrl(varchar255) category(char1) affId(varchar50) ownerId(int25) dlbCat(varchar50) – Jesse J. Oct 19 '12 at 20:07
  • $sql = "UPDATE dlbprog SET affId='$aff_id' WHERE affId='$aff_id'"; – Johndave Decano Oct 19 '12 at 20:15
  • It says "UPDATE dlbprog SET affId=111 WHERE affId=111UPDATE dlbprog SET affId=333 WHERE affId=333" (That is the output) whereas it should say "UPDATE dlbprog SET affId=111 WHERE affId=323UPDATE dlbprog SET affId=333 WHERE affId=424 " – Jesse J. Oct 19 '12 at 20:23
  • In other words it is not replacing the old data. Or rather not getting the old affIds. Also aff_id is not needed, I don't need the affId= at the beginning, just the id number – Jesse J. Oct 19 '12 at 20:24
  • foreach($_POST['affid'] as $id) { $id = @mysql_real_escape_string(strip_tags($id)); $sql = "UPDATE dlbprog SET affId=".$id." WHERE affId=".$oldid; echo $sql; //@mysql_query($sql) or die(mysql_error()); } – Jesse J. Oct 19 '12 at 20:41
  • Notice: Undefined variable: oldid in C:\xampp\htdocs\pdimi\m_dlbuilder.php on line 56 UPDATE dlbprog SET affId=111 WHERE affId= Notice: Undefined variable: oldid in C:\xampp\htdocs\pdimi\m_dlbuilder.php on line 56 UPDATE dlbprog SET affId=222 WHERE affId= – Jesse J. Oct 19 '12 at 20:43
  • first one should read affId=323 and the second one should read affId=424 for the WHERE claus (the old affIds, so it knows what to replace) – Jesse J. Oct 19 '12 at 20:44
  • Anybody know how I can get the old value, so that I may UPDATE WHERE affId = oldvalue? The thing that stumbles me is how it is in a foreach statement. – Jesse J. Oct 19 '12 at 21:11