Here is the Table i Created in database...

    CREATE TABLE  `a1878876_uonrm`.`saveproposal` (
    `ID` INT( 20 ) NOT NULL AUTO_INCREMENT ,
    `PI` VARCHAR( 500 ) NOT NULL ,
    `Email` VARCHAR( 200 ) NOT NULL ,
    `RTitle` VARCHAR( 500 ) NOT NULL ,
    `Coauthors` VARCHAR( 500 ) NOT NULL ,
    `ExSummary` VARCHAR( 500 ) NOT NULL ,
    `LReview` VARCHAR( 500 ) NOT NULL ,
    `Objective` VARCHAR( 500 ) NOT NULL ,
    `Methodology` VARCHAR( 500 ) NOT NULL ,
    `EOutput` VARCHAR( 500 ) NOT NULL ,
    `References` VARCHAR( 500 ) NOT NULL ,
    `RDuration` VARCHAR( 500 ) NOT NULL ,
    PRIMARY KEY (  `ID` )
    ) ENGINE = MYISAM COMMENT =  'This Table save the proposals'

Here is the PHP Code to insert data into table.....

     $sqldel = "DELETE FROM saveproposal WHERE PI='$peru' AND RTitle='$title' AND Email='$email'";
     $resdel = mysqli_query($con, $sqldel);   

    $sqls = "INSERT INTO saveproposal (ID, PI, Email, RTitle, Coauthors, ExSummary, LReview, Objective, Methodology, EOutput, References, RDuration) VALUES (NULL, '$peru', '$email', '$title', '$auth', '$eta', '$pta', '$ota', '$mta','$eota','$rta','$rdt');";
    $result = mysqli_query($con,$sqls);

    if($result)
    { 
         header("Location:");
    }else{
            $erro ="$peru,$email,$title,$auth,$eta,$pta,$ota,$mta,$eota,$rta,$rdt";

            echo "Data is:".$erro;

            echo "SQL Query to execute: $sqls";

            die('Invalid query: ' . mysql_error());
    }

Here is the out put i am getting.... Insetead of data insert...

OUTPUT:

Data is: Mohammed Shafakhatullah Khan,shafakhat@unizwa.edu.om,One,Nine,Two,Three,Four,Five,Six,Seven,Eight
SQL Query to execute: INSERT INTO saveproposal (ID, PI, Email, RTitle, Coauthors, ExSummary, LReview, Objective, Methodology, EOutput, References, RDuration) VALUES (NULL, 'Mohammed Shafakhatullah Khan', 'shafakhat@unizwa.edu.om', 'One', 'Nine', 'Two', 'Three', 'Four', 'Five','Six','Seven','Eight');
Invalid query:

Please help me out!!!!!! I don't understand why INSERT QUERY IS NOT WORKING

Dani AI

Generated

Short summary: the INSERT failed because a column name was a MySQL reserved word — pointed that out and confirmed the fix. Two other common issues in situations like this are (a) using the wrong error function so the DB error is hidden, and (b) an incorrect redirect or output sent before a redirect.

Practical debugging and fixes to keep on the page:

Use mysqli error reporting during development so you see the real SQL error. Example:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

if (!mysqli_query($con, $sql)) {
    printf("Query failed: %s\n", mysqli_error($con));
    exit;
}

Make redirects correctly and make sure no output is sent before calling header:

header("Location: http://example.com/subdone.php");
exit;

Avoid fragile column names. Quoting reserved words with backticks will work, but renaming the column to a nonreserved, descriptive name is cleaner and reduces future surprises.

A few extra best practices that address similar failures:

  • Use prepared statements (mysqli or PDO) instead of building SQL with interpolated variables to avoid quoting bugs and SQL injection.
    $stmt = $con->prepare("INSERT INTO your_table (col1, col2) VALUES (?, ?)");
    $stmt->bind_param("ss", $val1, $val2);
    $stmt->execute();
  • Set the connection charset: mysqli_set_charset($con, "utf8mb4");
  • Use appropriate column types for long text (TEXT/LONGTEXT) instead of forcing large VARCHARs.
  • Prefer InnoDB for transactional safety and foreign key support.
  • Turn on PHP error reporting while debugging: error_reporting(E_ALL); ini_set("display_errors", 1);

These steps will both reveal the real error (so the cause is obvious) and make the insert logic more robust going forward.

Recommended Answers

All 3 Replies

The problem is in the name of the References field (column) which is a Mysql reserved word and should not be used as a field name. If you still wish to use it as a field name you should enclose it in backticks. I would recommend you change it so you avoid possible errors in future.

$sqls = "INSERT INTO saveproposal (ID, PI, Email, RTitle, Coauthors, ExSummary, LReview, Objective, Methodology, EOutput, `References`, RDuration) VALUES (NULL, '$peru', '$email', '$title', '$auth', '$eta', '$pta', '$ota', '$mta','$eota','$rta','$rdt');";

Se the list of mysql reserved words .

Thank you very much broj1 you are absolutely correct, i too figured out that thing on the same day as soon as i post it. You are dammn good to trace that really you belive it or not so many did not have solution for this question.

Thanks a lot for your kind help.....

No worries, mate .-) Please mark this as solved. Happy coding in 2015.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.