Rawr
Rawr

Reputation: 2224

Bind Param fails with empty variable

$query = "INSERT INTO api_fails (file, code, url, date) VALUES (?, ?, ?, ".time().")";

$stmt = $mysqli->stmt_init();
if(!$stmt->prepare($query)) {
    echo("Failed to prepare statement.<br />\n");
} else {
    $file = $_GET['f'];
    $code = $_GET['c'];
    $url = $_GET['u'];
    $stmt->bind_param("sis", $file, $code, $url);
    if($stmt->execute()) {
        echo("Inserted.<br />\n");
    }else {
        echo("Didn't insert.<br />\n");
    }
}

I have found if any of the following variables are not set, the insert fails...

    $file = $_GET['f'];
    $code = $_GET['c'];
    $url = $_GET['u'];

How do I setup my insert so that if the variable isn't set it just inserts nothing into that field?

Upvotes: 0

Views: 1245

Answers (1)

John Conde
John Conde

Reputation: 219804

Just assign empty values to those variables if they are not set:

$file = (isset($_GET['f'])) ? $_GET['f'] : '' ;
$code = (isset($_GET['c'])) ? $_GET['c'] : '' ;
$url  = (isset($_GET['u'])) ? $_GET['u'] : '' ;

Upvotes: 2

Related Questions