为什么我的表单不能使用POST方法?

时间:2017-08-17 05:26:30

标签: php forms

我写了以下HTML代码:

  <form action="index.php" method="POST">    
      <input type="text" name="title" required>
      <input type="text" name="brief_text" required>
      <textarea name="text" required></textarea>
      <input type="submit" name="add" value="Add">
  </form>

我的PHP代码:

<?php
require_once('db.php');

if(isset($_POST['add'])){
  $title = $_POST['title'];
  $brief_text = $_POST['brief_text'];
  $text = $_POST['text'];
  $blog_cat_id = $_POST['blog_cat_id'];

  if($title AND $brief_text AND $text AND $blog_cat_id){
    $insert_blog = "insert into blog values ('','$title','$brief_text','$text','$blog_cat_id',NOW())";
    $run_insertion = mysqli_query($con, $insert_blog);

    if($run_insertion){
      echo "Blog has been added!";
     }
    else{
      echo "Error adding blog!!!";
    }
 }
 else{
   echo "All fields are required!";
 }
}
else{
echo "GOODBYE";
}

?>

每次刷新页面时,它只显示表单和&#34; GOODBYE&#34;甚至不会将数据插入数据库表。 请帮帮我。

3 个答案:

答案 0 :(得分:0)

你几乎没有错,

1)应该是:$_POST['add']而不是$_POST['add_blog']

2)没有$_POST['blog_cat_id']没有形式

修改 复制了您的代码并进行了一些更改:

<强>码

if(isset($_POST['add'])){
    print_r($_POST);
    $title = $_POST['title'];
    $brief_text = $_POST['brief_text'];
    $text = $_POST['text'];
    $blog_cat_id = $_POST['blog_cat_id'];

    if($title AND $brief_text AND $text AND $blog_cat_id){
        echo "inside condition";
        $insert_blog = "insert into blog values ('','$title','$brief_text','$text','$blog_cat_id',NOW())";
        $run_insertion = mysqli_query($con, $insert_blog);

        if($run_insertion){
          echo "Blog has been added!";
      }
      else{
          echo "Error adding blog!!!";
      }
  }
  else{
     echo "All fields are required!";
 }
}
else{
    echo "GOODBYE";
}

<强> HTML:

<form action="index.php" method="POST">    
      <input type="text" name="title" required>
      <input type="text" name="brief_text" required>
      <input type="text" name="blog_cat_id" required>
      <textarea name="text" required></textarea>
      <input type="submit" name="add" value="Add">
  </form>

<强>输出

Array ( [title] => test [brief_text] => test [blog_cat_id] => 1 [text] => testing [add] => Add ) 

inside condition

现在,检查您的查询是否无效。

希望这会对你有所帮助。

答案 1 :(得分:0)

您的查询错误,未指定列,并且您对sql注入开放,您应该学会使用参数化查询。但是这次你可以使用以下内容。

试试这个:

<强> htmlcode

<form action="index.php" method="POST">    
  <input type="text" name="title" required>
  <input type="text" name="brief_text" required>
  <input type="text" name="blog_cat_id" required>
  <textarea name="text" required></textarea>
  <input type="submit" name="add" value="Add">
</form>

<强>的index.php

<?php
require_once('db.php');
if(isset($_POST['add'])){
  $title = $_POST['title'];
  $brief_text = $_POST['brief_text'];
  $text = $_POST['text'];
  $blog_cat_id = $_POST['blog_cat_id'];

  if($title AND $brief_text AND $text AND $blog_cat_id){
    $insert_blog = "insert into blog('col1','col2','col3','col4','col5','col6') values ('','$title','$brief_text','$text','$blog_cat_id',NOW())";
    $run_insertion = mysqli_query($con, $insert_blog);

    if($run_insertion){
      echo "Blog has been added!";
     }
    else{
      echo "Error adding blog!!!";
    }
 }
 else{
   echo "All fields are required!";
 }
}
?>

注意: col1, col2, col3, col4, col5 and col6将是您的列名。

答案 2 :(得分:0)

它是否仍在展示&#39; GOODBYE&#39;现在你已经改变了

$_POST['add_blog'] 

$_POST['add'] 

点击提交时?