1

First time trying to make a website and all other connections work excepts for this specific one. I am trying to connect to the server and update a non-specific variable. However, fails when I check if(!mysqli_stmt_prepare($stmt, $sql))

function addWeight($conn, $location, $weight){
require_once "dbh.inc.php";
$sql ="UPDATE userWeight SET ?=? WHERE userId = ?;";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql);
if (!mysqli_stmt_prepare($stmt, $sql)) {
    header("location: ../profile.php?error=sqlerror");
    exit();
}
mysqli_stmt_bind_param($stmt, "ssi",$location,$weight, $_SESSION["userid"]);
mysqli_stmt_execute($stmt);

//$result = mysqli_query($conn, $sql);
$result =mysqli_stmt_get_result($stmt);
    
$resultCheck = mysqli_num_rows($result);

// close statment 
mysqli_stmt_close($stmt);

}

where the function is called

<?php 
if(isset($_POST["submit"])){
    require_once "dbh.inc.php";
    require_once "functions.inc.php";
    
    session_start();
    
    $weight = $_POST['Weight'];
    // check if weight is full, if so shift all down and replace last 
    if($_SESSION['numWeight'] == 20){
        echo "WEIGHT IS FULL";
    }else{ // just add to next 
        $add = $_SESSION['numWeight']+1;
        $location ="Weight";
        $location .= strval($add);
    
        addWeight($conn, $location, $weight);
        
    }
}
Dharman
  • 26,923
  • 21
  • 73
  • 125
  • Welcome to Stack Overflow. It looks like there's two calls to `mysqli_stmt_prepare` in `addWeight`. Maybe try calling it just once? Also, the [mysqli_stmt_prepare documentation](https://www.php.net/manual/en/mysqli-stmt.prepare.php) says that question marks can't be used "to specify both operands of a binary operator such as the = equal sign." – Nathan Mills May 16 '21 at 22:16

1 Answers1

0

It looks like there's two calls to mysqli_stmt_prepare in addWeight. Maybe try calling it just once? Also, the mysqli_stmt_prepare documentation says that question marks can't be used "to specify both operands of a binary operator such as the = equal sign."

Sagar V
  • 11,606
  • 7
  • 43
  • 64
Nathan Mills
  • 1,902
  • 2
  • 8
  • 13
  • That was it. It did not like that I was trying to give a location and the value to replace in that location. Is there a way to dynamically change location or am I going to have to say if($location == "Weight#"){ do something } else if .... 20 times? – Firerexman007 May 17 '21 at 00:50