0

I have some code to do a simple mysqli procedural INSERT INTO my DB. It looks something like this:

$conn2 = mysqli_connect($servername, $username, $password, $dbname_name);
// Check connection
if (!$conn2) {
    die("Connection failed: " . mysqli_connect_error());
}

// prepare and bind
$stmt2 = $conn2->prepare("INSERT INTO db_name (resource_owner, resource_title, resource_text_intro, resource_active) VALUES (?, ?, ?, ?)");
$stmt2->bind_param("issi", $resource_owner, $resource_title, $introduction, $resource_active);

// set parameters and execute
$resource_title = $_POST['resource_title'];
$introduction = $_POST['introduction'];
$resource_active = 0;

$stmt2->execute();

For some reason that I cannot see, this is not inserting into the DB, nor am I getting any error messages. My form simply loops back to the form page (I have it set to redirect to a new page after an insert). I have verified that the $_POST['submit'] is in fact working, so this section of the script is not being skipped over.

Any thoughts?

Sheldon Scott
  • 741
  • 1
  • 7
  • 21

2 Answers2

0

Insert into db_name? change it with your table name inside the query, and remove the first parameter in the bind_param

Douraid Arfaoui
  • 212
  • 2
  • 6
0

More error handling

<?php
$conn2 = mysqli_connect($servername, $username, $password, $dbname_name);
// Check connection
if (!$conn2) {
    die("Connection failed: " . mysqli_connect_error());
}

// prepare and bind
$stmt2 = $conn2->prepare("INSERT INTO db_name (resource_owner, resource_title, resource_text_intro, resource_active) VALUES (?, ?, ?, ?)");
if ( !$stmt2 ) {
    die($conn2->error);
}
else if ( !$stmt2->bind_param("issi", $resource_owner, $resource_title, $introduction, $resource_active) ) {
    die($stmt2->error);
}
else {
    // set parameters and execute
    $resource_title = $_POST['resource_title'];
    $introduction = $_POST['introduction'];
    $resource_active = 0;

    if ( !$stmt2->execute() ) {
        die($stmt2->error);
    }
    else {
        echo 'done.';
    }
}
VolkerK
  • 95,432
  • 20
  • 163
  • 226