简体   繁体   中英

how to insert multiple record in databse using php

$link = mysqli_connect("localhost", "root", "", "jeetu") or die("Error " . mysqli_error($link));

if (isset($_POST['ok'])) {

    $n = $_POST['name[]'];
    $c = $_POST['contact[]'];
    $a = $_POST['address[]'];
    $count = count($n);

    for ($i = 0; $i <= $count; $i++) {
        print_r($n[$i]);
        print_r($c[$i]);
        print_r($a[$i]);        die(); 
        $query = "insert into add (name, contact, value) values ('" . $_POST['name[$i]'] . "'," . $_POST['contact[$i]'] . ",'" . $_POST['address[$i]'] . "')";
        mysqli_query($link, "$query");
    }
}

If you're using a form to input this data into a database there is no way your code will work since you're getting data from one form and not from many forms.

$count = count($n); will always return 1 because you are getting one name from the form. It only works if you're reading the data from the database.

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