本文介绍了插入表无法正常工作,通过PHP页面没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码可插入表中.我对INSERT INTO类别的问题是它从不将数据插入表中并且没有错误.我在具有不同表的代码中使用几乎相同的查询,并且在那里工作.有任何线索吗?

I have this code to insert into a table. My issue with INSERT INTO categories is that its never inserting data into the table and there is no error. I am using almost the same query in code with a different table and there it's working. Any clue?

<?php 
$action = $_GET['action'] ; 
if ($action=='question')
  question();
elseif ($action=='categories')
  categories();

function question() {
  if ((isset($_SESSION['loggedin']) &&  $_SESSION['loggedin'] == true))
  {
    $include("db.php");
    $category = $_POST['category'] ; 
    $subcategory = $_POST['subCategory'] ; 
    $question = $_POST['question'] ; 
    $answer = $_POST['answer'] ; 

    $query = "INSERT INTO faq (category,subcategory,question,answer)   
      VALUES('.$category.','.$subcategory.','.$question.','.$answer')";
    $success = mysql_query($query);

    if ($success)
    {
      echo '<a href="admin.php" >done >';       
    }
    else 
    {
      echo mysql_error();
    }

  }
}


function categories(){
  if ( ! (isset($_SESSION['loggedin']) && ! $_SESSION['loggedin'] == true))
  {
    include("db.php");
    $category = $_POST['category'] ; 
    $subcategory = $_POST['subCategory'] ; 

    $query = "INSERT INTO categories (category,subcategory)
      VALUES( '$category' , '$subcategory')";
    $success = mysql_query($query);

    if ($success)
    {
      echo '<a href="admin.php" >done>';
    }
    else 
    {
      echo mysql_error();
    }

  }
}
?>

推荐答案

一些问题:

  • 如果要将变量组合为字符串,则可以使用.".字符来加入它们,或者您可以在字符串中包括变量,只要该字符串用双引号引起来即可.在您的代码中,您同时执行了这两项操作.
  • 您没有使数据库输入过分.
  • 您对类别"功能的逻辑检查不正确.
  • 您的超链接标记缺少结束标记.

请参阅下面的修改后的代码.

See the amended code below.

<?php 

$action = $_GET['action'];

if( $action=='question' )
  question();
elseif( $action=='categories' )
  categories();

function question(){
  if( isset( $_SESSION['loggedin'] ) &&  $_SESSION['loggedin'] == true ){

    include( 'db.php' );

    $category = mysql_real_escape_string( $_POST['category'] ); 
    $subcategory = mysql_real_escape_string( $_POST['subCategory'] ); 
    $question = mysql_real_escape_string( $_POST['question'] );
    $answer = mysql_real_escape_string( $_POST['answer'] );

    $query = "INSERT INTO faq ( category , subcategory , question , answer ) VALUES( '{$category}' , '{$subcategory}' , '{$question}' , '{$answer}' )";

    echo "SQL Query to execute: $query"; # Debug Message

    $success = mysql_query( $query );

    if ( $success ){
      echo '<a href="admin.php">done</a>'; 
    }else{
      echo mysql_error();
    }

  }
}


function categories(){
  if( !( isset( $_SESSION['loggedin'] ) || $_SESSION['loggedin']==true ) ){

    include( 'db.php' );
    $category = mysql_real_escape_string( $_POST['category'] ); 
    $subcategory = mysql_real_escape_string( $_POST['subCategory'] );

    $query = "INSERT INTO categories ( category , subcategory ) VALUES ( '{$category}' , '{$subcategory}' )";

    echo "SQL Query to execute: $query"; # Debug Message

    $success = mysql_query( $query );

    if( $success ){
      echo '<a href="admin.php">done</a>';
    }else{
      echo mysql_error();
    }

  }
}

这篇关于插入表无法正常工作,通过PHP页面没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-20 22:22