php - mysql 发布来自 SERVER REQUEST_METHOD 的丢失数据

标签 php mysql post

我有以下代码,每当我尝试插入数据时,$content 都没有被插入,问题可能出在哪里?我正在使用功能测试输入来解决安全相关问题。

<?php
// define variables and set to empty values
$title = $content = $path = $file_type ="";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
  $title = test_input($_POST["title"]);
  $content = test_input($_POST["content"]);
  $path = test_input($_POST["path"]);
  $file_type = test_input($_POST["file_type"]);

}

function test_input($data)
{
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

$con=mysqli_connect("localhost","---","---","---");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


$sql="INSERT INTO articles (ArtID,Title,Content,Image_VideoLink_Path,file_type)
VALUES
('','$title','$content','$path',' $file_type')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "<h2 >Article Published</h2>";
echo"<a href='../mag/index.php'> View it Now </a>";

mysqli_close($con);
?>

这是表单的代码:

<form  method="POST" action="artPro.php">
  <fieldset>
    <legend>Create New Article</legend>
    <br/>
    Article Title:
    <input type="text" placeholder="enter title here" class="span3" name="title" required>

           Image/Video Path :

           <input type="text" placeholder="enter image name e.g k.jpg or k.mp4 for video" name="path" class="span3" required/>

          File Type :
          <input type="text" name="file_type" class="span3" required placeholder="e.g: image or video"/>

  <br/>

            <label>Article Content:</label>

           <textarea name="content" rows="20"  class="jqte-test span12" required   id="txtmsg"></textarea>


<br><button type="submit" class="btn btn-primary btn-large pull-right">Publish</button>
  </fieldset>
</form>

最佳答案

尝试这个查询:

$sql="INSERT INTO articles (Title,Content,Image_VideoLink_Path,file_type) VALUES ('$title','$content','$path',' $file_type')";

$content 未被插入,因为您提供了文章 ID。我确信文章 ID 将是您的主键,并且会自动递增。因此,您不需要在 sql 查询(语句)中提及文章 id。

关于php - mysql 发布来自 SERVER REQUEST_METHOD 的丢失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22350019/

相关文章:

php - 在 while 循环中使用 GROUP_CONCAT 在一行中输出同一用户的多个数据库条目

php - 我可以加入表并在同一个查询中使用 AVG

mysql - 如何将变量从 shell 脚本传递到 mysql 脚本

javascript - JQuery Post 错误 react

php - 清理所有已发布字段的正确方法?

php - 是否可以在不关闭两步验证的情况下使用 PHPMailer?

javascript - 停用模态后如何停止YouTube视频?

mysql - 将默认的 MYSQL JSON 值设置为 `{}` 还是 `NULL` 更好?

mysql - Excel 导出无法正确显示波斯语字符

javascript - Node.js发送带base64解码字符的POST请求时,出现无效字符错误