0

I'm a newbie in PHP, I have a dynamic textbox which looks like these

name="name1"
name="name2"
name="name3"

the number of fields depends on how many user wants but the problem is on the SQL script, I need to put the $ctr besides "$_POST[name]" in order for the compiler see all my fields.

for ( $ctr = 1; $ctr <= 10; $ctr += 1) 
    {
    $sql = "INSERT INTO siblings (name) VALUES ('$_POST[name]')";    
    }​

the problem is how to place the variable besides "$_POST[name]"

Thank you Guys!

- joel

Edited by peter_budo: Keep It Clear - Please use code tags when posting any code

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by diafol
0

you can't use such model as

name="name1"
name="name2"
name="name3"

you can use array

name[]="name1"
name[]="name2"
name[]="name3"

for name of your textbox set name="name[]" after that in post it will come over as array


Know that one string as name cannot be equal to 3 different values at the same time. Only possible way is to use array.
And after that into the POST you will get this array value.

Edited by sv3tli0: n/a

0

sv3tli0 has it on the array.
When you come to use the SQL, build the VALUES from - something like this (not tested):

function clean($array) {
    return array_map('mysql_real_escape_string', $array);
}
$names = clean($_POST['name']); 
$nstr = implode("'),('",$names);
$sql = "INSERT INTO siblings (name) VALUES ('$nstr')";
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.