I have a field where i know for sure the entries would be something like Edward, John, Jacob in a input field.
Now, i am trying to insert a new row for each of the people. I am doing this:
$person = mysql_real_escape_string($_POST['person']);
foreach($person_array as $value)
{
$insert = "INSERT INTO people (Person)
VALUES ('".$value."')";
$query = mysql_query($insert) or die ("Error: ".mysql_error());
}
Using the above query i am unable to insert into the table. What am i doing wrong?
If $_POST['person'] is indeed a value as such : "John, Edward, Jacob" then it's just a string; you'll need to convert it into an array with explode. Also, it should be $_POST not $POST.
$person = explode(',', $_POST['person']);
Then you can run your foreach function.
What's the error you get? Is its primary key auto-incremented?
Also:
$person = mysql_real_escape_string($POST['person']);
Should be:
$person = mysql_real_escape_string($_POST['person']);
Your inputs should look like: <input type="text" name="person[]">
Assuming that is the case, you are running mysql_real_escape_string
on an array.
Try this:
$person = $POST['person'];
foreach($person as $value) {
$value = mysql_real_escape_string($value);
$insert = "INSERT INTO people (Person) VALUES ('$value')";
$query = mysql_query($insert) or die("Error: ".mysql_error());
}
As DampeS8N commented, mysql_real_escape_string
returns a string. You can't apply it to an Array
.
Also, anuragbh found a typo on your code, it's $_POST
instead of $POST
.
Lastly, try escaping the special chars inside your loop.
foreach($_POST['person'] as $value)
{
$value = mysql_real_escape_string($value);
//your query here
}