In yii how to get success message after executing sql - php

i need to know how to get success message after successful execution of sql or failure message of wrong execution.my example is below
`
public function actionSql()
{
$table_no='1';
$employee='1';
$status='1';
$connection=Yii::app()->db;
$sql="INSERT INTO orders_transaction (table_no,employee,status) VALUES(:table_no,:employee,:status)";
$command=$connection->createCommand($sql);
$command->bindParam(":table_no",$table_no,PDO::PARAM_STR);
$command->bindParam(":employee",$employee,PDO::PARAM_STR);
$command->bindParam(":status",$status,PDO::PARAM_STR);
$command->execute();} `
after executing i need to know is the row successfully inserted or not.
i used below one but no use its only echoing successfully not failure
if($command->execute())
{
echo "Successful";
}
else {
echo "ERROR";
}
so i tried this one its giving permission denied error for localhost with password" "
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
}
else {
echo "ERROR";
}
i hope you under stand my problem.please give any suggestion or answer.

Execute()
returns the number of affected rows(for INSERT, DELETE, UPDATE etc).
$num = $command->execute();
here $num will contain the affected number of rows.

Related

PHP Ticket System

I'm making a VERY basic ticket system. Where basically if an SQL query or something doesn't run properly. It will insert a few bits of data into a MySQL table so the admin can run through and see what has happened and try to fix it.
<?php
$stamp = gettimestamp();
$error = $db->error;
$page = basename($_SERVER['PHP_SELF']);
echo "Error Updating! Please contact your IT Admin Team!";
echo "Stamp: ".$stamp;
echo "Error: ".$error;
echo "Page: ".$page;
echo "Email: ".$email;
$ticketSQL="INSERT INTO `db744544270`.`supportTickets` (`timestamp`, `problem`, `page`, `user`) VALUES ('$stamp', '$error', '$page', '$email')";
if ($db->query($ticketSQL) === TRUE) {
echo '<script>alert("Record Upated!");</script>';
header( "refresh:5;url=index.php" );
//var_dump($sql);
//echo "<script>location.href = 'index.php';</script>";
} else {
echo "Error AGAIN!";
}
?>
Above is the code I am running and basically I have put this into a file and just include that file on every page where I need it. For example inside the error handling of an if statement. If query TRUE-> YAY, else query FALSE->include ticket page.
Like I said...very basic..
Whenever I run the page and at the moment I am getting an error from another query but that's a question for a different time :) this is the current error i get...
Error updating record: Unknown column '1' in 'where clause'
Fatal error: Call to undefined function gettimestamp() in /homepages/38/d735513801/htdocs/includes/supportTicket.php on line 2
I have no idea why it is saying it's undefined. I have tried changing the type and everything. It just will not insert into the table, I feel like I'm being stupid but I can't see why it's not working...

prepared statements don't work

if(isset($_SESSION['id'])) {
$message=$_POST['message'];
$cid=$_POST['cid'];
$user_id=$_SESSION['id'];
$stmt=$conn->prepare("update comments set message=? where id=? and user_id=?");
$stmt->bind_param("sss",$message,$cid,$user_id);
if(!$stmt->execute()){
echo "error";
} else {
echo "success";
}
I always get success but when I go to the database I find out that nothing is changed.
You are trying to do an UPDATE with a WHERE clause. If it does not actually update anything, it will still be considered a success. The reason !$stmt->execute() would happen, is if there is a sql error, and it bails.
So what you should be doing, is also check if num rows affected.
if ( ! $stmt->affected_rows ) { echo 'nothing changed!'; }

Record not getting updated in PHP/MySQL

I am trying to change some values in a table but for some reason the values are not getting updated. Here is the php :
$title=mysqli_escape_string($conn,$_POST['title']);
echo $title."<br>"; //Test
$note=mysqli_escape_string($conn,$_POST['jnote']);
echo $note."<br>"; //Test
$query="UPDATE articles SET return_j='1' AND j_note='$note' WHERE article_name='$title'";
if ($conn->query($query) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
Problem is I am getting no error. 'Record updated successfully' is what I get. So the query is being executed successfully but the table is not getting updated. I don't understand why.
When I am updating other columns of the table (in some other scripts), I am having no problem.
Swap out the AND for a comma.
UPDATE articles SET return_j='1', j_note='$note' WHERE article_name='$title'
and I don't think you need
===TRUE
just leave it out of the condition statement.

Update query using php mysqli_query (procedural style)

I am using this code to update rows and the value of $result is coming true. The if condition is executing. But the data in the table isnt updated. So, No errors yet the updation isnt happening.
What is the problem? Any help appriecated. :) Happy Diwali
for($i=0;$i<$len-1;$i++){
$sqlquery="UPDATE items_data SET item_price='".$price[$i]."' WHERE item_name='".$items[$i]."'";
$result=mysqli_query($connection,$sqlquery);
if($result){
echo "Working";
}
else
{
echo mysqli_error($connection);
}
}

PHP MySqli Delete error message

This code does delete a record from the table correctly but the second time run on the same table it still reports "Record deleted successfully". Why is that?
// sql to delet a record
$sql = "DELETE FROM setidata WHERE SETIData_ID=834";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
echo "<br> Finished";
?>
Because mysqli_query returns true not matter if the record exists or not.
use mysqli_affected_rows(); to see how many rows have been deleted.
if(mysqli_affected_rows($conn)==0){
print "Nothing done";
}
Because mysqli_query($conn, $sql) is returning true since since there were no errors triggered by the query or in the function mysqli_query(). If you want to do something redundant, mysqli_query() will not stop you.
As per your code If query is succesful then it will echo as opted. You can run this query in phpmyadmin->SQL. First time it will show deleted rows 1. Next time it will show 0.

Categories