tags:

views:

43

answers:

4

In this statement, I am trying to see if there if the latest posting in the database that has the exact same title, price, city, state, detail. If there is, then it would say to the user that the exact post has been already made; if not then insert the posting into the dbc. (This is one type of check so that users can't accidentally post twice. This may not be the best check, but this statement error is annoying me, so I want it to work :))

Why won't this sql work? I think it's not letting the title=$title and not getting the value in the $title...

ERROR: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in postad.php on line 365

//there is a form that users fill out that has title, price, city, etc
<form>
blah blah
</form>

     //if users click submit, then does all the checks and if all okay, insert to dbc
    if (isset($_POST['submit'])) 
    {

    // Grab the pposting data from the POST and gets rid of any funny stuff
    $title = mysqli_real_escape_string($dbc, trim($_POST['title']));
    $price = mysqli_real_escape_string($dbc, trim($_POST['price']));
    $city = mysqli_real_escape_string($dbc, trim($_POST['city']));
    $state = mysqli_real_escape_string($dbc, trim($_POST['state']));
    $detail = mysqli_real_escape_string($dbc, trim($_POST['detail']));

     if (!is_numeric($price) && !empty($price))
     {
        echo "<p class='error'>The price can only be numbers. 
            No special characters, etc</p>";
    }

    //Error problem...won't let me set title=$title, detail=$detail, etc.            
     //this statement after all the checks so that none of the variables are empty
    $query="Select * FROM posting 
WHERE user_id={$_SESSION['user_id']}
AND title=$title
AND price=$price
AND city=$city
AND state=$state
AND detail=$detail";
$data = mysqli_query($dbc, $query);
if(mysqli_num_rows($data)==1) 
{
    echo "You already posted this ad. Most likely caused by refreshing too many times.";
}

}

A: 

Try num rows condition like this

if(mysqli_num_rows($data)>0) {
}
Karthik
+1  A: 

mysqli_query($dbc, $query) is returning FALSE, which you are assigning to $data and passing to mysqli_num_rows

See this page to see what may be causing it to return false.

Matt
The weird part is that if I say "$title'Selling Apples'", then the query would work if there is a title of that. I don't think the title=$title is working. wierd...
ggfan
Try to check if the $query is boolean type. If so, You can try find out what happend by using mysqli_error().
Michał Mech
A: 

Manual (mysqli_query()):

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query()  will return a result object. For other successful queries mysqli_query()  will return TRUE. 

So You have error in query.

Michał Mech
+2  A: 

Wow guess I should go back to basic Mysql rules...

$query="Select * FROM posting 
WHERE user_id='{$_SESSION['user_id']}'
AND title='$title'
AND price='$price'
AND city='$city'
AND state='$state'
AND detail='$detail'
";

Those ' ' was what caused the error

ggfan
"I should go back to basic Mysql rules..." - or make a big step forward by using prepared, parametrized statements ;-) see http://docs.php.net/mysqli.prepare and http://docs.php.net/pdo.prepared-statements
VolkerK