0

I'm trying to write a PHP code that inserts and updates values and rows in a mySQL database with phpmyadmin, the update on an existing row works fine, but insert does not work. To give you some context, deviceID is the primary key, if deviceId does not already exist in the database, an INSERT should be done.

I think the problem lies in the comparison of whether the if-statement returns the empty set or a result. Help would be appreciated, thanks in advance!

Warning:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given in C:\wamp\www\android_connect\update.php on line 43

line 43 is: $arr = mysqli_fetch_array($query);

Here is the php code:

if ($_SERVER ["REQUEST_METHOD"]=="POST"){
require'connectiontest.php';
createStudent();
}

$response = array();

function createstudent()
{

// check for required fields
if (isset($_POST['deviceId']) && isset($_POST['buildingId']) && 
isset($_POST['levelId']) && isset($_POST['floorplanId']) && 
isset($_POST['latitude']) && isset($_POST['longitude'])
&& isset($_POST['x']) && isset($_POST['y']) && isset($_POST['i']) && i 
sset($_POST['j']) && isset($_POST['heading']) 
&& isset($_POST['probability']) && isset($_POST['roundtrip'])) {
// extract data from POST into variables
global $connect;

<?php


$deviceId = $_POST['deviceId'];
$buildingId = $_POST['buildingId'];
$levelId = $_POST['levelId'];
$floorplanId = $_POST['floorplanId'];
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$x = $_POST['x'];
$y = $_POST['y'];
$i = $_POST['i'];
$j = $_POST['j'];  
$heading = $_POST['heading'];
$probability = $_POST['probability'];
$roundtrip = $_POST['roundtrip'];
$query = "SELECT * FROM devicelocations WHERE deviceId = '$deviceId';";
$arr = mysqli_fetch_array($query);

if (sizeof($arr) == 1) {
    error_log("Database is empty, doing an INSERT.", 0);
$query = "INSERT INTO devicelocations (deviceId, buildingId, levelId, floorplanId, latitude, longitude, x, y, i, j, heading, probability, roundtrip) VALUES ('$deviceId', '$buildingId', '$levelId', '$floorplanId', '$latitude', '$longitude', '$x', '$y', '$i', '$j', '$heading', '$probability', '$roundtrip');";}

else {
    error_log("Database already has that deviceId, doing an UPDATE.", 0);
    $query = "UPDATE devicelocations SET buildingId = '$buildingId', levelId = '$levelId', floorplanId = '$floorplanId', latitude = '$latitude', longitude = '$longitude', x = '$x', y = '$y', i = '$i', j = '$j', heading = '$heading', probability = '$probability', roundtrip = '$roundtrip' WHERE deviceId = '$deviceId';";}

mysqli_query($connect, $query) or die (mysqli_error($connect));
mysqli_close($connect);



// check if row inserted or not
if ($query) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "Entry successfully inserted or updated.";

    // echoing JSON response
    echo json_encode($response);
} else {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "Entry was not successfully inserted or updated.";

    // echoing JSON response
    echo json_encode($response);
}
}  else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}

}

?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Jed
  • 27
  • 4

3 Answers3

1

you didn't even qxecute the query before that connection with database must be made.

$query = "SELECT * FROM devicelocations WHERE deviceId = '$deviceId';";
$result=mysqli_query($con,$query);//executing query


$arr = mysqli_fetch_array($result);


$con=mysqli_connect(ip,"username","password",database)

is connection to database

jasinth premkumar
  • 1,430
  • 1
  • 12
  • 22
  • The connection to the databse works, I am able to update values in rows that I inserted with another code. Now that I executed the query as you said, i dont get any warnings, but it is still not possible to insert a new row. – Jed May 15 '18 at 16:34
  • I made an edit; "require'connectiontest.php';" is where the connection is done. By insert a new row i mean that the "INSERT INTO" statement does not work. – Jed May 15 '18 at 16:51
  • sixe arr return s size of result which will be greater than one so use `if(mysqli_num_rows==1)` – jasinth premkumar May 15 '18 at 16:52
0

Use

$count=mysqli_num_rows($your_query);  
if($count>0){    
   update query
}
else
{    
   insert query
}
Ginxxx
  • 1,602
  • 2
  • 25
  • 54
0

This solved the problem:

$result=mysqli_query($connect,$query);
$count=mysqli_num_rows($result);
if($count>0) {
update query } else{
insert query }
Jed
  • 27
  • 4