简体   繁体   中英

Am using php, json services while i execute below code i am getting a blank page, no data updated in database…correct me if wrong

PHP

I am a beginner to web services.This is my code using php, json and mysql in restful web services in order to update the user status.Even I am not able to update the user information to the database..plz help me out...!!

    <?php

    //content header-type

    //header('content-type: application/json');

    // connect to data-base

    $dbhost = 'localhost';
     $dbuser = 'root';
      $dbpass = '';
       $conn = mysql_connect($dbhost, $dbuser, $dbpass); 
       if(! $conn ) 
        {
           die('Could not connect: ' . mysql_error());
            }  

            //on submit action

            if(isset($_PUT['update']))
            {

    if($_SERVER['REQUEST_METHOD'] == "POST")
    {
        $id = isset($_SERVER['HTTP_ID']) ? 
        mysql_real_escape_string($_SERVER['HTTP_ID']) : "";

        $status = isset($_SERVER['HTTP_STATUS']) ? 
        mysql_real_escape_string($_SERVER['HTTP_STATUS']) : "";

        // validations of code

        if(!empty($id)){
            $qur = mysql_query("UPDATE  `miisky`.`register` SET  `status` =  '$status' WHERE  `register`.`ID` ='$id';");
            if($qur){
                $json = array("status" => 1, "msg" => "Status updated!!.");
            }else{
                $json = array("status" => 0, "msg" => "Error updating status");
            }
        }else{
            $json = array("status" => 0, "msg" => "User ID not define");
        }
    }else{
            $json = array("status" => 0, "msg" => "User ID not define");
        }
        @mysql_close($conn);

        /* Output header */
        header('Content-type: application/json');
        echo json_encode($json);
    }
    ?>

HTML

And hear goes my html validation part, to validate the user in order to receive the input from user and update the status of particular user.

<body>
  <form method = "POST" action = "update2.php">

    <h1>Please enter your user id to update your other details</h1>
    <label>ID</label>
    <input type = "text" name = "ID"><br/>
    <label>Status</label>
    <input type = "text" name = "status">
    <br/>
    <input type = "submit" name = "update">

 </form>
</body>

HTML forms only support GET and POST. Only method attribute allowed GET and POST request in html form. So you can't set PUT method type in method attribute. Change you method type in method attribute. As per the HTML standard, you can not.

 <form method = "PUT" action = "update2.php">

Change your method attribute eg GET or POST.

<form method = "GET" action = "update2.php">

Due to wrong mehtod attribute value your if condition is not coming true. You always got GET value from:-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM