I have a prepared statement and param with variables, but it's possible that some of these variables may be empty and it gives me an error.. how to treat the code to SKIP the empty param?
CODE:
$stmt = $conn->prepare("insert into account (accountID, firstName, imageURL, sex, cat1) VALUES (:accountID, :firstName, :sourcefilename, :sex, :cat1)");
$stmt->execute(array(
':accountID' => $accountID,
':firstName' => $first_name,
':sourcefilename' => $sourcefilename,
':sex' => $gender,
':cat1' => $cat1));
Related
I cant figure out why this is still giving me a PDO error. I am relatively new to php so any help would be nice.
$query = "INSERT INTO users (userID, email, password, fName, lName, dob,
online, verified, lastLogedIn, gender) VALUES (:id ,:email, :pass, :fn, :ln,
dob, :online, :verified, :lli, :gender )";
$queryParams = array(
':id' => 0,
':email' => $_POST['email'],
':pass' => $_POST['password'],
':fn' => $_POST['firstName'],
':ln' => $_POST[':lastName'],
':dob' => $_POST[':dob'],
':online' => $online,
':verified' => $verified,
':lli' => $lli,
':gender' => $gender
);
try{
$stmt = $db->prepare($query);
$result = $stmt->execute($queryParams);
var_dump($result->errorCode());
}catch(PDOException $ex){
$response["success"] = 0;
$response["message"] = "Database Error. failed to add user to database";
echo '<pre>';
print_r($result);
echo '</pre>';
die(json_encode($ex));
}
My code keeps popping out this error, I know it means there's a mismatch in the query and the database but i cant figure out where.
you have omit : in dob, change to:
$query = "INSERT INTO users (userID, email, password, fName, lName, dob, online, verified, lastLogedIn, gender) VALUES (:id ,:email, :pass, :fn, :ln, :dob, :online, :verified, :lli, :gender )";
You forgot the colon for the dob parameter.
(:id ,:email, :pass, :fn, :ln, :dob, :online, :verified, :lli, :gender
^
Missing : before dob
"INSERT INTO users (userID, email, password, fName, lName, dob,
online, verified, lastLogedIn, gender) VALUES (:id ,:email, :pass,
:fn, :ln, :dob, :online, :verified, :lli, :gender )";
^^
$queryParams = array(
':id' => 0,
':email' => $_POST['email'],
':pass' => $_POST['password'],
':fn' => $_POST['firstName'],
':ln' => $_POST[':lastName'],//might be typo
':dob' => $_POST[':dob'],// might be typo
':online' => $online,
':verified' => $verified,
':lli' => $lli,
':gender' => $gender
);
Your variable might need to be as
$_POST[':lastName'] ===> $_POST['lastName']
$_POST[':dob'] ===> $_POST['dob']
Removed those extra colons
I have an problem I can't seem to solve.
$query = 'INSERT INTO members (username,password,email,active,owner_service) VALUES ( )';
$stmt = $db->prepare($query);
$stmt->bind_param( );
$stmt->execute(array(
':username' => $_POST['username'],
':password' => $hashedpassword,
':email' => $_POST['email'],
':active' => $activasion,
'"$owner_service"' => $POST_['owner_service']
));
'username, password, email and active' are all placeholders but 'owner_service' is a radio button and I don't know how to write it in code if I want to insert it into my table. Do I use ' :xx ' for the placeholders and ' ? ' for my radio button? So the code will look like
(:username, :password, :email, :active, ?)
and in the bind_param write
('$owner_service') ?
i have some problems with execute a PDO.
No error, just nothing happens. The code looks like:
$stmt = $dbh->prepare("INSERT INTO `member_accounts` ('firstname','lastname','email','password') VALUES (:fname,:lname,:e,:pw)");
$arr = array(
':fname' => $_POST['firstname'],
':lname' => $_POST['lastname'],
':e' => $_POST['email'],
':pw' => $_POST['password'],
);
$stmt->execute($arr);
Anyone see the problem? I'm to new at PDO.. Thanks
Remove single quotes here
('firstname','lastname','email','password')
Right statement will be
$stmt = $dbh->prepare("INSERT INTO `member_accounts` (firstname,lastname,email,password) VALUES (:fname,:lname,:e,:pw)");
You can also use backtick(`) with column name but not single quotes.
$stmt = $dbh->prepare("INSERT INTO `member_accounts` (`firstname`,`lastname`,`email`,`password`) VALUES (:fname,:lname,:e,:pw)");
use the following code
$sql="INSERT INTO `member_accounts`
(firstname,lastname,email,password) VALUES
(:fname,:lname,:e,:pw)";
$stmt = $dbh->prepare($sql);
//pdo $stmt is false if any error occur
if($stmt)
{
$arr = array(
':fname' => $_POST['firstname'],
':lname' => $_POST['lastname'],
':e' => $_POST['email'],
':pw' => $_POST['password'],
);
$stmt->execute($arr);
}
i have this
$query = "INSERT INTO players VALUES (
UUID(),
:firstname,
:lastname,
:age,
:birthplace,
:height,
:weight,
:bats,
:throws,
:position,
:jersey,
:status,
:team,
:image_path
)";
$sth = $db->prepare($query);
$sth->execute(array(
':firstname' => $firstname,
':lastname' => $lastname,
':age' => $age,
':birthplace' => $birthplace,
':height' => $height,
':weight' => $weight,
':bats' => $bats,
':throws' => $throws,
':position' => $position,
':jersey' => $jersey,
':status' => $status,
':team' => $team,
':image_path' => $image_path
));
$id = $db->lastInsertId();
return $id;
and i'm trying to return the last ID that was inserted, and all i'm getting is a 0 returned.
any help is greatly appreciated
thanks
LAST_INSERT_ID() and friends will work only for integer IDs that are created via an AUTO_INCREMENT column. You need to run two queries - first
SELECT UUID() AS newuuid;
then fetch and store this result (e.g. in $uuid), then
"INSERT INTO players VALUES (
:uuid,
:firstname,
:lastname,
...
execute(array(
':uuid' => $uuid,
':firstname' => $firstname,
':lastname' => $lastname,
':age' => $age,
leaving you with the $uuid still valid.
I am trying to use a simple MySQL insert query with the parameters in array form. It keeps telling me the number of parameters are wrong. I have tried the following, all producing the same error:
$stmt3 = $link->prepare('INSERT INTO messages VALUES(null, :room, :name, :message, :time, :color)');
$stmt3->execute(array(':room' => $Clean['room'],':name' => $Clean['name'],':message' => $Clean['message'],':time' => $time,':color:' => $Clean['color']));
and
$stmt3 = $link->prepare('INSERT INTO messages VALUES(:null, :room, :name, :message, :time, :color)');
$stmt3->execute(array(':null' => null, ':room' => $Clean['room'],':name' => $Clean['name'],':message' => $Clean['message'],':time' => $time,':color:' => $Clean['color']));
as well as declaring the columns specifically to avoid the null insert:
$stmt3 = $link->prepare('INSERT INTO messages (room, name, message, time, color) VALUES(:room, :name, :message, :time, :color)');
$stmt3->execute(array(':room' => $Clean['room'],':name' => $Clean['name'],':message' => $Clean['message'],':time' => $time,':color:' => $Clean['color']));
This is my first time using PDO (I normally use mysqli, but my current shared host does not have the mysqlnd plugin, preventing me from using prepare(), so any insight from that point of view is appreciated.
The problem - and you will kick yourself - is with :color.
The array key for the value you are passing for that marker when calling execute() is named :color:. Remove the trailing : (I'm guessing this was just a typo anyway).
$stmt3->execute(array(
':room' => $Clean['room'],
':name' => $Clean['name'],
':message' => $Clean['message'],
':time' => $time,
':color' => $Clean['color'],
));
I might be wrong here, but as far as I know you need to do this:
$stmt3->bindParam(':room', $Clean['room']);
$stmt3->bindParam(':name', $Clean['name']);
//and so on
But as a personal preference, I've always done it like this
$stmt3 = $link->prepare('INSERT INTO messages VALUES(null, ?, ?, ?, ?, ?)');
$stmt3->execute(array($Clean['room'], $Clean['name'], $Clean['message'], $time, $Clean['color']))