0

I keep getting a

Parse error: syntax error, unexpected T_STRING in /newref-exec.php on line 6

I have read loads of different solutions to this problem but non seem to work

<?php
// connect to database
 require($DOCUMENT_ROOT . "connect.php");
// check for blank entries
if ($_POST[doi2] == "NULL"){
    echo "No Data to add";
    }
    else
    {
    $sql="INSERT INTO ref (uid, date, doi, title, year, journal)
    VALUES 
    ('1','CURDATE ()','$_POST[doi2]','$_POST[title2]','$_POST[year2]','$_POST[journal2]')";
    if (!mysqli_query($link,$sql,$con))
        {
        die('The Reference could not be added, beacuse of SQL Error:' . mysql_error());
        }
        echo "New Reference Added";
    }
?>
2
  • 1
    if ($_POST['doi2'] == "NULL"){ Commented Oct 23, 2012 at 16:00
  • What is $DOCUMENT_ROOT, I hope you mean $_SERVER['DOCUMENT_ROOT'] (register_globals should never be on). Commented Oct 23, 2012 at 16:06

2 Answers 2

2

This line, the key needs to have quotes,

if ($_POST['doi2'] == "NULL"){

Also, if you want to check for empty entries, you need to probably do this,

if ($_POST['doi2'] == ""){ //checking "NULL", checks for a string 'NULL'
Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't need to, it should. This will throw a warning, not an error.
There are a lot of other places where quotes is required. And quotes is not required when comparing with null.
0

Rewrite the code in the below way.

<?php
// connect to database
 require($DOCUMENT_ROOT . "connect.php");
// check for blank entries
if ($_POST['doi2'] == null){
    echo "No Data to add";
} else {
        $doi2 = $_POST['doi2'];
        $title2 = $_POST['title2'];
        $year2 = $_POST['year2'];
        $journal2 = $_POST['journal2'];

        $sql="INSERT INTO ref(uid, date, doi, title, year, journal) VALUES('1', 'CURDATE()', '$doi2', '$title2', '$year2', '$journal2')";
        if (!mysqli_query($link, $sql, $con))
        {
            die('The Reference could not be added, beacuse of SQL Error:' . mysql_error());
        }
        echo "New Reference Added";
}
?>

4 Comments

You have a lot of quotes problem. I changed the $_POST from your query and gave variables which i defined before your query.
Also you dont need quotes when comparing with null.
!empty() would be preferable to == null.
In many cases, empty() cannot be used with string variables for the simple fact that empty() returns true if your variable is set the the string value '0'. If the string character '0' is a possibly valid value for your string variable — if all non-zero-length strings are valid — you cannot use empty(). This is even more dangerous if you are using empty() to check if a string variable is defined. Also worth mentioning, empty() returns false for a string value that is nothing but a single space (or any number of spaces really).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.