我的代码中的set_title()函数有什么问题

时间:2019-05-21 08:34:36

标签: php mysql sql mysqli

我创建了一个名为set_title()的函数,该函数用于在数据库中设置标题和描述,我不知道我哪里写错了

         function set_title($file,$title = "",$description = ""){
           $pathinfo = pathinfo($file);
           $file = $pathinfo['basename'];
           if ($title == "") {
           $title = ucfirst($pathinfo['filename']);
            }
            if ($description !== "") {
            $description = mb_substr($description, 0, 150);
            }
           $sql = "SELECT file, title ,description FROM title  WHERE file = '$file'";
           $con = new mysqli('localhost','root','','jbstore');
           $result = $con->query($sql);
           if($result->num_rows > 0){
             $data = $result->fetch_assoc();
               if($data['description'] == ""){
                 $sql = "INSERT INTO title (description) VALUES('$description')";
                 $con->query($sql);
               }
           }elseif ($result->num_rows == 0) {
             $sql = "INSERT INTO title (file,title,description) VALUES ('$file','$title',$description)";
             $result = $con->query($sql);
           }
         }

我希望它可以将数据插入数据库,但是什么也没发生

1 个答案:

答案 0 :(得分:0)

代码中的某些问题。

 $sql = "INSERT INTO title (file,title,description) VALUES ('$file','$title',$description)";

此处$ description将用作INT值。

在$ description中加上quote('')

 $sql = "INSERT INTO title (file,title,description) VALUES ('$file','$title','$description')";