2

I have a snippet of code as follows:

$sql = "INSERT INTO tasks
                (task_name, description, due_date, state, userID, importID)
                VALUES (?, ?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_bind_param($stmt, "sssiss", $name, $description, $due, 0, $_SESSION['userID'], $id);

When run in the browser, the browser does not load and a try catch statement does not work so I am not able to identify the exact error. I have an almost identical bit of code which works perfectly. The only difference is that this bit of code is in a for each loop. Will that have something to do with it?

EDIT: I forgot to mention all of this is in an if statement to prepare the statement.

if (!mysqli_stmt_prepare($stmt, $sql)) {
    header("Location: ../import.php?error=sqlinserterror");
    exit();
} else {
    mysqli_stmt_bind_param($stmt, "sssiss", $name, $description, $due, 0, $_SESSION['userID'], $id);
Ryo Suzuki
  • 152
  • 8
  • I added the try catch right around the mysqli_bind_param line with only that line within the try. No error was caught but the browser still did not load. And sorry I am new to php, what is error reporting? I am also certain the bind line is what causes the error as the browser loads when it is commented out. – Ryo Suzuki Mar 23 '20 at 01:53
  • 1. in order to catch the exception you must enable exceptions for mysqli.2. you shouldn't catch that exception in your case whatsoever. – Your Common Sense Mar 23 '20 at 04:06

1 Answers1

2

With bind_param you need to pass in parameter names, not values. Try:

mysqli_stmt_bind_param($stmt, "sssiss", $name, $description, $due, $zero, $user_id, $id);
$zero = 0;
$user_id = $_SESSION['userID'];
user3783243
  • 5,368
  • 5
  • 22
  • 41