简体   繁体   中英

php - insert multiple rows into mysql from form loop

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:

在此处输入图片说明

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); ?> 

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 id s in HTML is invalid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM