How can I check data has been inserted successfully? - php

I have two insert statements. The second one will be executed only after successful execution of first one. What I would like to do is:
$sqlone="Insert into .....";
$sqltwo="Insert into.....";
If (mysql_query($sqlone))
{
If (mysql_query($sqltwo))
{
Show message Data inserted in both tables.
}
}

Try this
$query1 = '...';
$query2 = '...';
$query3 = '...';
if(mysql_query($query1)) {
if(mysql_query($query2)) {
if(mysql_query($query3)) {
echo "success";
}
else { echo "error"; }
}
else { echo "error"; }
}
else { echo "error"; }

Sounds like you're looking for transactions.
A bit of googling gave me some info on database transactions in PHP - hope it helps.

That syntax looks like it works, as...
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
http://au2.php.net/mysql_query

From the documentation:
For other type of SQL statements,
INSERT, UPDATE, DELETE, DROP, etc,
mysql_query() returns TRUE on success
or FALSE on error.
So as far as I can see there is no problem with what you already have.

You can also create the DB transaction as well in which commit of one insert will execute the second insert statement..

Related

CodeIgniter - Ensuring activerecord where statement has a criteria

Is the following code safe for deleting data?
public function remove($sc1_id = false)
{
if(!$sc1_id) redirect('backend/sections/index');
$sub_section_ids = $this->flatten($this->db->select('sc2_id')->from('sub_sections')->join('sections', 'sc2_sc1_id = sc1_id', 'INNER')->where('sc1_id', $sc1_id)->get()->result_array());
if($sub_section_ids)
{
$this->db->where_in('s2l_sc2_id', $sub_section_ids);
$this->db->delete('sub_section_prod_link');
}
$this->db->where('sc1_id', $sc1_id);
$this->db->delete('sections');
$this->db->where('sc2_sc1_id', $sc1_id);
$this->db->delete('sub_sections');
redirect('backend/be_sections/index');
}
I want to make sure that the where statements are being fullfilled. In testing, data has been wiped, so perhaps $sc1_id was truey, yet still not making a condition on the where statement, or perhaps I need to flush_cache() or reset_query() as well?
One thing I would add, it's a transaction statement. You're using more than one table for the delete process, if one of then fails you can't go back. So:
$this->db->trans_begin();
//your statements here
//if something goes wrong, just undo
if($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
}
//if everything is ok, proceed
else
{
$this->db->trans_commit();
redirect('to_somewhere');
}

PHP ADOdb, How to test if query is "insert, update, delete, or drop"?

MySQL/i's $db->query('some query') will return a result set for successful SELECT, SHOW, DESCRIBE or EXPLAIN, or return true for successful INSERT, UPDATE, DELETE, DROP, etc.
As such, we can easily identify the "type" of query:
$result = $db->query('some query that we want to identify');
if($result === false){
echo 'Error'; exit;
}
if($result === true){
// query is a successful INSERT, UPDATE, DELETE, DROP, etc.
}else{ // else type of $result will be result set
// query is a successful SELECT, SHOW, DESCRIBE, EXPLAIN
}
How can we do the above using PHP ADOdb?
I'm currently using:
$result = $db->Execute('some query');
if($result === false){
echo 'Error'; exit;
}
if(get_class($result) === 'ADORecordSet_empty'){
// query is a successful INSERT, UPDATE, DELETE, DROP, etc?
}else{
// query is a successful SELECT, SHOW, DESCRIBE, EXPLAIN ?
}
which seems to work, but it definitely feels fragile and "working against the API". Is there a better way to do it?
Are there built-in ADOdb functions to do it?
ADOdb returns an empty recordset object when the underlying API called in the DB-specific driver to execute the query returns true.
By default this means an ADORecordSet_empty object, but this may be different if custom recordset classes are in use (setting rsPrefix).
In other words, I don't think there is any other way of achieving what you want; you can make the code more portable by comparing against $db->rsPrefix . 'empty' instead of hardcoding 'ADORecordSet_empty'.
You can use affected_rows() method
if ($DB->affected_rows() !== 0) {
//true
}
http://devcodepro.com/view/70/1/ADOdb-Check-if-MySQL-query-executed-successfully

Codeigniter Active Record Insert Function

So i have been pulling my hair out over this for the past two days. I have identified the problem down to this so far:
I am inserting some simple data into the database using Active Record:
if($this->db->insert('table', $data)){
return true;
}
else{
return false;
}
The problem is that it was always returning true whether the data got inserted or not. How i figured this out was that after several failed attempts when the data finally got inserted, the AUTO_INCREMENT ID was at 17, meaning that the insert query was running but failing to insert, hence always returning true. I want to know a reliable method of knowing whether data got inserted or not. Tried:
$this->db->affected_rows() > 0;
as well. But same issue prevails. It returns true.
If you have auto incremental id in your table then check $this->db->insert_id()
if greater the zero or us number then we can say data inserted or
again fire a sql query with id we get and if record exist then data is inserted
but i think that is not necessary just check insert_id
You need to insert your data first and get the result after. You will need to do something like this:
$this->db->insert('my_table', $my_data);
$is_inserted = $this->db->affected_rows() > 0;
if($is_inserted) {
echo 'Yay! Its works!';
} else {
echo 'Something went wrong. No insert ):'
}
You must perform your insert query before get the result. So you will need to run your insert and then get the affected rows. Codeigniter provides the class $this->db that help us to do this very easily. You can even get the inserted id running $this->db->insert_id() instead $this->db->affected_rows() to get the brand new inserted id.
You may find these links useful:
Query Helper Functions -
https://ellislab.com/codeigniter/user-guide/database/helpers.html
Active Record Class
https://ellislab.com/codeigniter/user-guide/database/active_record.html
Good Luck!
i always do this for check data is inserted or not
$this->db->insert('table', $data);
$id = $this->db->insert_id();
if($id)
{
echo "data inserted";
}
else
{
echo "data not inserted";
}
you can also do this also worked for me
$query = $this->db->insert('table', $data);
if($query)
{
echo "data inserted";
}
else
{
echo "data not inserted";
}
This is what I do. It works for me.
$this->db->set($data)->insert($table_name);
if ($this->db->affected_rows()) {
return $this->db->insert_id(); // or return true; in your case
}
return false;

Best way to test an insert and get success/error without committing?

I'm writing a PHP script that does a batch import of data into a table, and I want to have a "test mode" where the system processes the INSERT statements and notes if there are any errors on any of the statements, but does not commit the data to the table.
What is the best way to execute an INSERT to get either success or mysql_error() without having the actual operation be permanent?
For example:
$r=mysql_query($cmd);
if ($r) {
$rownote.=" ADDED";
$addcount++;
} else {
$merror=mysql_error();
$rownote.=" ERROR: (<FONT SIZE=\"-1\">$merror</FONT>)";
$failcount++;
}
I want to be able to execute the above in a "test mode" and generate the rownotes, but not actually execute the statements. Should I use some sort of rollback? Or is there a way I can specify an insert statement that would not do the actual insert but return whether or not the INSERT would be successful or the error?
Here is my solution that works. (I am aware that some people don't like using mysql_query and it is depreciated, but this is a legacy app I'm having to modify).
if ($ftestmode) {
mysql_query("SET AUTOCOMMIT=0");
mysql_query("START TRANSACTION");
}
$r=mysql_query($cmd);
if ($r) {
$rownote.=" ADDED";
$addcount++;
} else {
$merror=mysql_error();
$rownote.=" ERROR: (<FONT SIZE=\"-1\">$merror</FONT>)";
$failcount++;
}
$errors[]=$rownote;
if ($ftestmode) {
mysql_query("ROLLBACK");
}

php mysql delete from database

Hey hello everyone,i have a slight problem with my small delete function in php,below is my code
function delete()
{
$q = "DELETE FROM example WHERE **author='frank'";**
$r = mysql_query($q) or die (mysql_error());
if($r)
{
echo 'done';
}
else
{
echo 'not done';
}
}
Now i don't have any author with that name Frank so that means it is not deleting anything
from the database but still shows that done msg
I am not sure why????can anyone please help me
That's because there was no error, delete did execute, it just didn't do anything. You want:
if(mysql_affected_rows() > 0) {
echo "done";
}
There is no error in your query. It will complete successfully. If you read the documentation you will see:
"For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error."
As there is no error in your query it will return TRUE even though nothing has actually been deleted. Deleting nothing is not considered an error.
A delete query will always succeed, even if no rows are actually deleted. If you want to determine how many rows were affected by an insert, update or delete operation, use mysql_affected_rows()

Categories