默认值未设置NULL

时间:2017-04-12 10:11:13

标签: javascript php jquery ajax

我遇到了满足的问题。我正在尝试使用contenteditable进行ajax post methot。我有两个满足的div。

如果它没有写任何东西,它必须是数据库表中的NULL,但它显示空帖子没有设置 NULL

AJAX

$("body").on("click", ".post", function() {
   var text1 = $("#text1").html();
   var text2 = $("#text2").html();

   var data = 'text1=' + encodeURIComponent(text1) + '&text2=' + encodeURIComponent(text2);

      $.ajax({
         type: 'POST',
         url: '/requests/post.php',
         data: data,
         cache: false,
         beforeSend: function() {
            // Do something
         },
         success: function(html) {
            console.log("post sended");
         }
      });

});

HTML

<div class="abc" id="text1" contenteditable="true" placeholder="Write something1"></div>
<div class="text2" id="text2" contenteditable="true" placeholder="Write something2"></div>

post.php中

<?php 
include "../inc/inc.php";
if(isset($_POST['text1'])){
    $text1 = mysqli_real_escape_string($db, $_POST['text1']);
    $text2 = mysqli_real_escape_string($db, $_POST['text2']);
    $data = $POST->POST($uid,$text1, $text2); 
}
?>

和POST功能

public function POST($uid,$text1, $text2) {


    $time=time(); // Current post time
    $ip=$_SERVER['REMOTE_ADDR']; // user ip
    $query = mysqli_query($this->db,"SELECT post_id,text1,text2 FROM `posts` WHERE uid_fk='$uid' order by post_id desc limit 1") or die(mysqli_error($this->db));
    $result = mysqli_fetch_array($query, MYSQLI_ASSOC); 
    // Add the insert POST
     $query = mysqli_query($this->db,"INSERT INTO `posts` (text1,text2, uid_fk,time) VALUES ('$text1','$text2','$uid','$time')") or die(mysqli_error($this->db));

}

所有代码都运行正常。但问题是text2。如果没有写任何文本,它会在数据库中显示空结果。我想设置ID默认 NULL

enter image description here

错误的方式,

enter image description here

看起来应该是这样的

enter image description here

我在这里缺少什么?在这方面,任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

问题是您传递@Override public int onStartCommand(Intent intent, int flags, int startId) { // restart service every hour to get the current step count ((AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE)) .set(AlarmManager.RTC, System.currentTimeMillis() + AlarmManager.INTERVAL_HOUR, PendingIntent.getService(getApplicationContext(), 2, new Intent(this, SensorListener.class), PendingIntent.FLAG_UPDATE_CURRENT)); return START_STICKY; } 而不是empty value本身

只检查字符串是否为null,如果为空则将其置为

empty

此外,您if(isset($_POST['text1'])){ $text1 = mysqli_real_escape_string($db, $_POST['text1']); $text2 = mysqli_real_escape_string($db, $_POST['text2']); $text1 = !empty($text1) ? $text1 : null; $text2 = !empty($text2) ? $text2 : null; $data = $POST->POST($uid,$text1, $text2); } 声明正在字符串周围添加insert,这会使'成为字符串null

所以要相应地处理使用pdo:

'null'

修改

出于某些原因,如果您不能或不想使用PDO。你最终会破解sql。类似的东西:

$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname","$dbusername","$dbpassword");
$stmt = $dbh->prepare("INSERT INTO `posts` (text1,text2, uid_fk,time) VALUES (:text1,:text2,:uid,:time)");

$statement->execute(array(':text1'=>$text1,
                          ':text2', $text2,
                          ':uid', $uid,
                          ':time', $time
                    ));

答案 1 :(得分:0)

为逻辑添加预先检查,因为您必须提供NULL或不使用插入中的字段来获取表格中的真实NULL

// Add the insert POST
 $text2 = $text2?"'$text2'":'NULL';
 $query = mysqli_query($this->db,"INSERT INTO `posts` (text1,text2, uid_fk,time) VALUES ('$text1',$text2,'$uid','$time')") or die(mysqli_error($this->db));

INSERT TEXT vs NON-TEXT

插入真实的NULL

INSERT INTO posts (text1) VALUES (null)

插入内容为NULL

的文字

INSERT INTO posts (text1) VALUES ('NULL')

插入表格默认值:

INSERT INTO posts (text1) VALUES (default)

插入内容为default

的文字

INSERT INTO posts (text1) VALUES ('default')

在此处更多地了解sql:

INSERT INTO test (testfield) VALUES (null), ('null'), (default), ('') ;

使用一个字段test创建一个表VARCHAR(50) NULL DEFAULT NULL并执行SQL并查看所获得的内容。您将获得2个带有空值的条目,一个带有空字符串,另一个带有字符串null