【问题标题】:JSON not properly inserting into mysql through phpJSON没有通过php正确插入mysql
【发布时间】:2016-06-28 01:22:35
【问题描述】:

我一直在尝试通过 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

这是我的三个文件:

插入.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;
}

数据.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 时,它将显示密码不能为空,但注释该行将导致错误显示“电子邮件”不能为空

【问题讨论】:

  • 尝试在查询插入中删除 id
  • 我删除了插入中的 id 并从值中删除了 null,但我仍然得到同样的错误
  • 也尝试删除查询参数 date_created

标签: php mysql json sql-insert


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您错误地访问了解码后的 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'];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      • 2014-06-25
      • 2013-02-22
      • 2015-08-30
      • 1970-01-01
      • 2019-05-06
      相关资源
      最近更新 更多