简体   繁体   中英

Can't get database to UPDATE

Let me try to explain what I have going on here. This is a members area and when a button is clicked, this script is called. Ideally, it subtracts the cost of the item (points) and updates the database with the new points variable.

Script:

<?php
session_start();
if ($_SESSION['email'])
{
    $dbemail=$_SESSION['email'];

//connecting here
    // Check connection
    if (mysqli_connect_errno())
    {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM tsfs_members WHERE email = '$dbemail'");
    $row = mysqli_fetch_array($result);

//putting rows into variables to make calling them easier
    $id = $row["id"];
    $firstname = $row["firstname"];
    $lastname = $row["lastname"];
    $email = $row["email"];
    $streetaddress = $row["streetaddress"];
    $city = $row["city"];
    $state = $row["state"];
    $zip = $row["zip"];
    $points = $row["points"];
    $date = $row["date"];

$newpoints = $points - '75';

$sql = "UPDATE tsfs_members SET points='50' WHERE id=?";

    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param('s',$id);
    $stmt->execute();       

    echo "Thanks! Your have" . $newpoints . "left in your account";
   }
   else 
{
  echo "<div style='text-align:center; color:#ff0000; font-size:200%; margin-top:40px; font-weight:bold;'>You must be a registered user!</div>";
}

?>

This is throwing an error of Call to a member function prepare() on a non-object

Any help will be greatly appreciated!

Another question you asked contains:

mysqli_query($con,"UPDATE feedback SET approved=1 WHERE approved='0'");

so do the same for this one.

As in:

$stmt = $con->prepare($sql);

and not:

$stmt = $mysqli->prepare($sql);

since you're already passing DB connection using $con in:

$result = mysqli_query($con,"SELECT...

$mysqli is undefined, and that is where your non-object error comes from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM