user4792429
user4792429

Reputation:

how i put data in url in webservice in php

I'm first time using webservice in php, i'm using REST for this. I search lot on google and blogs but not find how to put data in mysql that send in url in php? here's my simple code--

<?php

if ($_SERVER['REQUEST_METHOD'] == 'PUT') {

parse_str(file_get_contents("php://input"), $_PUT);

if ($id = $_PUT['id']) {


    $name = $_PUT['name'];

    $id = NULL;

    $email = $_PUT['email'];

    $address = $_PUT['address'];

    $conn = mysqli_connect('localhost', 'root', '', 'webservice');
    //var_dump($conn);
    $sql = "
                UPDATE webservice SET name='$name' , address='$address' , email='$email' WHERE id='$id'
           ";

    if (mysqli_connect_errno()) {


        echo json_encode(array(
            'status' => 'failure',
            'message' => 'Could Not connect to database',
        ));
    }

    $data = mysqli_query($conn, $sql);

    if ($data) {

        echo json_encode(array(
            'status' => 'successful',
        ));
    } else {

        echo json_encode(array(
            'status' => 'failure',
        ));
    }
  }
 }
?>

and here's my url that i send-- REST-PHP/put.php?id=1&name=webservice&[email protected]&address=address

as i'm newbie please ignore my faults...

and thanks in advance

Upvotes: 1

Views: 133

Answers (1)

Raja
Raja

Reputation: 861

Use $_GET instead of $_PUT as You are sending a GET request not a PUT.

Updated Code :

<?php

if ($_SERVER['REQUEST_METHOD'] == 'GET') {

//parse_str(file_get_contents("php://input"), $_GET);

if ($id = $_GET['id']) {


$name = $_GET['name'];

//$id = NULL; // This is wrong
$id = $_GET['id'];

$email = $_GET['email'];

$address = $_GET['address'];

$conn = mysqli_connect('localhost', 'root', '', 'webservice');
//var_dump($conn);
$sql = "
            UPDATE webservice SET name='$name' , address='$address' , email='$email' WHERE id='$id'
       ";

if (mysqli_connect_errno()) {


    echo json_encode(array(
        'status' => 'failure',
        'message' => 'Could Not connect to database',
    ));
}

$data = mysqli_query($conn, $sql) or die(mysqli_error($conn));

if ($data) {

    echo json_encode(array(
        'status' => 'successful',
    ));
} else {

    echo json_encode(array(
        'status' => 'failure',
    ));
  }
 }
}
?>

Upvotes: 1

Related Questions