1

I need to get the last value of numAlarms and Update it if exist and insert it if it's not exist

if (isset($_POST['send'])) {

if (isset($_GET['stdId'])) {
    $stdId = $_GET['stdId'];
}

if (isset($_GET['subR'])) {
    $subR = $_GET['subR'];
}

        $getInc = $db->prepare("SELECT numAlarms FROM stud_abs_alerts WHERE studId=? AND sub_id=?");
        $getInc->bind_param('ii', $stdId, $subR);
        if ($getInc->execute()) {
            $rest = $getInc->get_result();
            if ($a = mysqli_fetch_array($rest)) {
                $inc = $a['numAlarms'];
                $increment = ++$a;
                $id = "";
                $numAlarms = 1;

                //var_dump($rest);

                $getInfo = $db->prepare('INSERT INTO stud_abs_alerts (id, studId, sub_id, numAlarms) VALUES (?,?,?,?)
                ON DUPLICATE KEY UPDATE numAlarms=VALUES(?)');
                $getInfo->bind_param('iiiii', $id, $stdId, $subR, $numAlarms, $numAlarms);

                if ($getInfo->execute()) {
                    echo "done";
                } else {
                    echo "Error";
                }
            }
        }
    }

GOT : Fatal error: Call to a member function bind_param() on a non-object

hagello
  • 2,843
  • 2
  • 27
  • 37
Yousef Altaf
  • 2,631
  • 4
  • 46
  • 71
  • Try var_dump($getInfo); it seems to be failing during the prepare, and will therefore return false instead of an object. If you turn on PDO exceptions it should give you a useful error message. Also I think you are missing a variable in your bind_param, you need to add another $numAlarms – rjdown Nov 08 '15 at 18:14
  • @rjdown thanks yes I was missing `$numAlarms` now it did put it but still the same – Yousef Altaf Nov 08 '15 at 18:22
  • Well, turn on PDO exceptions and see what is causing prepare to return false. See here for how to do that http://stackoverflow.com/questions/8992795/set-pdo-to-throw-exceptions-by-default – rjdown Nov 08 '15 at 18:36
  • @rjdown Thanks in fact I am using mySqli so I am looking now at this [link](http://php.net/manual/en/mysqli.error.php) – Yousef Altaf Nov 08 '15 at 20:59
  • Oops my bad, yep that'll work – rjdown Nov 08 '15 at 21:00

1 Answers1

0

http://www.php.net/manual/en/mysqli.prepare.php

Return Values mysqli_prepare() returns a statement object or FALSE if an error occurred.

Call to a member function bind_param() on a non-object means that $getInfo, is not an object. So an error must have occurred and a boolean is returned instead of an object.

EDIT:

You have some error here:

$getInc = $db->prepare("SELECT numAlarms FROM stud_abs_alerts WHERE studId=? AND sub_id=?");

Probably some invalid field. Try this:

$getInc = $db->prepare("SELECT numAlarms FROM stud_abs_alerts WHERE studId=? AND sub_id=?");
if($getInc == false) {
    echo $mysqli->error;
}
$getInc->bind_param('ii', $stdId, $subR);

And see the error.

Velko Georgiev
  • 684
  • 4
  • 11