0

I have a database table like this and want to update some values

customerNumber (INT)
company (VARCHAR)
contactPerson (VARCHAR)
phoneNumber (VARCHAR)
username (VARCHAR)
password (VARCHAR)
email (VARCHAR)
create_at (TIMESTAMP)

This is my update fuction

public function updateUser()
{
    //UPDATE datadump SET content=? WHERE id=?
    $query =  'UPDATE customer SET (company, contactPerson, phoneNumber, email) VALUES (?, ?, ?, ?) WHERE customerNumver = ?';

    $paramType = 'ssssi';
    $ParamValue = array(
        $_POST["CustomerCompany"],
        $_POST["CustomercontactPerson"],
        $_POST["CustomerphoneNumber"],
        $_POST["CustomerEmail"],
        $_POST["customerNumber"]
    );
    $this->ds->insert($query, $paramType, $ParamValue);
}

I'am calling insert because what I read should act as an update. Or does it not?

public function insert($query, $paramType, $paramArray)
{
    $stmt = $this->conn->prepare($query);
    $this->bindQueryParams($stmt, $paramType, $paramArray);

    $stmt->execute();
    $insertId = $stmt->insert_id;
    return $insertId;
}

I get the error in the log

thrown in /opt/lampp/htdocs/lib/DataSource.php on line 158 [23-Feb-2021 10:38:57 Europe/Berlin] PHP Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, first array member is not a valid class name or object in /opt/lampp/htdocs/lib/DataSource.php:158 Stack trace: #0 /opt/lampp/htdocs/lib/DataSource.php(158): call_user_func_array(Array, Array) #1 /opt/lampp/htdocs/lib/DataSource.php(118): DataSource->bindQueryParams(false, 'ssssi', Array) #2 /opt/lampp/htdocs/Model/Customer.php(293): DataSource->insert('UPDATE customer...', 'ssssi', Array) #3 /opt/lampp/htdocs/modify-address.php(28): Customer->updateUser() #4 {main} thrown in /opt/lampp/htdocs/lib/DataSource.php on line 158

This is line 158 in DataSource.php

public function bindQueryParams($stmt, $paramType, $paramArray = array())
    {
        $paramValueReference[] = & $paramType;
        for ($i = 0; $i < count($paramArray); $i ++) {
            $paramValueReference[] = & $paramArray[$i];
        }
        call_user_func_array(array(
            $stmt,  //this is line 158
            'bind_param'
        ), $paramValueReference);
    }

Is the problem that I do not have all the fields in my database in my prepere statement? I do not want to update all fields, how do I do or can I do as I do?

Does it work to use my insert function or how should I modify it?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Xtreme
  • 1,601
  • 7
  • 27
  • 59
  • 2
    The error you're getting is because you're passing in invalid arguments to the function `call_user_func_array()` _"Uncaught TypeError: call_user_func_array()"_. Why are you even using `call_user_func_array()` instead of just use it directly: `$stmt->bind_param(....)`? This looks like a case of over engineering for no reason. – M. Eriksson Feb 23 '21 at 10:13
  • And no, you don't need to update all fields. It's enough to update the ones you want to change. The only thing I can see with your `insert()` method that won't work for updates is `$insertId = $stmt->insert_id;`. Since updates doesn't add new rows, there won't be any "insert_id". – M. Eriksson Feb 23 '21 at 10:21
  • @MagnusEriksson $stmt in the call_user_func_array is the $query. Can you see anything wrong with my $query? – Xtreme Feb 23 '21 at 10:48
  • Use the correct format: `UPDATE tablename SET col1 = ?, col2 = ?, ... WHERE id = ?`. – M. Eriksson Feb 23 '21 at 13:37

0 Answers0