JSON没有通过php正确插入mysql

时间:2016-06-28 01:22:36

标签: php mysql json sql-insert

我一直在尝试通过php将json文件中的数据插入到mysql中。 我有一个函数在一个文件中执行插入,而解码在另一个文件中完成。 当我运行代码时,我收到了这个错误:

You could not be registered due to a system error. We apologize for any inconvenience.

Column 'password' cannot be null

Query: INSERT INTO users (id, email, password, username, deviceId, date_created) VALUES (null, ?, ?, ?, ?, NOW() )
Query was executed

enter code here

以下是我的三个文件:

insertion.php:

//Require files
require('functions.php');
if(file_exists('data.json')){
    echo "The file exists ";

    $file= file_get_contents('data.json');
    echo json_encode('data.json');

    $data=json_decode($file, true);

    var_dump($data);

$email= $data["email"];
$password= $data["password"];
$username= $data["username"];
$deviceId= $data["deviceId"];
$tableName= 'users';
$email= "email@example.com";

$error=json_last_error();
echo "<br><br>";
echo "your email shoudl be displayed right here: ".$email. "This is email";
echo "<br>JSON Errors will display here:". $error;

$execute= dataInsert($tableName, $email, $password, $username, $deviceId);

if($execute){
    echo "Query was executed";


}



}
else{

echo "file does not exist";

}

的functions.php:

//------------------------dataInsert()---------------------------//

function dataInsert($tableName, $email, $password, $username, $deviceId){


//set database connection
require('mysqli_connect.php');
 if($dbc){
  echo "<h3>connection successful</h3>";

 }

//form query using the values and tablename
$query = "INSERT INTO users (id, email, password, username, deviceId, date_created) 
VALUES (null, ?, ?, ?, ?, NOW() )"; 


//Prepare statement
$stmt= mysqli_prepare($dbc, $query);


//Bind the variables

mysqli_stmt_bind_param($stmt, 'sssi', $email, $password, $username, $deviceId);


//Execute the query

$result=mysqli_stmt_execute($stmt);



if($result){
    echo "Success !";

}
else{
    // Public message:
        echo 'System Error
        <p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>'; 

        // Debugging message:
        echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $query . '</p>';

}

mysqli_close($dbc); // Close the database connection.
return true;
}

data.json

<pre>
<code>
{"users":[
{
    "email":"fakeemail@gmail.net",
    "password":"mypass12",
    "username":"myusername",
    "deviceId":"21"
 }

]}

当我使用var_dump显示json数组时,它看起来是正确的:

"data.json"array(1) { ["users"]=> array(1) { [0]=> array(4) { ["email"]=> string(19) "fakeemail@gmail.net" ["password"]=> string(6) "mypass12" ["username"]=> string(13) "myusername" ["deviceId"]=> string(2) "21" } } } 

我已经能够在数据库中插入行,但除了日期和自动增量ID之外,它们都是空白的。当我设置$ email = email@example.com时,它将显示密码不能为空,但注释该行将导致错误显示'email'不能为空

2 个答案:

答案 0 :(得分:0)

您正在ID字段中插入null。尝试删除它。

答案 1 :(得分:0)

您正在错误地访问已解码的json字符串的值,传递true作为json_decode的第二个值,您将获得一个关联数组。

var_dump($data['users'][0]['email']);

结果

fakeemail@...

所以你可以做类似

的事情
if (count($data['users'])) 
{
    foreach($data['users'] as $user) 
    {
        // insert query passing
        // $user['email'], $user['password'],
        print $user['email'];
    }
}