【问题标题】:adding array values to mysql database using php and jquery使用 php 和 jquery 将数组值添加到 mysql 数据库
【发布时间】:2014-01-03 21:50:37
【问题描述】:

我正在尝试通过为每个查询逐一插入多行来填充表,因为我正在发送一个数组元素,以下是我的代码:

标记:

<form action="post.php" method="POST">
    <h1>user sign up</h1>
    name:<input type="text" name="name" id="name"/>
    password:<input type="text" name="pass" id="pass"/>

    <div id="container">
        <a href="#"><span id="add_field">» Add your list...</span></a><br/><br/>
    </div>
    <input id="go" class="btn" name="btnSubmit" type="submit" value="Signup" />
</form>

脚本:

<script>
var count = 0;
    $(function(){
        $('#add_field').click(function(){
            count +=1;
            //this is for appending input type="text" with the name of fields[]
            // example like this: <input id="field_"+the incremented value (1,2,3..)+"name="fields[]" type="text"/>
            $('#container').append('<strong>Link #' + count + '</strong>'+ '<input id="field_' + count + '" name="fields[]' + '" type="text" /><br/>' );
        });
    });
</script>

php:

<?php  
$db = mysqli_connect("localhost","root","","test");
error_reporting(E_ALL);
//on click of the submit button
if(isset($_POST['btnsubmit'])){
    //if the fields are not empty
        if(!empty($_POST['fields']) && !empty($_POST['name']) && !empty($_POST['pass'])){
            //the array / dynamic field
            $fields = $_POST['fields'];
            //the other two normal fields
            $user = $_POST['name'];
            $pass = $_POST['pass'];

            //looping through and inserting the rows in table  
            foreach($fields as $key=> $value){
                $query = mysqli_query($db,"INSERT INTO sample VALUES('','$user','$pass','$value')");
                if($query){
                    echo "added <br/>";
                } else {
                    echo "try";
                }
            }//end of foreach loop
            echo "user added";
        } else {
            echo "fill the web fields";
        }//end of the !empty if statement
}//end of first if
?>

在这里我想填充如下表格

如果只输入了一个 $_POST['fields']

id | username   | password | field
----------------------------------------
1 | name        | password | field value

如果输入了多个 $_POST['fields']

id | username   | password      | field
------------------------------------------------
 1 | name       | password      | field value 1
 2 | name 2     | password 2    | field value 2
 3 | name 3     | password 3    | field value 3

依此类推(取决于数组大小/...)

【问题讨论】:

  • AAAANNNNDDDD 有什么问题???
  • 它没有插入数据

标签: php jquery mysql arrays


【解决方案1】:

我想你的问题是如何做到这一点,因为代码没有意义。你快到了,只需改变这个

mysqli_query($db,"INSERT INTO sample VALUES('','$user','$pass','$value')");

进入这个:

mysqli_query($db,"INSERT INTO sample (user, pass, value) VALUES(" . 
    mysql_real_escape_string($user) . "," .
    mysql_real_escape_string($pass) . "," .
    mysql_real_escape_string($value) . 
")");

(user, pass, value) 更改为您使用的列名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 2014-05-12
    • 2017-01-15
    相关资源
    最近更新 更多