Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VA

Hie i want to update my database from the form and from this code , i am getting this error >>>>>> "Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\about.php on line 85"

HERE IS LINE 85: $query = "SELECT * FROM $tab_nam WHERE title = $_POST['title'] AND story = $_POST['story']"; Please help !!

<?
                // Allow certain file formats
                //Note: This can be replaced by including a access type
                if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
                && $imageFileType != "gif" ) {
                    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
                    $uploadOk = 0;
                    exit();
                } else { $uploadOk = 1; echo "Good"; echo "</br>"; }

                if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                    //echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";

                    $servername = "127.0.0.1";
                    $username = "root";
                    $password = "";
                    $dbname = "news_db2";
                    $tab_nam= "news_tab";
                    $when=getdate(); $yr=$when["year"]; $mon=$when["month"]; $day=$when["mday"];
                    $zuva=$day." ".$mon." ".$yr;

                    $conn = new mysqli($servername, $username, $password);

                    // Create database
                    $sql = "CREATE DATABASE IF NOT EXISTS $dbname";
                    if ($conn->query($sql) === TRUE) {
                        echo "Database created successfully"."</br>";
                    } else {
                        echo "Error creating database: " . $conn->error."</br>"; 
                    }

                    // Create connection
                    $conn = new mysqli($servername, $username, $password, $dbname);
                    // Check connection
                    if ($conn->connect_error) {
                        die("Connection failed: " . $conn->connect_error);
                    }

                    // sql to create table
                    $sql = "CREATE TABLE IF NOT EXISTS $tab_nam(
                    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                    title VARCHAR(150) NOT NULL,
                    story TEXT(30) NOT NULL,
                    reporter VARCHAR(50),
                    img VARCHAR(50),
                    det VARCHAR(50)
                    )";

                    if ($conn->query($sql) === TRUE) {
                        echo "Table ".$tab_nam." created successfully" ."</br>";
                    } else {
                        echo "Error creating table: " . $conn->error."</br>"; }

                    //Checking if newsupdate has been added to the database
                    $query = "SELECT * FROM $tab_nam WHERE title = $_POST['title'] AND story = $_POST['story']";

                    $result = mysql_query($query);

                    if ( mysql_num_rows ( $result ) > 1 )
                    {
                        /* Username already exists */
                        echo 'Username already exists. </br>';
                    }
                    else
                    {
                        //sql Inserting data into the news table
                        $sql = "INSERT INTO $tab_nam (title, story, reporter, img, det)
                        VALUES ($_POST['title'], $_POST['story'], $_POST['reporter-name'], $_FILES[file]['img'], $zuva)";

                        if ($conn->query($sql) === TRUE) {
                            echo "New record created successfully. <br>";
                        } else {
                            echo "Error: " . $sql . "<br>" . $conn->error;
                        }
                    }

                    $conn->close();

                    header("Location: about.php?status=posted");

                    exit();

edited to format code.

1 Answer

I think your problem is that your $_POST['title'] returns a string but you do not have quotes around the variable. Try adding double quotes around $_POST and your other string returns and see what happens.