存储长文本的最佳数据类型,如新闻,文章。

时间:2016-07-13 02:47:14

标签: php mysql

当我试图存储新闻条目时,这是我的问题,其中一些条目正在插入,但有些则不是。我不知道数据类型是否存在问题。我只是复制一些我在互联网上找到的新闻并将其粘贴到我的表格中。有人可以帮我解决这个问题吗?

我的数据库结构.. enter image description here

未插入的新闻样本。我在维基中复制此文本并将其粘贴到我的新闻内容输入中。并没有奏效。 enter image description here

成功插入的新闻样本 enter image description here

这是我的添加代码。

 <?php
    include_once('connection.php');
    session_start();
   $username = ucfirst($_SESSION['username']);

  if(isset($_POST['submit'])){

    $title = $_POST['title'];
    $date = $_POST['date'];
    $content = $_POST['content'];
    $file=$_FILES['image']['tmp_name'];
    $image= @addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name= addslashes($_FILES['image']['name']);
    move_uploaded_file($_FILES["image"]["tmp_name"],"img/" . $_FILES["image"]["name"]);
    $newsimage="img/" . $_FILES["image"]["name"];



    $sql ="INSERT into news (news_title, news_date, news_content, news_image) VALUES ('$title', '$date', '$content', '$newsimage')";
    mysqli_query($con, $sql);

    echo '<div class="alert alert-success alert-dismissable" id="success-alert">';
   echo '<strong>Successfully posted!</strong>';
   echo '</div>';

  }




  ?>

2 个答案:

答案 0 :(得分:1)

这里几乎没有什么问题。

发布表单数据时,您需要使用POST而不是GET。

如果数据成功发布转义文本,然后将其插入数据库:

$content = mysqli_real_escape_string(stripslashes($_POST['content']));

LONGTEXT适用于较长的文字。除非您使用二进制数据(如图片等),否则您不需要LONGBLOB

答案 1 :(得分:0)

我通过将$content = $_POST['content'];更改为$content = mysqli_real_escape_string($con,$_POST['content']);

来解决这个问题