0

Hi guys my php update query doesnt return me any value. It should return me success or failed but its not can you guys fix this?

disregard securities here I just use this query for my android app.

Here is my code.

<?php
include_once("connection.php");

if(isset($_POST['txtCar_No']) &&  isset($_POST['txtCarModel']) && 
    isset($_POST['txtCarType']) && isset($_POST['txtCapacity']) && 
    isset($_POST['image']) && isset($_POST['txtFuelType']) && 
    isset($_POST['txtPlateNumber']) &&  isset($_POST['txtcarPrice']))
{
    $now = DateTime::createFromFormat('U.u', microtime(true));
    $id = $now->format('YmdHis');

    $upload_folder = "upload";
    $path = "$upload_folder/$id.jpeg";
    $fullpath = "http://carkila.esy.es/$path";

    $image = $_POST['image'];
    $Car_No = $_POST['txtCar_No'];
    $Car_Model = $_POST['txtCarModel'];
    $Car_Type = $_POST['txtCarType'];
    $Capacity = $_POST['txtCapacity'];
    $Fuel_Type = $_POST['txtFuelType'];
    $PlateNumber = $_POST['txtPlateNumber'];
    $carPrice = $_POST['carPrice'];

    $query = "UPDATE tbl_cars SET Car_Model='$Car_Model', Car_Type='$Car_Type', Capacity='$Capacity', fuelType='$Fuel_Type' ,carPlatenuNumber='$PlateNumber', image='$fullpath' , carPrice = '$carPrice' WHERE Car_No=$Car_No";

    $result = mysqli_query($conn,$query);

    echo $Car_No;

    if($result > 0){
        echo "success";   
        exit();
    } else {
        echo "failed";
        exit();
    }
}
?>
Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
Jengjeng
  • 27
  • 6
  • Please check [this](http://stackoverflow.com/questions/6131304/how-to-determine-if-a-mysql-update-query-succeeded-when-the-data-passed-in-the-q) Question. Since its related and handled before – Taacoo Sep 15 '16 at 07:57
  • you can use mysqli_error function and check error in your query. it shows any error in query – bhawani Sep 15 '16 at 07:59
  • use mysqli_num_rows($result) – phpdroid Sep 15 '16 at 08:01

2 Answers2

0

You have to use mysqli_affected_rows($conn) to get rows affected by this update query.

E.g.:

$result = mysqli_query($conn,$query);
$count = mysqli_affected_rows($conn);



if($result == TRUE && $count > 0){
    echo "success";   
    exit();
} else {   
    print_r (mysqli_error($conn));
    echo "failed";
    exit();
}
rbr94
  • 2,227
  • 3
  • 23
  • 39
0

What is the value in $return after $result = mysqli_query($conn,$query);?

For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries, mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE. Returns FALSE on failure.

So the value of $result after your UPDATE-query can only be true or false, nothing else.

Your echo..if... can be simplified to one line:

echo ($result?"success":"failed");

Hope this helps.