1
votes

According to everything I've found and seen, this seems correct. When I print $query the outcome is the following:

"INSERT INTO customers (FirstName, MiddleInit, LastName, Address, City, State, Zip, Email, Gender) VALUES (?,?,?,?,?,?,?,?,?)"

The parameters should have been filled in with the variables in bindValues(). So, for example ...

INSERT INTO customers (FirstName, MiddleInit, LastName, Address, City, State, Zip, Email, Gender) VALUES (Bill, A, Hopkins, 123 Ave, ....)

I'd like to stick with this method - it is surrounded by a try/catch block. From printing the query variable out I can see that is where the issue is.

What am I missing? I really appreciate you looking!

$query = 'INSERT INTO customers (FirstName, MiddleInit, LastName, Address, City, State, Zip, Email, Gender) VALUES (?,?,?,?,?,?,?,?,?)';
        echo $query;
        $statement = $db->prepare($query);
        $statement->bindValue(1, $firstName);
        $statement->bindValue(2, $middle);
        $statement->bindValue(3, $lastName);
        $statement->bindValue(4, $address);
        $statement->bindValue(5, $city);
        $statement->bindValue(6, $state);
        $statement->bindValue(7, $zip);
        $statement->bindValue(8, $email);
        $statement->bindValue(9, $gender);
        $success = ($statement->execute());
1
no idea, not enough code to provide a solution. whether you are using PDO as the API to connect with and if your variables are indeed correct. Check for errors. - Funk Forty Niner
That isn't how prepared statements work. The statement stays as a query with placeholders (like a function with arguments) and when executed on the database, the bound parameters are injected (like calling the function) - Phil
I'm voting to close this question as off-topic because it is about a misunderstanding of prepared statements - Phil
@Phil Example #2 php.net/manual/en/pdostatement.bindvalue.php states it, or is there something I'm missing? - Funk Forty Niner
I suggest you read en.wikipedia.org/wiki/Prepared_statement. @Fred-ii- I think OP is expecting the $query string to be changed by binding the values - Phil

1 Answers

0
votes

We need more code considering the error but you can try this with prepared statements:

$query = 'INSERT INTO customers (FirstName, MiddleInit, LastName, Address, City, State, Zip, Email, Gender) VALUES (:firstName, :middle, :lastName, :address, :city, :state, :zip, :email, :gender)';
$statement = $db->prepare($sql);
$statement->execute(array(':firstName'=>$firstName, ':middle'=>$middle, ':lastName'=>$lastName, ':address'=>$address, ':city'=>$city, ':state'=>$state, ':zip'=>$zip, ':email'=>$email, ':gender'=>$gender));