If I run my function edit_profile() without any parameter, then empty strings are written in DB. If $input['email'] is empty for example, I'd like UPDATE not to update this column.
I tried to do:
SET email = IF(LENGTH(:email)=0, email, :email),
It didn't work, I'm not sure how to do same as above with PDO.
function edit_profile($input) {
//
$user_id = 1;
//
try {
//
$conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USERNAME, DB_PASSWORD);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('UPDATE users SET
email = :email,
password = :password,
name_surname = :name_surname,
age = :age,
sex = :sex,
education = :education,
avatar = :avatar
WHERE id = :id');
$stmt->execute(array(
':id' => $user_id,
':email' => $input['email'],
':password' => $input['password'],
':name_surname' => $input['name_surname'],
':age' => $input['age'],
':sex' => $input['sex'],
':education' => $input['education'],
':avatar' => $input['avatar']
));
echo $stmt->rowCount(); // 1
//
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
//
}
edit_profile();
Try
UPDATE users SET
email = COALESCE(NULLIF(:email, ''),email),
password = :password,
name_surname = :name_surname,
age = :age,
sex = :sex,
education = :education,
avatar = :avatar
WHERE id = :id
I'm basically trying to use COALESCE (http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce) to use :email only if not null. If it's null, use the old email.
In case :email is empty string and not simply NULL, I've added NULLIF to convert the empty string to NULL ;)
SET email = IF(LENGTH(:email1)=0, email, :email2)
$stmt->execute(array(
':id' => $user_id,
':email1' => $input['email'],
':email2' => $input['email'],
':password' => $input['password'],
':name_surname' => $input['name_surname'],
':age' => $input['age'],
':sex' => $input['sex'],
':education' => $input['education'],
':avatar' => $input['avatar']
));
Related
I'm having the problem that the query I'm passing through a function is duplicating me the rows. Before, instead of the statement->execute(array()), I had PARAM_STR working for each value. However, it started to give me problems. This is the code that is duplicating me the rows.
static public function mdlIngresarUsuario($datos){
$statement = Conexion::conectar()->prepare("INSERT INTO usuarios (id, nombre, usuario, password, rol, estado) VALUES (null, :nombre, :usuario, :password, :rol, :estado)");
$statement->execute(array(
':nombre' => $datos['nombre'],
':usuario' => $datos['usuario'],
':password' => $datos['password'],
':rol' => $datos['rol'],
':estado' => 0
));
if ($statement->execute()) {
return "ok";
} else {
return "error";
}
}
You're executing the statement twice:
$statement->execute(array(
':nombre' => $datos['nombre'],
':usuario' => $datos['usuario'],
':password' => $datos['password'],
':rol' => $datos['rol'],
':estado' => 0
));
and then in the condition of the if statement:
if ($statement->execute()) {
return "ok";
}
Just log the return of the first "execute", and use that as the condition:
$success = $statement->execute(array(
':nombre' => $datos['nombre'],
':usuario' => $datos['usuario'],
':password' => $datos['password'],
':rol' => $datos['rol'],
':estado' => 0
));
if( $success ) { return "ok"; }
This is happening because you call $statement->execute twice, once where you 'create' it and once in the if statement. You should assign the value of the first execute to a variable and use that variable in the if statement, like:
public static function mdlIngresarUsuario($datos)
{
$statement = Conexion::conectar()->prepare("INSERT INTO usuarios (id, nombre, usuario, password, rol, estado) VALUES (null, :nombre, :usuario, :password, :rol, :estado)");
$result = $statement->execute(array(
':nombre' => $datos['nombre'],
':usuario' => $datos['usuario'],
':password' => $datos['password'],
':rol' => $datos['rol'],
':estado' => 0
));
if ($result) {
return "ok";
} else {
return "error";
}
}
I am using the below PHP file to upload XML to MySQL database, but the issue is my table name is Con-1 and cannot use it.
I have error in the below line:
array(
':name' => $data->Con-1[$i]->name,
':address' => $data->Con-1[$i]->address,
':gender' => $data->Con-1[$i]->gender,
':designation' => $data->Con-1[$i]->designation,
':age' => $data->Con-1[$i]->age
)
please help me to solve it
<?php
//import.php
sleep(3);
$output = '';
if(isset($_FILES['file']['name']) && $_FILES['file']['name'] != '')
{
$valid_extension = array('xml');
$file_data = explode('.', $_FILES['file']['name']);
$file_extension = end($file_data);
if(in_array($file_extension, $valid_extension))
{
$data = simplexml_load_file($_FILES['file']['tmp_name']);
$connect = new PDO('mysql:host=localhost;dbname=testing','root', '');
$query = "
INSERT INTO `Con-1`
(name, address, gender, designation, age)
VALUES(:name, :address, :gender, :designation, :age);
";
$statement = $connect->prepare($query);
for($i = 0; $i < count($data); $i++)
{
$statement->execute(
array(
':name' => $data->Con-1[$i]->name,
':address' => $data->Con-1[$i]->address,
':gender' => $data->Con-1[$i]->gender,
':designation' => $data->Con-1[$i]->designation,
':age' => $data->Con-1[$i]->age
)
);
}
$result = $statement->fetchAll();
if(isset($result))
{
$output = '<div class="alert alert-success">Import Data Done</div>';
}
}
else
{
$output = '<div class="alert alert-warning">Invalid File</div>';
}
}
else
{
$output = '<div class="alert alert-warning">Please Select XML File</div>';
}
echo $output;
?>
You should be able to do it using variable variables, so first assign Con-1 to a variable and then substitute this variable for the name in the assignment...
$var = 'Con-1';
$statement->execute(
array(
':name' => $data->$var[$i]->name,
':address' => $data->$var[$i]->address,
':gender' => $data->$var[$i]->gender,
':designation' => $data->$var[$i]->designation,
':age' => $data->$var[$i]->age
)
);
I have a update query using PDO,
but when I execute it, it deletes my record.
public function update($e) {
$sql = 'UPDATE experiences SET company = :company, position = :position, duty = :duty WHERE id = :id';
//$this->operaction($sql, $e);
$id = $e['id'];
$company = $e['company'];
$position = $e['position'];
$duty = $e['duty'];
$pdostmt = $this->db->prepare($sql);
$pdostmt->bindValue(':id', $id, PDO::PARAM_INT);
$pdostmt->bindValue(':company', $company, PDO::PARAM_STR);
$pdostmt->bindValue(':position', $position, PDO::PARAM_STR);
$pdostmt->bindValue(':duty', $duty, PDO::PARAM_STR);
$pdostmt->execute();
}
$e is an array of $_POST array('id' => '6', 'company' => 'webcanada', 'position' => 'web developer', 'duty' => 'build website');
Can anyone suggest I have done wrong?
Thanks.
I'm trying to execute two INSERT statements and I need the last inserted id to do so. I've tried $question_id = $dbh->lastInsertId();, but it doesn't work. Now I'm executing an additional SELECT LAST_INSERT_ID() statement, but that doesn't work either. I keep getting this error: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined, which occurs because $question_id is empty because selecting the last insert id doesn't seem to work.
Here's my code:
public function add_question($user_id, $group_id, $title, $caption, $datetime, $status) {
// Add user to database
try {
$dbh = new DBHandler();
$sql =
"INSERT INTO question(
user_id,
group_id,
title,
caption,
created_date_time,
question_status
)
VALUES(
:user_id,
:group_id,
:title,
:caption,
:created_date_time,
:question_status
)";
$stmt = $dbh->get_instance()->prepare($sql);
$stmt->execute(
array(
':user_id' => $user_id,
':group_id' => $group_id,
':title' => $title,
':caption' => $caption,
':created_date_time' => $datetime,
':status' => $status
)
);
//$question_id = $dbh->lastInsertId();
$sql= "SELECT LAST_INSERT_ID() AS question_id";
$stmt = $dbh->get_instance()->prepare($sql);
$stmt->execute();
// Resultset
$result = $stmt->fetchAll();
foreach($result AS $question_id_row) {
$question_id = $question_id_row['question_id'];
}
$sql =
"INSERT INTO notification(
n_question_id,
n_question_user_id,
n_question_group_id,
n_question_title
)
VALUES(
:n_question_id,
:n_question_user_id,
:n_question_group_id,
:n_question_title
)";
$stmt = $dbh->get_instance()->prepare($sql);
$stmt->execute(
array(
':n_question_id' => $question_id,
':n_question_user_id' => $user_id,
':n_question_group_id' => $group_id,
':n_question_title' => $title
)
);
echo 'Question added!';
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
Your query fails with invalid number of parameter and parameter was not defined because you define this parameter
:question_status
but you bind
':status' => $status
change
$stmt->execute(
array(
':user_id' => $user_id,
':group_id' => $group_id,
':title' => $title,
':caption' => $caption,
':created_date_time' => $datetime,
':question_status' => $status //here
)
);
and it will work
Why do I not get a result in the following code and num_rows returns 0?
Here is my code:
function get_user($user_id) {// This function give some data from table user with user_id
try {
$connection = start_service();
$user_id = sanitize_string($user_id);
$stmt = $connection->prepare(
"SELECT user_id, name, lname, gender, password, avatar, bdate, user_group_id, status, signup_date, last_signin
FROM user
WHERE user_id = ?
");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$stmt->bind_result($user_id, $name, $lname, $gender, $password, $avatar, $bdate, $user_group_id, $status, $signup_date, $last_signin);
if($stmt->num_rows){
while($stmt->fetch()) {
array_push($result, array(
"user_id" => $user_id,
"name" => $name,
"lname" => $lname,
"gender" => $gender,
"password" => $password,
"avatar" => $avatar,
"bdate" => $bdate,
"user_group_id" => $user_group_id,
"status" => $status,
"signup_date" => $signup_date,
"last_signin" => $last_signin
));
}
} else {
$result = false;
}
print $stmt->num_rows;
$stmt->close();
end_service();
} catch (exception $e) {
end_service_error();
echo $e->getMessage(), "\n";
throw $e;
}
return $result;
}
Where is the problem and what must I do to fix it?
I think you need to call mysqli_stmt_store_result() before calling mysqli_stmt_num_rows().
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($user_id, $name, $lname, $gender, $password, $avatar, $bdate, $user_group_id, $status, $signup_date, $last_signin);
if($stmt->num_rows){
...
}