简体   繁体   中英

How to insert multiple rows to mysql using php form when checkbox is ticked in the row

I Have a PHP form where I am sending to MYSQL DB fields: ID, NAME, CITY I have another form where I am editing whole list. What id like to do is to populate the same NAME (choosing from list) to every row when checkbox is ticked.

Form.php

<form method="post" action="submit.php" >
<select name="name" id="name"><option type="text" name="name" value="<?php echo $name ?>"><?php echo $name ?></option></select>
<select name="city" id="city"><option type="text" name="city" value="<?php echo $city ?>"><?php echo $city ?></option></select>
<input type="checkbox" name="check[<?php echo $id ?>]" value="<?php echo $id ?>">
<button type="submit" name="update">Update</button>
</form>

submit.php

if (isset($_POST['update'])) {
    foreach($_POST['check'] as $key=>$value){
    $id = $_POST['id'];
        $name = $_POST['id'][$key];
    $city = $_POST['city'];
        mysqli_query($db, "UPDATE table SET name='$name', city='$city' WHERE id=$id");
}

It would be awsome to tick checkbox to those rows where i want to have the same defined NAME and submit it.

在此输入图像描述

I have created an example, hope it will help you.You cannot have id value in option tag, you can only have value.

    <form method="post" action="submit.php">
        <select name="name" id="name">
          <option value="1">Option 1</option>
        </select>
        <select name="city" id="city">
          <option value="Athens">Athens</option>
        </select>
        <input type="checkbox" name="check_box" />
        <button type="submit" name="update">Update</button>
    </form>    

<?php
    if (isset($_POST['update'])) {

        $check_box_checked = isset($_POST['check_box']);

        if($check_box_checked){
            echo "Checked";
        }else{
            echo "Un Checked";
        }

        $id = $_POST['city'];
        echo $id;

        $name = $_POST['name'];
        echo $name;

        //mysqli_query($db, "UPDATE table SET name='$name', city='$city' WHERE id=$id");
    }
    ?>

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