javascript - 默认 NULL 未设置

标签 javascript php jquery ajax

我在 contenteditable 方面遇到问题。我正在尝试使用 contenteditable 制作 ajax post 方法。我有两个 contenteditable 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

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

最佳答案

问题是您正在传递 empty value而不是null本身

只需检查字符串是否为 empty如果为空则置 null

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作为字符串 'null' :

因此要进行相应的处理,请使用 pdo:

$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
                    ));

编辑:

由于某些原因,如果您不能或不想使用 PDO。你最终会侵入sql。像这样的东西:

$text2 = !empty($text2) ? "'".$text2."'":null;
$text1 = !empty($text1) ? "'".$text1."'":null;
$query = mysqli_query($this->db,"INSERT INTO `posts` (text1,text2, uid_fk,time) VALUES ($text1,$text2,'$uid','$time')");

关于javascript - 默认 NULL 未设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43366571/

相关文章:

javascript - 如何在 javascript 中以正确的顺序连接波斯字符和英文字符?

javascript - React Redux,在获取数据并将其映射到组件后重新渲染?

javascript - 有没有办法获取表单元素及其相关值

php - 如何在 PHP 中获取 JavaScript 变量值

javascript - iFrame WYSIWYG 字体大小不会重置

javascript - 我怎样才能在jquery中循环这个函数?

javascript - 是否可以制作一个模式,该模式应在开头具有特定文本,然后接受特定文本之后的任何内容

javascript - 有人可以解释这两个 http 调用之间的区别以及为什么一个失败而另一个没有失败吗?

php - 将哈希# 添加到 UrlGenerator

PHP 按包含 Y-m-d H :i:s date 的元素对多维数组进行排序