0
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
      $name = test_input($_POST["name"]);
      $email = test_input($_POST["email"]);
      $phone = test_input($_POST["phone"]);
      if($name) {
         $data = array(
                "name" => $name,
                "email" => $email,
                "phone" => $phone
          );
          $insert = $db->setKey($keyname, $data);
          if($insert == 1){
            echo "successfully inserted";
          } else{
            echo "Error";
          }
      } else{
    
        echo "Name is required";
      }
}

在这里,我试图将用户详细信息插入到 replit 数据库中。它已插入,但仅存储一个用户详细信息。例如,第一个我输入为 name=>test、email=>test@gmail.com、phone=>12121。此输入将插入数据库。但是第二次如果我输入其他用户详细信息,它将替换这个值。您能帮我将多个用户详细信息插入数据库吗?这里是 setKey() 函数。

 public function setKey($keyname, $data) {
    if ((isset($keyname)) && (!empty($keyname)) && (isset($data)) && (!empty($data))){

    // serialize the data so that it's in one key
    // if you do an array, it will make a key for each value
    $data = array($keyname => serialize($data));

    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data)
        )
    );
    $context  = stream_context_create($options);
    $result = file_get_contents($this->REPL_DB_URL(), false, $context);
    if ($result === FALSE) {
      return false;
    } else {
      return true;
    }
    } else {
      return "You must set a keyname and have data. Syntax: setKey($keyname, $data)";
    }
  }
4

0 回答 0