简体   繁体   English

插入函数PHP mySQL

[英]insert function PHP mySQL

I have a function in PHP mySQL that inserts records into a database. 我在PHP mySQL中有一个将记录插入数据库的函数。 It take in an array of decoded JSON data, a field name, a connection and a DB name. 它接收解码的JSON数据数组,字段名称,连接和数据库名称。 It then builds a SQL statement and inserts the records one by one, or at least thats what I think it should do. 然后,它会构建一条SQL语句,并一一插入记录,或者至少就是我认为应该做的事情。 I run everything, make the connection, database and table but when it gets to this function, it fails. 我运行所有内容,建立连接,数据库和表,但是当它到达此功能时,它将失败。 I feel it has to do with my INSERT statement but I am not sure how to fix it. 我觉得这与我的INSERT语句有关,但是我不确定如何解决它。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Function: 功能:

function insertRecords($array,$fieldname, $conn, $db_NAME)
{
    mysqli_select_db($conn, $db_NAME);

    $sql_insert = "INSERT INTO `tbl_articles` (`" . $fieldname . "`) VALUES ('" . $records . "')";

    foreach ($array as $records)
    {

        if(mysqli_query($conn, $sql_insert))
        {
            echo "Records Inserted.";
        }
        else 
        {
            die('Error : ' . mysqli_error($conn) . "<br>");
        }
    }
    echo $sql_insert . "<br>";
}

It just looks like that your query string is displaced there. 看起来好像您的查询字符串已被替换在那里。 Just try this: 尝试一下:

function insertRecords($array,$fieldname, $conn, $db_NAME){
    mysqli_select_db($conn, $db_NAME);

    foreach ($array as $records)
    {
        $sql_insert = "INSERT INTO `tbl_articles` (`" . $fieldname . "`) VALUES ('" . $records . "')";
        if(mysqli_query($conn, $sql_insert))
        {
            echo "Records Inserted.";
        }
        else 
        {
            die('Error : ' . mysqli_error($conn) . "<br>");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM