1

I have a form with an "add contact" section, where upon a click, it adds another row to the form with 3 more input boxes. (The segment of code on jfiddle: http://jsfiddle.net/fmdx/cYxYP/)

HTML

<form method="post" action="">
<div style="padding-left:40px;">
<div id="customcopies" style="padding-left:40px;">
1. Name: <input type="text" id="copiestoname_1" name="copiestoname[]" placeholder="Jane Doe Smith" required>, Institution: <input type="text" id="copiestoinst_1" name="copiestoinst[]" placeholder="Bank" required>, Method: <input type="text" id="copiestomethod_1" name="copiestomethod[]" placeholder="Email" required>
</div>
</div>
<input type="submit" name="submit_val" value="Submit" />
</form>

With this being the PHP/MYSQL for insertion:

if (isset($_POST['submit_val'])) {
    foreach ($_POST['copiestoname'] as $key=>$value) {
    $copiestoname = mysql_real_escape_string($value);

        mysql_query("INSERT INTO copiesto (name) VALUES ('$copiestoname')") or die(mysql_error());
        echo "Completed";
    }
echo "" . count($_POST['copiestoname']) . " Names Added<br>";
mysql_close();
}

The table in the database is:

Table Name: copiesto
+-------------+-------------+-------------+---------+
| index       |  name       | institution | method  |
+-------------+-------------+-------------+---------+

How would I expand the current MYSQL code to accept entries from the other 2 arrays and input their data into the same MYSQL row during that loop?

6
  • 1
    Do you have a reason for using mysql_query? Is this a legacy application from the 1990s? Commented Sep 6, 2013 at 19:33
  • It's the code I've pieced together from examples and tutorials, sorry if it is a bit outdated! Commented Sep 6, 2013 at 19:36
  • what's wrong with this script? Commented Sep 6, 2013 at 19:45
  • I'd like to get the MYSQL code to accept the values from the other 2 input box arrays, inserting 1 value from each array into the row of the mysql database. The code above only does the first array, but I haven't been successfully able to expand it to include the other 2. Commented Sep 6, 2013 at 20:03
  • @fmdx Learning from tutorials is an extremely risky way to go about learning PHP and web development as it will teach you all kinds of hazardously bad habits. A much better solution is to use a popular framework so that you can learn from good examples, plus make use of community developed add-ons that will snap in to your application without needing to be customized. Commented Sep 9, 2013 at 15:03

1 Answer 1

4

you can use for loop instead

for ($i=0; $i < count($_POST['copiestoname']); $i++ ) {
  $copiestoname = mysql_real_escape_string($_POST['copiestoname'][$i]);
  $copiestoinst = mysql_real_escape_string($_POST['copiestoinst'][$i]);
  $copiestomethod = mysql_real_escape_string($_POST['copiestomethod'][$i]);

  mysql_query("INSERT INTO copiesto (name, institution, method) VALUES ('$copiestoname', '$copiestoinst', '$copiestomethod')") or die(mysql_error());
  echo "Completed";
}
Sign up to request clarification or add additional context in comments.

1 Comment

I get this error: "Column count doesn't match value count at row 1"

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.