简体   繁体   中英

how to save data dynamically to database

JQUERY CODE

 $(document).ready(function () {
    $("#add").click(function () {
        $(".left .inputs").append("<li><input type ='text' name='name[]' class='txtbox1'></li>");
        $(".right .inputs").append("<li><input type ='text' name='grade[]' class='txtbox'></li>");
    });
 });

MY PHP CODE

<?php
    $con = mysql_connect ("localhost","root","") or die('cannot connect to database error: '.mysql_error());
    if (isset($_POST['name']) && isset($_POST['grade']))
    {

        $desk_report = $_POST['name'];//contains array value
        $desk_action = $_POST['grade'];//contains array value
        foreach($desk_report as $key=>$user) { //Loop through arrays
            if (!empty($desk_report[$key]) && !empty($desk_action[$key])) {
                mysql_select_db("quickbook", $con);
                $sql = "INSERT INTO student_reg(relative_name,relative_grade) VALUES ('$desk_report[$key]','$desk_action[$key]')";
                if ($sql_run = mysql_query($sql)) {
                    echo 'ok.'; 
                } else {
                    echo '*Sorry, we couldn\'t register you at this time. Try again later.';
                }
            }
        }
    }
?>

I want to add inputs data to database.i created a code.but this is not working .can you help me?

Check the database which you used is exists or not, try your code like

<?php
    $con = mysql_connect ("localhost","root","") or die('cannot connect to database error: '.mysql_error());

    //add this line to your code here instead of in for loop
    mysql_select_db('quickbook',$con) or die("Could not select database!");
    if (isset($_POST['name']) && isset($_POST['grade']))
    {
        $desk_report = $_POST['name'];//contains array value
        $desk_action = $_POST['grade'];//contains array value
        foreach($desk_report as $key=>$user) { //Loop through arrays
            if (!empty($desk_report[$key]) && !empty($desk_action[$key])) {
                $sql = "INSERT INTO student_reg(relative_name,relative_grade) VALUES ('$desk_report[$key]','$desk_action[$key]')";
                if ($sql_run = mysql_query($sql)) {
                    echo 'ok.'; 
                } else {
                    echo '*Sorry, we couldn\'t register you at this time. Try again later.';
                }
            }
        }
    }
?>

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