0

So I am displaying multiple html forms on a page - works great because I am just using a simple loop to display it 80 times but the issue I am facing is I would like to take all 80 forms and submit them into multiple rows in my database. It's only submitting the last form (which is 80)

If anyone can help me find the issue... I would really appreciate it! I've done some searching before submitting this question but I can't seem to find a answer.


Here is what my table looks like:

enter image description here

Here is my form builder

<?php 
// Counts the number of forms
$counter = 1;

echo '<form action="insert.php" method="post">';


// Loop through forms
for ($i = 1; $i < 81; $i++) {

$username = 'admin';
$title = 'test';
$name = 'a';
$image_src = '<img src="image/'.$i.'.jpg">';
$transition = 'fade';
$group_name  = '0';


echo '<hr>' . '('. $counter .')'. '<br>';

echo "
<label>Username:</label>
<input type='text' name='username' id=' required='required' value='".$username."'/>

<br /><br />

<label>Title:</label>
<input type='text' name='title' id=' required='required' value='".$title."'/>

<br/><br />

<label>Name:</label>
<input type='text' name='name' id=' required='required' value='".$name."'/>

<br/><br />

<label>Content:</label>
<input type='text' name='content' id=' required='required' value='".$image_src."'/>

<br/><br />

<label>Image:</label>
<input type='text' name='image' id=' required='required' value='images/".$i.".jpg'/>

<br/><br />

<label>CSS Animate:</label>
<input type='text' name='cssanimate' id=' required='required' value='".$transition."'/>

<br/><br />

<label>Group Name:</label>
<input type='text' name='group_name' id=' value='0'/>

<br/><br />
";

$counter++;

}

echo '<input type="submit" value="Submit" name="submit"/>';
echo '</form>';

?>


Here is my php code: (insert.php)

<?php

$con=mysqli_connect("localhost","admin","password","database_name");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


$sql="INSERT INTO table_name (username, title, name, content, image, cssanimate, group_name, group_sort) VALUES ('".$_POST["username"]."', '".$_POST["title"]."', '".$_POST["name"]."', '".$_POST["content"]."', '".$_POST["image"]."', '".$_POST["cssanimate"]."', '".$_POST["group_name"]."', '".$_POST["group_sort"]."') ";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  } echo "Added!";

mysqli_close($con);


?>
3
  • 1
    All 80 form will have a field with name content. How should insert.php know which field with name content you want? try adding the $i to the name of the fields so all names are different. Then in insert.php you can write a loop that runs 80 times aswell and retrieves "fieldname" . $i for each field Commented Jan 24, 2018 at 16:21
  • 2
    This might be relevant: stackoverflow.com/a/20683296/5749974. You could point name attributes to an array and then loop through it in PHP before inserting. Commented Jan 24, 2018 at 16:23
  • Thank you guys! This helped me Commented Jan 24, 2018 at 16:41

1 Answer 1

4

If you have 80 elements with this name:

name='username'

Then how will this code know which one to use?:

$_POST['username']

Essentially, as your browser builds the form submission, each successive element of the same name over-writes the previous one. So you end up with only the last record.

Instead, give them an array-style name:

name='username[]'

(Repeat for all your duplicated form elements.)

Then in the PHP code, this would itself be an array:

$_POST['username']

You could loop over that array:

for ($i = 0; $i < count($_POST['username']); $i++) {
    $username = $_POST['username'][$i];
    $title = $_POST['title'][$i];
    // etc.
}

This assumes that all of your arrays will be the same length. Which, given this HTML, I suppose they should be. But you can add error checking for that just in case. Either way, each iteration of the loop would build variables equating to that "record" which was submitted.

From there you should be able to build your SQL query with those variables. However, please note that your current way of doing that is extremely unsafe and wide open to SQL injection. To correct that, this is a good place to start.

Side note: Your HTML has invalid id attributes. Additionally, if you want to specify id attributes in that loop, you're going to need to ensure that they are somehow different. Duplicated ids in HTML is invalid.

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

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.