What I'm trying to do is:
If the age input in my form = 28, 30, 25 or 21 then I want to auto insert value 8 in the column (VE), else keep it empty. Is this the right way to do that?
if($form_data->action == 'Insert')
{
$age=array(28, 30, 25, 21);
$age_str=implode("','", $age);
if($form_data->age == $age_str){
$query="INSERT INTO tbl
(VE) VALUE ('8') WHERE id= '".$form_data->id."'
";
$statement = $connect->prepare($query);
$statement->execute();
}
$data = array(
':date' => $date,
':first_name' => $first_name,
':last_name' => $last_name,
':age' => $age
);
$query = "
INSERT INTO tbl
(date, first_name, last_name, age) VALUES
(:date, :first_name, :last_name, :age)
";
$statement = $connect->prepare($query);
if($statement->execute($data))
{
$message = 'Data Inserted';
}
}
Also, how do I insert the new row with the row id from the other form data going into tbl?
Use php's in_array instead of trying to compare a string. To get the id of the query where you insert the form data, you can return the id of the insert row from your prepared statement.
if ($form_data->action == 'Insert') {
// assuming $age, $date, $first_name, $last_name
// already declared prior to this block
$data = array(
':date' => $date,
':first_name' => $first_name,
':last_name' => $last_name,
':age' => $age
);
$query = "
INSERT INTO tbl
(date, first_name, last_name, age) VALUES
(:date, :first_name, :last_name, :age)
";
$statement = $connect->prepare($query);
if ($statement->execute($data)) {
$message = 'Data Inserted';
// $id is the last inserted id for (tbl)
$id = $connect->lastInsertID();
// NOW you can insert your child row in the other table
$ages_to_insert = array(28, 30, 25, 21);
// in_array uses your array...so you don't need
// if($form_data->age == $age_str){
if (in_array($form_data->age, $ages_to_insert)) {
$query="UPDATE tbl SER VE = '8' WHERE id= '".$id."'";
$statement2 = $connect->prepare($query);
$statement2->execute();
}
}
}
Related
$query = "INSERT INTO news VALUES (NULL, :param1 , :param2 )";
$stmt = $pdo->prepare($query);
$params = array(
"param1" => $p['title'],
"param2" => $p['body'],
);
$data = $stmt->execute($params);
// here i would like get current inserted ID. Is possible?
$id = $data->id ???? ;
How can i make this?
$query = "INSERT INTO news VALUES (NULL, :param1 , :param2 )";
$stmt = $pdo->prepare($query);
$params = array(
"param1" => $p['title'],
"param2" => $p['body'],
);
$data = $stmt->execute($params);
so you can do like this to get last inserted Id
$last_id = $pdo->lastInsertId();
Use :
$last_insert_id = $pdo->lastInsertId();
You could use PDO::lastInsertId
$last_insert_id = $pdo->lastInsertId();
This is the table where I´m trying to the the insert.
When i try to make a insert using pdo I get the following error:
Array ( [0] => HY093 [1] => [2] => )
All of the information comes from a html form.
The connection to the db is working because before I do this insert I do a fetch so that's not the problem.
I already checked all the variables with echo and they are correct.
Also tried to add the column 'id' to the sql, and give it the value NULL, but the error is the same as the above.
But since the column 'id' is auto incremented i didn't put it in the sql. See my code above to understand what I did.
$nome = $_POST['name'];
$mail = $_POST['email'];
$psw = $_POST['pass'];
$ni = $_POST['nif'];
This two variables comes from a fetch, and it works. They are only here because the belong to the Insert statement.
$roleid = $row['id'];
$rolen = $row['nome'];
$sql = "INSERT INTO users ( nome, email, psw, nif, role_id, role_name)
VALUES ( :nome, :mail, :psw, :ni, :roleid, :rolen)";
$stmt = $db1->prepare($sql);
$stmt->bindValue('nome', $nome, PDO::PARAM_STR);
$stmt->bindValue('email', $mail, PDO::PARAM_STR);
$stmt->bindValue('psw', $psw, PDO::PARAM_STR);
$stmt->bindValue('nif', $ni, PDO::PARAM_INT);
$stmt->bindValue('role_id', $roleid, PDO::PARAM_INT);
$stmt->bindValue('role_name', $rolen, PDO::PARAM_STR);
$stmt->execute();
$error = $stmt->errorInfo();
print_r($error);
Executing this insert I get the following error:
Array ( [0] => HY093 [1] => [2] => )
This is the solution to the question, I wasn't using the same name in the bindValue() function
$name = $_POST['name'];
$email = $_POST['email'];
$psw = $_POST['psw'];
$nif = $_POST['nif'];
$roleid = $row['id'];
$rolen = $row['nome'];
$sql = "INSERT INTO users ( nome, email, psw, nif, role_id, role_name)
VALUES ( :name, :email, :psw, :nif, :roleid, :rolen)";
$stmt = $db1->prepare($sql);
$stmt->bindValue(':name', $name, PDO::PARAM_STR);
$stmt->bindValue(':email', $email, PDO::PARAM_STR);
$stmt->bindValue(':psw', $psw, PDO::PARAM_STR);
$stmt->bindValue(':nif', $nif, PDO::PARAM_INT);
$stmt->bindValue(':roleid', $roleid, PDO::PARAM_INT);
$stmt->bindValue(':rolen', $rolen, PDO::PARAM_STR);
$stmt->execute();
Since I can't/don't know how to auto_increment two columns in one table I trying to do this with transactions. This is what I trying
$pdo->beginTransaction();
try
{
$sql = "INSERT INTO users ( username, password, firstname, lastname, email, user_image, path)
VALUES (:username, :password, :firstname, :lastname, :email, :user_image, :path)";
$q = $pdo->prepare($sql);
$q->execute(array(
':username' => $username,
':password' => sha1($password),
':firstname' => $firstname,
':lastname' => $lastname,
':email' => $email,
':user_image' => $forDB,
':path' => $path,
));
$lastInsertID = $pdo->lastInsertId();
$sql = $pdo->prepare("INSERT INTO users (usertype)
VALUE (:user_id)");
$sql->execute(array(
':user_id' => $lastInsertID
));
$pdo->commit();
}
// any errors from the above database queries will be catched
catch (PDOException $e)
{
// roll back transaction
$pdo->rollback();
// log any errors to file
ExceptionErrorHandler($e);
exit;
}
So basically I want to insert in column usertype the ID of this record (user_id) both columns must be equal.
Now when I try with this .. it is save empty fields except for the usertype which is updated with lastInsertID
Change
$sql = $pdo->prepare("INSERT INTO users (usertype)
VALUE (:user_id)");
to this
$sql = $pdo->prepare("UPDATE users SET usertype=:user_id WHERE user_id=:user_id");
I have a function that inserts or updates a value in the database.
I am using a prepare and execute statement and I want to match my function's ELSE-clause to match the prepared statement.
In my ELSE-clause I have one more value (i.e. $id), so I'm not sure if I can assign it in the execute array.
function insert_value($item_name, $description, $supplier_code, $cost, $sell_price,$num_on_hand, $reorder_point, $back_order, $id=0)
{
$connection = db_connect();
if($id==0)
{
$sql = 'INSERT INTO inventory (itemName, description, supplierCode, cost, price, onHand, reorderPoint, backOrder)
VALUES(:itemName, :description, :supplierCode, :cost, :price, :onHand, :reorderPoint, :backOrder);';
}
else
{
//NEED TO CHANGE THIS PART
$sql = "UPDATE inventory SET itemName='$item_name', description='$description', supplierCode='$supplier_code',
cost='$cost', price='$sell_price', onHand='$num_on_hand', reorderPoint='$reorder_point', backOrder='$back_order'
WHERE id='$id'";
}
$prepare = $connection->prepare($sql);
$prepare->execute(array( // AND THIS PART
":itemName" => $item_name,
":description" => $description,
":supplierCode" => $supplier_code,
":cost" => $cost,
":price" => $sell_price,
":onHand" => $num_on_hand,
":reorderPoint" => $reorder_point,
":backOrder" => $back_order,
));
}
Something like:
function insert_value($item_name, $description, $supplier_code, $cost, $sell_price,$num_on_hand, $reorder_point, $back_order, $id=0){
$connection = db_connect();
$arr = array(":itemName" => $item_name,
":description" => $description,
":supplierCode" => $supplier_code,
":cost" => $cost,
":price" => $sell_price,
":onHand" => $num_on_hand,
":reorderPoint" => $reorder_point,
":backOrder" => $back_order);
if($id==0){
$sql = 'INSERT INTO inventory (itemName, description, supplierCode, cost, price, onHand, reorderPoint, backOrder)
VALUES(:itemName, :description, :supplierCode, :cost, :price, :onHand, :reorderPoint, :backOrder)';
}else{
$sql = "UPDATE inventory SET itemName=:itemName, description=:description, supplierCode=:supplierCode,
cost=:cost, price=:price, onHand=:onHand, reorderPoint=:reorderPoint, backOrder=:backOrder
WHERE id=:id";
$arr[":id"] = $id;
}
$prepare = $connection->prepare($sql);
$prepare->execute($arr);
}
Also you might want to check (if you don't do this already) to see if $connection is valid otherwise you might get errors once it gets to prepare/execute.
I think this is something related to PDO.
this is my patientinfo table
patientid | name | age | email | address
and this is my remarks tables
patientid | remarksid | date | description
I'd like to INSERT data to the patientinfo and to the remarks table where patientid of both tables will be synchronized. The problem is I dont know how to query this. This is what I do but it gives me an error.
$query = "INSERT INTO patientinfo (name, age, email, address)
VALUES (:name, :age, :email, :address);";
$query_params = array(
':name' => $_POST['name'],
':age' => $_POST['age'],
':email' => $_POST['email'],
':address' => $_POST['address'],
);
$query = "INSERT INTO remarks (patient_id, description) VALUES (:patient_id, :remarks) WHERE remarks.patient_id = patientinfo.patient_id;";
$query_params = array(':remarks' => $_POST['remarks']);
try{
$stmt = $dbname->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex){
$response["success"] = 0;
$response["message"] = $ex ;
die(json_encode($response));
}
i made patientid in the patientinfo AUTOINCREMENT.
PLEASE! THANK YOU SO MUCH FOR YOUR HELP!
$query = "INSERT INTO patientinfo (name, age, email, address)
VALUES (:name, :age, :email, :address);";
$query_params = array(
':name' => $_POST['name'],
':age' => $_POST['age'],
':email' => $_POST['email'],
':address' => $_POST['address'],
);
try{
$stmt = $dbname->prepare($query);
$stmt->execute($query_params);
$patient_id = $dbname->lastInsertId();
$query = "INSERT INTO remarks (patientid, description) VALUES (:patient_id, :remarks)";
$query_params = array(':remarks' => $_POST['remarks'],':patient_id'=>$patient_id);
$q = $dbname->prepare($query);
$q->execute($query_params);
}catch(PDOException $ex){
$response["success"] = 0;
$response["message"] = $ex ;
die(json_encode($response));
}
You should write something like that. Check column names please(patientid or patient_id ? )