Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Trees

Liquid Web Managed Dedicated Server: RAID 1 SSD with 16GB of RAM minimum! 24/7 On-Site Heroic Support! Industry-Leading SLA!

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Liquid Web Cloud VPS Hosting

8,980 questions

12,393 answers

573 users

How to insert MySQL multiple rows in a form using input type text and update all the rows in PHP

1 Answer

0 votes
/* index.php */

$db_host        = 'localhost';
$db_user        = 'user';
$db_password    = 'password';
$db_name        = 'db_name';
 
$mysqli = new mysqli($db_host, $db_user, $db_password , $db_name);

$sql = "SELECT query, query_id FROM query_table WHERE user_id = ?";
    
$sql_query = $mysqli->prepare($sql);
$sql_query->bind_param('s', $user_id);
$sql_query->execute();

$sql_query->bind_result($query, $query_id);
$sql_query->store_result();

?>
<form action="update.php" method="post">
<?php
while ($sql_query->fetch())
{
?>
    <input type="text" name="query[]" value="<?php echo $query; ?>"><br />
<?php
}
?>
<button type="submit">Update</button>                                            
</form>

<?php
$sql_query->close();
$mysqli->close(); 
?>
/* update.php */

$db_host        = 'localhost';
$db_user        = 'user';
$db_password    = 'password';
$db_name        = 'db_name';
 
$mysqli = new mysqli($db_host, $db_user, $db_password , $db_name);
    
for ($i = 0; $i < count($_POST['query']); $i++)
{
    if ($sql_query = $mysqli->prepare("UPDATE query_table SET query = ? WHERE query_id = ?"))
    {
        $sql_query->bind_param('ss', $_POST['query'][$i], $_POST['query-id'][$i]);
        $sql_query->execute();
    } 
}
$sql_query->close();
$mysqli->close(); 

 




answered Nov 11, 2017 by avibootz
edited Nov 11, 2017 by avibootz
...