开发者

Insert into table not working and no error through php page

开发者 https://www.devze.com 2023-03-29 09:08 出处:网络
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 dif

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?

开发者_JAVA百科<?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();
    }

  }
}
?>


A few issues:

  • If you are combining variables into a string, you can use the "." character to join them, or you can include variables within the string, so long as the string is wrapped in double quotation marks. In your code, you were doing both at once.
  • You were not santising your database input.
  • Your logic checks for the "categories" function were incorrect.
  • Your hyperlink tags were missing the closing tags.

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();
    }

  }
}


First off, to help debugging, I'd put these two lines at the top of your scripts to show all the errors produced. Don't put these in a production environment, however.

error_reporting(E_ALL);
ini_set('display_errors', '1');

Change

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

To this:

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

You have missed out a . (dot) after $answer; it was a syntax error, not a query error.

To make things a bit simpler, you can actually omit the dots completely:

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

Do be aware of SQL injection attacks; use mysql_real_escape_string() to make your query safe(er)


Another issue might be your include file. Try changing

include("db.php");

To

require("db.php");

This will fail if the include file can't be found. In this case, go fix!


I had the same problem, used or die(mysql_error()); and realized I wasn't doing addslashes($string) on one of my variables. It had characters I needed to escape.


I had the same mysterious situation. And worse, on a remote, identical, database the same code was working. The solution was to define a default value (any value) to all the database fields... Hope this helps.


i have been stuck in this problem for a long time the only solution i found is

use mysqli instead of mysql because in newer versions of php these mysql has been deprecated for example

use the following methods

and make sure keep the order correct dirst the variable e.g '$conn' and then database name in the method 'mysqli_select_db($conne,'checksum');'.

similarly in 'mysqli_query($conne,$enter_command);' first variable and then the variable for query.

$conn=mysqli_connect($mysql_host,$mysql_user,$mysql_password);
mysqli_select_db($conne,'checksum'); // checksum is database name
mysqli_query($conne,$enter_command);
mysqli_close($conne);

also make sure to give spaces when entering database table columns

$enter_command = "INSERT INTO dbase (name , lastname , password) VALUES ('".$name."','".$lastname."','".$password."')";

you can also use this syntax

$enter_command = "INSERT INTO dbase (name , lastname , password) VALUES ('{$name}','{$lastname}','{$password}')";

try avoiding sql injection

$gender      = mysql_real_escape_string($_POST['gender']);

hope this will work


Your code is working just fine, The issue here is the database auto increment was not set. This can happen When auto increment is not set. you will not be able to insert additional records beyond one no matter how many times you run your script. If you are having this kind of issue, check to make sure auto increment is enabled.

This is not really an error but the developer of mysql or php should give a warning when the user forgets to enable auto increment so the user has the option to fix it or ignore it.

0

精彩评论

暂无评论...
验证码 换一张
取 消