0

I have written a code that uploads values to mysql server , i'm testing from Postman and it does send data and connects successfully but no data is pushed into my database , i appreciate any help ,

  • Php code

  <?php 

   include_once('db.php');

   // init db
   $conn = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
   if($conn -> connect_error){
      print("Failed to connect to server");
   } else {
      print("Connected\n");
      writeDataToServer();
   }

   function writeDataToServer(){

      global $conn;
      $TABLE = 'countryInfo'; 


      if($_SERVER['REQUEST_METHOD'] == "POST"){
 

      $countryName = $_POST['countryName'];
      $countryPhone = $_POST['countryPhone'];
      $countryCurrency = $_POST['countryCurrency'];
      
      if(!empty(trim($countryName)) && !empty(trim($countryPhone)) && !empty(trim($countryCurrency))){
   

         echo 'Values check passed';

         $sql  = "INSERT INTO `countryInfo` ('countryName','countryPhone','countryCurrency') VALUES (?,?,?)";
         $stmt = $conn -> prepare($sql);
         $stmt ->bind_param("sss",$countryName,$countryPhone,$countryCurrency);
         $stmt -> execute();

         echo 'Successfully Inserted';

         $stmt -> close();
         $conn -> close();
         


        } else {

         echo "Please check if all values are set";

        }


      } else {

         echo 'REQUEST METHOD IS NOT POST';
      }

 
   }

?>

Taki
  • 3,290
  • 1
  • 16
  • 41
  • Quotes are for strings, remove those from the columns, or change to backticks. Is error reporting enabled here? – user3783243 Feb 25 '23 at 00:48
  • If only `$_SERVER['REQUEST_METHOD'] == 'POST'` is allowed you should use `POST` not `REQUEST`. – user3783243 Feb 25 '23 at 00:49
  • The `writeDataToServer` is a pretty limited function also. I would have this procedural or pass values to the function so it is more useful. – user3783243 Feb 25 '23 at 00:51
  • You should debug the cause of that then.. maybe you are sending a GET and not a POST? If that were the case you would have no behavior because your `if($_SERVER['REQUEST_METHOD'] == 'POST'){` has no corresponding `else`. – user3783243 Feb 25 '23 at 00:53
  • @user3783243 Here is an image from post , and i have added some echos after passing each steps , POST CHECK , CHECK IF VALUES ARE EMPTY , link : https://ibb.co/cwvZj0T – Taki Feb 25 '23 at 00:59
  • from your link ibb.co/cwvZj0T, you are defining the method as post, but passing the values as query which will be considered as GET. You need to pass it from body tab in postman – Alaksandar Jesus Gene Feb 25 '23 at 01:09
  • @user3783243 Thank you for the help , now it does work with $_POST , the values are not empty now , but for some reason it does not push data into my database any reason , Thank you – Taki Feb 25 '23 at 01:18

0 Answers0