0

I'm trying to post some data to PHP through postman, I set up everything with all required fields and their values but for some reason when I try to post data, it says that field is empty even though I set the value in post man request.

  <?php 

   include_once 'creds.php';

   $con  =  mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
   if($con -> connect_error){
     echo "Failed connecting to database".mysqli_connect_errno();
   } else {
    echo "Success connecting\n";

  }


  
    //Register User
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
  
       if(!isset($username)){
          echo "Username field cannot be empty\n";
          return;
       }

       if(!isset($password)){
          echo "password field cannot be empty\n";
          return;
       }

       if(!isset($email)){
          echo "Email field cannot be empty\n";
          return;
       }

     
      $username = $_POST['username'];
      $password = $_POST['password'];
      $email = $_POST['email'];
       

        $stmt = $con->prepare("INSERT INTO `shop`(`userID`, `username`, `email`, `password`) VALUES (?,?,?,?)");
        $stmt-> bind_param("sss",$username,$password,$email);

        if($stmt -> execute()){
           echo "Succesfully Registered";
         } else {
           echo "Failure\n".mysqli_error($con);
         }
      } 

?>
  • This is screenshot of Postman request

enter image description here

Dharman
  • 30,962
  • 25
  • 85
  • 135
Taki
  • 3,290
  • 1
  • 16
  • 41
  • You want to post to phpMyAdmin? fyi, phpMyAdmin is a GUI for managing MySQL servers, databases, tables, users etc. (Pretty sure you can't POST to phpMyAdmin, but I could be wrong) – brombeer Jun 13 '22 at 05:48
  • Put the post fields in the "Body" tab of the request, not the "Params" tab (in Postman). For the type, select "form-data". – Rylee Jun 13 '22 at 06:33
  • It looks like you confused the langauge PHP with the tool phpMyAdmin. You can't post data to phpMyAdmin – Dharman Jun 13 '22 at 10:26
  • Basically i'm trying to post data to my online sql database , – Taki Jun 13 '22 at 13:55
  • To the one who closed my questions , well the link you provided does not solve my problem , why would you close the question in the first place to ask me to open a new one .. why this is happenning lot now , obviously before i post , i look into others topics on stack . it is just a waste of time to keep closing my questions and ask me to open a new case .. – Taki Jun 13 '22 at 14:16

1 Answers1

-2

You need to define the variable $username before using this.

Shift this 2 lines of codes

$username = $_POST['username'];
$password = $_POST['password'];
$password = $_POST['email'];

just before

   if(!isset($username)){

I hope, issue will be resolved

Naveed Ramzan
  • 3,565
  • 3
  • 25
  • 30