tags:

views:

121

answers:

4

I am new to php.

I want to have a form such as this:

 Name   <TextField>   Corresponding pic:  <dropdown> hiddenID (not shown to user)
 Name   <TextField>   Corresponding pic:  <dropdown> hiddenID (not shown to user)
 Name   <TextField>   Corresponding pic:  <dropdown> hiddenID (not shown to user)
 Name   <TextField>   Corresponding pic:  <dropdown> hiddenID (not shown to user)
 Name   <TextField>   Corresponding pic:  <dropdown> hiddenID (not shown to user)

 Update Values (button)

After the button is clicked (form submitted) how will I get all those values. I want to update a database table for each of those values. query would be like

update user_tables name=<newnameSubmitted>, pic=<valueFromDropDown> 
where id=<hiddenIdGottenBack>

In this example I have 5 values...how will I be able to run the above update statement 5 times with corresponding values.

Please clear my confusion or guide me to a tutorial that will help me out a little.

A: 

In php all submitted values are stored in the $_POST array, $_POST['name'], etc. (supposing you are using post and not get), so you will have to loop through all values in that array.

jeroen
+2  A: 
<?
  $cmd=$_POST["cmd"];

  if(isset($cmd)) {
    $name=array_map('mysql_escape_string', $_POST["name"]);
    $sel=array_map('mysql_escape_string', $_POST["sel"]);
    for($i=0;$i<count($name);$i++) {
          $sql="update TABLENAME set col1='{$name[$i]}' where col2='{$sel[$i]}'";
    }

?>

<form method="post" action="sample.php">
Value1 <input type="text" name="name[]"/> <select name="sel[]">...</select>
Value1 <input type="text" name="name[]"/> <select name="sel[]">...</select>
Value1 <input type="text" name="name[]"/> <select name="sel[]">...</select>
Value1 <input type="text" name="name[]"/> <select name="sel[]">...</select>
Value1 <input type="text" name="name[]"/> <select name="sel[]">...</select>
<input type="submit" name="cmd" value="Submit"/>
</form>
adatapost
SQL INJECTION!Don't ever ever ever (not even in code examples online) put values from the browser directly into SQL.
JasonWoof
The important thing to take away from this example is the use of [] in the HTML to get back an array of values. But yes, escape your inputs :)
Mark Biek
ahh, thanks for encoding, but mysql_escape_string() is deprecated in favor of mysql_real_escape_string()
JasonWoof
adatapost
A: 

I like to keep things simple, so I just give my form fields predictable names, like so:

<input name="name1" ...> <input name="pic1" ...>
<input name="name2" ...> <input name="pic2" ...>

Then in php, I process them in a loop:

for($i = 0; $i < 5; ++$i) {
   if(isset($_REQUEST["name$i"]) && isset($_REQUEST["pic$i"])) {
      # update database with those values
   }
}
JasonWoof
The only problem I have with this approach is that you have to modify your code if the number of inputs ever changes.
Mark Biek
well, when I've done this sort of thing, I've generated the list of inputs in php, so it's php already knows how many fields there are. If the number of inputs is dynamic on the client end (ie there's a javascript button to make another one) it might make more sense to do it a little differently. though if they are always consecutive you could just for($i = 0; isset($_REQUEST["name$i"]); ++$i)
JasonWoof
A: 
  1. Learn to use arrays.
  2. Learn to read arrays format

    echo '

    '; print_r($_POST); echo '
    ';

  3. Understand that $_POST is an array.

  4. extract - is an function when used on an array, it makes variables with arrays key as variable name, and the value assigned to that key as the variable's value.

    extract($_POST);

  5. For security reasons, you should use mysql_prepare , or $mysqli->prepare.

I think it's a very bad idea to call extract() on $_POST. Only if you're very very careful, and don't mess up will this not result in a security problem.This is basically like register_globals.
JasonWoof