tags:

views:

62

answers:

4
+1  Q: 

PHP unwanted error

There isnt an actual error, but it's still displaying an error message when a user who is not the buyer or seller goes to the page. Is it possible to suppress the error message?

Direct link to images

alt text

alt text

  //transaction id
$transactionid = $_GET['id'];

//Retrieve info about transaction
$query = "SELECT ads.*, feedback.*, transactions.* FROM (ads INNER JOIN transactions ON ads.id=transactions.ad_id) INNER JOIN feedback ON transactions.id=feedback.transaction_id WHERE transaction_id = '$transactionid'";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
$seller = $row['seller'];
$buyer = $row['buyer'];

//check if user is buyer or seller
if ($_SESSION['user_id'] == $seller) {
    $query = "SELECT * FROM feedback WHERE transaction_id = '$transactionid' AND seller_comment IS NULL";
    $data1 = mysqli_query($dbc, $query);
} else if ($_SESSION['user_id'] == $buyer) {
    $query = "SELECT * FROM feedback WHERE transaction_id = '$transactionid' AND buyer_comment IS NULL";
    $data1 = mysqli_query($dbc, $query);
}

//if user is buyer/seller echo form to them to submit feedback
if (mysqli_num_rows($data1) == 1) {
    echo '<p><form method="post" action="feedback.php?id=' . $transactionid . '&action=submitfeedback">
    <textarea id="feedback" name="feedback" rows="10" cols="30"></textarea><br/>
<input type="submit" value="Submit" name="submit" /></form></p>';
} else {
    echo '<p>feedback already given</p>';
}
A: 

Try

@mysqli_num_rows

@ suppresses errors for that function

However

it is best to remove the source of the error.

if (false !== $data1 && 1 === mysqli_num_rows($data1)) {
  // submit comment
} else {
  // comment already submitted
}
Simon
Hiding error messages instead of solving the underlying problem is not a good idea.
Artefacto
Bad idea. He should sort out the root of the problem.
Pekka
-1 Fix errors, don't hide them
Mark Baker
+1  A: 

What if $_SESSION['user_id'] does not equal $seller OR $buyer. Then $data1 never gets set and is null when passed into num_rows.

To suppress it put an @ before the function, like:

@mysqli_num_rows(...)

OR check for !$data before you do the num_rows:

if(!$data) {
    // not a buyer or seller
} else {
    // do the mysqli_num_rows
}

I would do the latter, it is much cleaner and clear.

Bob Fincheimer
+1: The root of the problem is an imperfectly designed program flow.
Pekka
Suppression is not a very good coding practice. The idea behind the warning is so we can fix the issues.
cdburgess
The error supression is _really_ silly - it irritates me to no end in code I have to use.If you want to stop errors from breaking the output, follow JCD's suggestion, don't use the `@`-method.
gnud
+7  A: 

You may want to resolve the warning, not just cover it up. You should be able to resolve this by changing the if statement to:

if(!empty($data1) && mysql_num_rows($data1) == 1) {

The problem is trying to pass NULL to the mysql function.

cdburgess
Thanks that worked!
Jonny
Great! Glad to help. Please consider marking this as your accepted answer.
cdburgess
Its a FALSE not a NULL btw. Your code works either way though.
Simon
Simon, thanks for the clarification. While you are correct the value is FALSE, the DB is interpreting it as NULL as indicated by the warning.
cdburgess
A: 

Try putting something like the following in your php.ini file:

display_errors = Off
log_errors = On
error_log = "error.log"

This will suppress error messages from being displayed on pages and instead output them to a logfile.

JCD
i didnt know about this thanks!
Jonny
-1 That hides the error, yes. However, it's like saying "if there's a gas leak, use air freshener to cover up the smell." Both the smell and the warning message have a reason, namely, to alert you that there is a problem, and that it should be fixed to prevent bigger problems in the future. Fix the cause, not the symptoms.
Piskvor
You're absolutely right, I should have included that these settings should only be used in a production environment.
JCD