You are doing several things wrong.
$vs = “:ip, flt_status, sunrise”;
First, you're apparently using smart quotes instead of straight quotes. Code needs straight quotes.
Next, you put a :
prefix before ip
but you missed that prefix before the other two named parameters. You need a :
before each one.
$match = array(
‘:ip’=>”$ip”,
‘:flt_status’=>”null, PDO::PARAM_INT”,
’:sunrise’=>”$sunrise”
);
Next, you put null
inside a quoted string. That makes it a string literal containing the word 'null'
, not a true null. These two concepts are different, like the difference between a chicken and a piece of paper with the word "chicken" written on it.
Also, you don't need to put quotes around the variables.
Also, for what it's worth, the :
prefix on the array keys of your array are optional. In early versions of PDO, they were mandatory, but now they're not. It does no harm to keep the colon prefix, I just wanted to let you know because it could make it easier in the future to prepare arrays of parameters from another associative array, like $_POST.
$vars = “ip, flt_status, sunrise”;
This is fine except for the continued use of smart quotes.
$sql = "INSERT INTO skeds ( $vars ) VALUES ( $vs )";
Here's where you will get into trouble with your $vs
because it contains only one parameter placeholder, followed by two plain column names. Passing column names in the VALUES clause is not illegal in SQL, but it makes no sense to do it.
$query = $db_found->prepare( $sql );
$query->execute( $match );
Okay, except that you are not checking for errors. Unless you have enabled PDO's attribute for throwing exceptions, you need to check the return status of prepare()
and execute()
because they return false if there's any error. This continues to be one of the most common mistakes among PHP developers!
Here's how I would write this code.
As you connect to PDO, enable exceptions. See http://php.net/manual/en/pdo.error-handling.php
$db_found = new PDO(...);
$db_found->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Then the code for this routine:
$placeholders = ":ip, :flt_status, :sunrise";
$values = array(
'ip' => $ip,
'flt_status' => null,
'sunrise' => $sunrise
);
$columns = "ip, flt_status, sunrise";
$sql = "INSERT INTO skeds ($columns) VALUES ($placeholders)";
$stmt = $db_found->prepare($sql);
$stmt->execute($values);