0

for study purpose i wrote this PHP script in order to save data into mysql DB,

my final scope is make a post request using Alamofire via an Xcode app i'm developing.

but for trial i'm testing the post request using 'Postman app'

here my php script:

<?php

$host = "127.0.0.1";
$user = "root";
$password = "";
$database = "login_db";

$staff = $_POST['staff_ID'];
$password = $_POST['password'];
$email = $_POST['email'];
$cpt = $_POST['isCPT'];
$name = $_POST['Name'];



$connessione = new mysqli($host,$user,$password,$database);

if ($connessione === false) {

    die("errore connessione db " .$connessione->connection_error);

}

$query = "INSERT INTO users (staff_ID , password, email,  isCPT,  Name ) VALUES(?, ?, ?, ?, ?)";
 
$stmt = $connessione->prepare($query);  //Prepare
$stmt->bind_param("sssss", $staff,$password,$email,$cpt,$name);  //Bind
$stmt->execute();//Execute
if($stmt->affected_rows === 0) exit('Nothing was inserted.');  //Check to see if it worked
$stmt->close();
$connessione -> close();

?>

and a screenshot of my post request with Postman:

image

what is wrong? looks like php not receiving the post value, so each variable are null.

i tried to use isset, but not solving anything...like this for each value, but i end up in error

 if (isset($_POST['staff_ID'])){
      echo "ok";
    }else {
      echo "error";
    }

error:

<br />
<b>Warning</b>:  Undefined array key "staff_ID" in <b>E:\XAMPP\htdocs\test\nxSignUp.php</b> on line <b>8</b><br />
<br />
<b>Warning</b>:  Undefined array key "password" in <b>E:\XAMPP\htdocs\test\nxSignUp.php</b> on line <b>9</b><br />
<br />
<b>Warning</b>:  Undefined array key "email" in <b>E:\XAMPP\htdocs\test\nxSignUp.php</b> on line <b>10</b><br />
<br />
<b>Warning</b>:  Undefined array key "isCPT" in <b>E:\XAMPP\htdocs\test\nxSignUp.php</b> on line <b>11</b><br />
<br />
<b>Warning</b>:  Undefined array key "Name" in <b>E:\XAMPP\htdocs\test\nxSignUp.php</b> on line <b>12</b><br />
<br />
<b>Warning</b>:  Undefined variable $mysqli in <b>E:\XAMPP\htdocs\test\nxSignUp.php</b> on line <b>35</b><br />
<br />
<b>Fatal error</b>:  Uncaught Error: Call to a member function prepare() on null in E:\XAMPP\htdocs\test\nxSignUp.php:35
Stack trace:
#0 {main}
  thrown in <b>E:\XAMPP\htdocs\test\nxSignUp.php</b> on line <b>35</b><br />
Damiano Miazzi
  • 1,514
  • 1
  • 16
  • 38
  • You are sending those parameters via the URL, not via the request body. – Ivar May 30 '21 at 11:01
  • 1
    you don't have $mysqli you have $connessione – nbk May 30 '21 at 11:06
  • thanks, i correct the $connessione mistake, but i still get many error says:Warning: Undefined array key "staff_ID" in E:\XAMPP\htdocs\test\nxSignUp.php on line 8
    – Damiano Miazzi May 30 '21 at 11:08
  • look like no data has been pass with the post request – Damiano Miazzi May 30 '21 at 11:09
  • 2
    If you are only starting to learn PHP then you should learn PDO instead of mysqli. PDO is much easier and more suitable for beginners. Start here https://phpdelusions.net/pdo & https://websitebeaver.com/php-pdo-prepared-statements-to-prevent-sql-injection – Dharman May 30 '21 at 11:11

0 Answers0