Jump to content

Query is Empty!!!


yakoup46

Recommended Posts

I am not really sure what is going on, but for some reason I am getting a "Query is Empty" error. This code was suggested to me in another post so I am not completly farmilar with it. I have looked it over but cannot find why I would be getting the error.

 

function checkAnswer($button, $inputbox, $session, $table, $answer) {

    if(isset($_POST[$button]) || $_POST[$inputbox]) {

        $sql="INSERT INTO $table VALUES ('$_POST[$inputbox]')";

        if($_POST[$inputbox] != $answer) {

            echo "Sorry Wrong!";

            session_start(); 

            if(isset($_SESSION[$session])) {

                $_SESSION[$session] = $_SESSION[$session] + 1; // Could use $_SESSION[$session]++;

            } else {

                $_SESSION[$session] = 1;

            }

        } else {

            echo "Correct!";

        }

    echo "<br />Number of incorrect answers = ". $_SESSION[$session];

}

}

 

checkAnswer("buttonsumofodds", "inputbox1", "views1", "Sumofodds", "280");

checkAnswer("buttonsumofchain", "inputbox3", "views3", "Sumofchain", "820");

checkAnswer("buttonsumofsquares", "inputbox2", "views2", "Sumofsquares", "2870");

checkAnswer("buttonsumofprimes", "inputbox5", "views5", "Sumofprimes", "1060");

checkAnswer("buttonsumofdifference", "inputbox4", "views4", "Diffofsums", "-144");

 

echo "<br /><a href='http://luta.comuv.com/Easy/easy.html'>Go Back</a>";

 

if(!mysql_query($sql,$con)) {

    die('Error: ' . mysql_error());

}

 

mysql_close($con);

?>

Link to comment
Share on other sites

I fixed the row thing... but doesnt the bold stuff tell it what table to put it into? :confused:

 

checkAnswer("buttonsumofodds", "inputbox1", "views1", "Sumofodds", "280");

checkAnswer("buttonsumofchain", "inputbox3", "views3", "Sumofchain", "820");

checkAnswer("buttonsumofsquares", "inputbox2", "views2", "Sumofsquares", "2870");

checkAnswer("buttonsumofprimes", "inputbox5", "views5", "Sumofprimes", "1060");

checkAnswer("buttonsumofdifference", "inputbox4", "views4", "Diffofsums", "-144");

Link to comment
Share on other sites

I did as you had suggested an still got the query was empty error! ARRRGHHH!!! LOL

 

function checkAnswer($button, $inputbox, $session, $table, $column, $answer) {

    if(isset($_POST[$button]) || $_POST[$inputbox]) {

        $sql="INSERT INTO $table ($column) VALUES ('$_POST[$inputbox]')";

        if($_POST[$inputbox] != $answer) {

            echo "Sorry Wrong!";

            session_start(); 

            if(isset($_SESSION[$session])) {

                $_SESSION[$session] = $_SESSION[$session] + 1; // Could use $_SESSION[$session]++;

            } else {

                $_SESSION[$session] = 1;

            }

        } else {

            echo "Correct!";

        }

    echo "<br />Number of incorrect answers = ". $_SESSION[$session];

}

}

 

checkAnswer("buttonsumofodds", "inputbox1", "views1", "Sumofodds", "ValueOdds", "280");

checkAnswer("buttonsumofchain", "inputbox3", "views3", "Sumofchain", "ValueChain", "820");

checkAnswer("buttonsumofsquares", "inputbox2", "views2", "Sumofsquares", "ValueSquares", "2870");

checkAnswer("buttonsumofprimes", "inputbox5", "views5", "Sumofprimes", "ValuePrimes", "1060");

checkAnswer("buttonsumofdifference", "inputbox4", "views4", "Diffofsums", "ValueDiff", "-144");

 

echo "<br /><a href='http://luta.comuv.com/Easy/easy.html'>Go Back</a>";

 

if(!mysql_query($sql,$con)) {

    die('Error: ' . mysql_error());

}

 

mysql_close($con);

?>

Link to comment
Share on other sites

You should also move session_start(); out of the function (and above any output, best as the first line of PHP in the script), it is currently attempting to initialize the session after output is given, and every time the function is called.

Link to comment
Share on other sites

<?php
session_start();  //first line of php

function checkAnswer($button, $inputbox, $session, $table, $column, $answer) {

    static $questionNum = 1;

    if(isset($_POST[$button]) || $_POST[$inputbox]) {
        $sql="INSERT INTO $table ($column) VALUES ('" . mysql_real_escape_string($_POST[$inputbox]) . "')";
        
        if(!mysql_query($sql)) {
           die('Error: ' . mysql_error());
        }

        if($_POST[$inputbox] != $answer) {
            echo "<span style=\"color: red; display: block;\">Question " . $questionNum . ". Sorry wrong answer!</span>";
            if(isset($_SESSION[$session])) {
                $_SESSION[$session] = $_SESSION[$session] + 1; // Could use $_SESSION[$session]++;
            } else {
                $_SESSION[$session] = 1;
            }
        } else {
            echo "<span style=\"color: green; display: block;\">Question " . $questionNum . ". Correct!</span>";
        }
    echo "<br />Number of incorrect answers = " . (int)$_SESSION[$session];
}
$questionNum++;
}

checkAnswer("buttonsumofodds", "inputbox1", "views1", "Sumofodds", "ValueOdds", "280");
checkAnswer("buttonsumofchain", "inputbox3", "views3", "Sumofchain", "ValueChain", "820");
checkAnswer("buttonsumofsquares", "inputbox2", "views2", "Sumofsquares", "ValueSquares", "2870");
checkAnswer("buttonsumofprimes", "inputbox5", "views5", "Sumofprimes", "ValuePrimes", "1060");
checkAnswer("buttonsumofdifference", "inputbox4", "views4", "Diffofsums", "ValueDiff", "-144");

echo "<br /><a href='http://luta.comuv.com/Easy/easy.html'>Go Back</a>";

mysql_close($con);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.