首页 > 解决方案 > 将数据放入php和curl中的更新表中

问题描述

我是一名初级软件开发人员。我想了解网络服务是如何工作的?我有一个数据库,我有一个表,其中逐行包含我的数据。现在我想通过 CURL 更新我插入的表。但我无法将当前值放入更新表中。

<?php

     require_once('index.php');

     require("index.php");


     if($_POST)
     {
        $id = ($_POST['id']);
        $first_name  = ($_POST['first_name']);
        $last_name  = ($_POST['last_name']);

        $query=$first_name.'|'.$last_name;
        $params=array(
           'action' => "UPDATESINGLE",
           'query' => $query
            );
           $postData='';
           foreach($params as $k => $v){
          $postData .= $k . '='.$v.'&';
           }
rtrim($postData, '&');
if(is_array($postData)){
  $count=count($postData);
}else{
  $count=0;
}

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://localhost/webservice/service.php");
curl_setopt($ch, CURLOPT_POST, count($params));
curl_setopt($ch, CURLOPT_POSTFIELDS,$postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close ($ch);
print_r($response);
}

$query = $myPDO->query("SELECT * FROM users",PDO::FETCH_ASSOC);


 echo '<table class="table" border="2">
<tr>
    <th scope="col">Row Id</th>
    <th scope="col">First Name</th>
    <th scope="col">Last Name</th>
    <th scope="col">Update</th>
    <th scope="col">Delete</th>
  </tr>';

foreach ($query as $row)
{
    echo '<tr>
              <td>'.$row["user_id"].'</td>
              <td>'.$row["first_name"].'</td> 
              <td>'.$row["last_name"].'</td> 
              <td><a href="?do=update&id='.$row["user_id"].'">Update</a>
              </td> 
              <td><form action="" method="post">
                    <input type="submit" name="" value="Delete">
                </form>
              </td> 

          </tr>';
        }
    echo '</table>';

    if(isset($_GET['do']))
     {

        $data=explode('|',$query);
        $username=$data[0];
        $usersurname=$data[1];

    $event=$_GET['do'];
    if($event == "update")
    {
         echo '<table class="table" border="2">
      <form action="" method="post">
        <input type="text" name="first_name" value="'.$username.'">
        <input type="text" name="last_name"  value="'.$usersurname.'">
        <input type="submit" name="" value="Update">
      </form>
       ';
     echo '</table>';
   }


   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL,"http://localhost/webservice/service.php");
   curl_setopt($ch, CURLOPT_POST, count($params));
   curl_setopt($ch, CURLOPT_POSTFIELDS,$postData);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $response = curl_exec($ch);
   curl_close ($ch);
   print_r($response);
   }
  ?>

我的预期结果是在更新之前在表中查看我当前值的当前值,但我无法接受它们。

标签: phpcurl

解决方案


推荐阅读