skip to Main Content

I am having some issues with my php code.
The website is …update.php?operation=pluseinsgast?variable=test

and the following code:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$connection = mysqli_connect("censored");

// Step 3: Determine the operation (increment or decrement)
$operationa = explode("?", $_GET['operation']);  // Assuming the parameter is passed as '?operation=increment' or '?operation=decrement'

$codea = explode("=", $operationa[1]);
$code = $codea[1];
echo $code;
if ($operationa[0] === 'pluseinsheim') {
    $column = 'heimscore';
    $increment = 1;
} elseif ($operationa[0] === 'pluszweiheim') {
    $column = 'heimscore';
    $increment = 2;
} elseif ($operationa[0] === 'minuseinsheim') {
    $column = 'heimscore';
    $increment = -1;
} elseif ($operationa[0] === 'pluseinsgast') {
    $column = 'gastscore';
    $increment = 1;
} elseif ($operationa[0] === 'pluszweigast') {
    $column = 'gastscore';
    $increment = 2;
} elseif ($operationa[0] === 'minuseinsgast') {
    $column = 'gastscore';
    $increment = -1;
} else {
    // Invalid operation
    echo $operationa[0];
    exit;
}

// Perform the update using prepared statement
$sql = "SELECT `$column` FROM `tuswettbergen50jahre` WHERE `id` LIKE '%$code%'";
$stmt = $connection->prepare($sql);
$stmt->bind_param('s', $code);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 1) {
    $row = $result->fetch_assoc();
    $currentValue = $row[$column];
    $newValue = $currentValue + $increment;
    
    // Update the value in the database
    $updateSql = "UPDATE `tuswettbergen50jahre` SET `$column` = ? WHERE `id` LIKE  '%$code%'";
    $stmt = $connection->prepare($updateSql);
    $stmt->bind_param('i', $newValue); // Updated to 'i' for integer
    $stmt->execute();
    
    // Check if the update was successful
    if ($stmt->affected_rows === 1) {
        echo "Update successful";
    } else {
        echo "Update failed";
    }
} else {
    echo "Record not found";
    echo $code;
}

// Step 4: Update the database
$stmt->close();
$connection->close();
?>

and the following error:
test Fatal error: Uncaught ArgumentCountError: The number of variables must match the number of parameters in the prepared statement in /mnt/web321/b3/67/512425767/htdocs/testenvironment/live/simple/update.php:40 Stack trace: #0 /mnt/web321/b3/67/512425767/htdocs/testenvironment/live/simple/update.php(40): mysqli_stmt->bind_param() #1 {main} thrown in /mnt/web321/b3/67/512425767/htdocs/testenvironment/live/simple/update.php on line 40

what can I do?

I tried to ask ChatGPT. Nothing helped.
My assumption is that the part with LIKE '%$code%'" is Incorrect. But I am yet learning and Dont know for sure.

2

Answers


  1. Chosen as BEST ANSWER

    Solved. Had to update $stmt->bind_param('s', $newValue); to $stmt->bind_param('is', $newValue, $code);


  2. It should be ? instead of $code:

    $sql = "SELECT `$column` FROM `tuswettbergen50jahre` WHERE `id` LIKE ?";
    

    also in the url between parameter you use & not another ?:

    update.php?operation=pluseinsgast&variable=test
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search