0

I am in a fix here. I have code that does not insert multiple data into mysql table with one form. Here's my code-

<?php
if (isset($_POST['submitted'])) {
  include('config.php');
  foreach($_POST['name'] as $row=>$nam) {
    $name=$nam;
    $class=$_POST['class'][$row];
    echo "<br/>" . $name . "<br/>" . $class;
    $sql="INSERT INTO multiple (name, class) VALUES ('$name','$class')";
  }
  if (!mysql_query($sql)) die('Error: ' . mysql_error());
  echo "1 record added";
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>Name1:
<input id="name" name="name[]" type="text">
<input type="hidden" name="submitted" value="true" />
</label>
<label> Class1:
<input id="class" name="class[]" type="text">
</label>
<label>Name2:
<input id="name" name="name[]" type="text">
</label>
<label>Class2:
<input id="class" name="class[]" type="text">
</label>
<label>Name3:
<input id="name" name="name[]" type="text">
</label>
<label>Class3:
<input id="class" name="class[]" type="text">
</label>
<label>Name4:
<input id="name" name="name[]" type="text">
</label>
<label>Class4:
<input id="class" name="class[]" type="text">
</label>
<label>Name5:
<input id="name" name="name[]" type="text">
</label>
<label>Class5:
<input id="class" name="class[]" type="text">
</label>
<input value="Add" type="submit">

</form>

When I press the submit button nothing inserts in the mysql table. Only empty fields are created. If I insert 5 text field I get 5 empty fields in sql table.

13
  • 1
    mysql_* is deprecated, and your code is open to SQL injection. Commented Aug 14, 2012 at 21:03
  • please show us the output of your echo statement. also echo $sql; Commented Aug 14, 2012 at 21:11
  • It's illegal to duplicate id's the way you are doing. Each value in id="" must only be used once. Commented Aug 14, 2012 at 21:13
  • Please show us the output of print_r($_POST); then we can debug this. Put that right at the top of the form. Commented Aug 14, 2012 at 21:13
  • 1 record addedArray ( [name] => Array ( [0] => Arti [1] => [2] => [3] => [4] => ) [submitted] => true [class] => Array ( [0] => a [1] => [2] => [3] => [4] => ) ) this is shown while print_r($_POST); Here "Arti" is a Name and "a" is a class Commented Aug 14, 2012 at 21:19

3 Answers 3

1

Imroz, your use of [] as part of the names of your input elements (not id's) example name="class[]" when the form is posted it builds an array. The post object PHP would recognize would be $_POST['class']

But that being an array means you have to handle it slightly different before inserting it into your database as you can't just (well maybe you can) drop an array into the DB

if you did

for($x=0;$x < count($_POST['class']); $x++)
{
    echo $_POST['class'][$x].'<br>';
}

you would be able to see all your posted inputs from the inputs with the name class[]

of course this is a core example of what you need to do overall, but I am just trying to express whats going on with your posted data.

Sign up to request clarification or add additional context in comments.

3 Comments

The OP's already taking into account that these are arrays using a foreach loop to iterate through.
Thank You Chris. I understand now, why empty fields are inserted. Thanks a lot Chris!
Chris, in the system you showed, is still inserting empty rows in the table :(
1

i think your inserting query is problem.....try like this

$query = mysql_query("INSERT INTO category VALUES('','$name','$class','')") or die(mysql_error()); 

Comments

0

You have the } in the wrong place.

The } right after the $sql= should be moved after the echo "1 record added";

I reformatting your code to use proper indenting and the error was obvious.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.