php - Adding Jwt Booking Api file

622

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in D:\xampp\htdocs\wordpress\wp-content\themes\twentyseventeen\booking.php on line 14.

It is showing the above error?

line 14 is:if ($qur) {

include_once('config.php');
if ($_SERVER['REQUEST_METHOD'] == "POST"){
$username = isset($_POST['username']) ? mysqli_real_escape_string($conn, 
$_POST['username']) : "";
$service = isset($_POST['service']) ? mysqli_real_escape_string($conn, 
$_POST['service']) : "";
$employee = isset($_POST['employee']) ? mysqli_real_escape_string($conn, 
$_POST['employee']) : "";
$date = isset($_POST['date']) ? mysqli_real_escape_string($conn, 
$_POST['date']) : "";
$booking_time = isset($_POST['booking_time']) ? 
mysqli_real_escape_string($conn, $_POST['booking_time']) : "";
$duration = isset($_POST['duration']) ? mysqli_real_escape_string($conn, $_POST['duration']) : "";
$sql = "INSERT INTO `booking_api` (`id`,`username`, `service`, `employee`, `date`,`booking_time`, `duration`) VALUES (NULL,'$username', '$service','$employee', '$date', '$booking_time','$duration');";
echo $sql;
$qur = mysqli_query($conn, $sql);
 if ($qur) {
    $fetch = mysqli_fetch_array($qur);
    if (strlen($fetch["id"]) > 0) {

        // base64 encodes the header json
        $encoded_header = base64_encode('{"alg": "HS256","typ": "JWT"}');

        // base64 encodes the payload json
        $encoded_payload = base64_encode('{"first_name":'.$fetch["first_name"].' "last_name": '.$fetch["last_name"].' "username": '.$fetch["username"]
                .' "email":'.$fetch["email"].' "password":'.$fetch["password"].' "phone_number":'.$fetch["phone_number"].'}');

        // base64 strings are concatenated to one that looks like this
        $header_payload = $encoded_header . '.' . $encoded_payload;

        //Setting the secret key
        $secret_key = 'Octaviasecretkey';

        // Creating the signature, a hash with the s256 algorithm and the secret key. The signature is also base64 encoded.
        $signature = base64_encode(hash_hmac('sha256', $header_payload, $secret_key, true));

        // Creating the JWT token by concatenating the signature with the header and payload, that looks like this:
        $jwt_token = $header_payload . '.' . $signature;

        //listing the resulted  JWT
        //echo $jwt_token;
      $json = array("status" => 1, "msg" => "Congratulations, Booking 
      Confirmed!");
     } else {
    $json = array("status" => 0, "msg" => "Error! Booking");
     }
     } else {
    $json = array("status" => 0, "msg" => "Request method not accepted");
     }
     }
   /* Output header */
   header('Content-type: application/json');
   echo json_encode($json);

Booking.php sql file

 CREATE TABLE `booking_api` (
 `id` int(150) NOT NULL,
`username` varchar(300) NOT NULL,
`service` varchar(200) NOT NULL,
`employee` varchar(200) NOT NULL,
`date` varchar(150) NOT NULL,
`booking_time` varchar(150) NOT NULL,
`duration` varchar(150) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `booking_api`
--

  INSERT INTO `booking_api` (`id`, `username`, `service`, `employee`, 
  `date`, `booking_time`, `duration`) VALUES
  (1, '[email protected]', 'Scalp Massage', 'Massage Employee 1', '2/02/2018', 
  '11:04 Am', '50 mins'),
  (2, '[email protected]', 'Personalized Massage', 'Massage Specialist', 
  '2/02/18', '11:27 AM', '50 mins'),
  (3, '[email protected]', 'Deep Tissue Massage', 'Massage Employee 5', 
  '2/02/2018', '6:16 PM', '50 mins'),
  (4, '[email protected]', 'Swedish Massage', 'Massage Employee 1', 
  '2/02/2018', '6:25 PM', '45 mins'),
  (5, '[email protected]', 'Swedish Massage', 'Massage Employee 7', 
  '5/02/2018', '10:46 AM', '60 mins'),
  (6, '[email protected]', 'Scalp Massage', 'Massage Employee 4', '6/02/2018', 
  '1:50 PM', '45 mins'),
  (7, '[email protected]', 'Head Massage', 'Massage Employee ', '26/02/2018', 
  '1:50 PM', '45 mins'),
  (8, '[email protected]', 'Head Massage', 'Massage Employee ', '26/02/2018', 
 '1:50 PM', '45 mins'),
  (9, '[email protected]', 'Head Massage', 'Massage Employee ', '26/02/2018', 
 '1:50 PM', '45 mins'),
  (10, '[email protected]', 'Head Massage', 'Massage Employee ', '26/02/2018', 
 '1:50 PM', '45 mins'),
 (11, '[email protected]', 'Head Massage', 'Massage Employee ', '26/02/2018', 
 '1:50 PM', '45 mins'),
 (12, '[email protected]', 'Head Massage', 'Massage Employee ', '26/02/2018', 
 '1:50 PM', '45 mins'),
 (13, '[email protected]', 'Personalised Massage', 'Massage Employee ', 
 '28/02/2018', '11:24 PM', '45 mins'),
 (14, '', '', '', '', '', ''),
 (15, '', '', '', '', '', ''),
 (16, '[email protected]', 'Personalised Massage', 'Massage Employee ', 
 '28/02/2018', '11:24 PM', '45 mins');
154

Answer

Solution:

Sounds like you want it to generate a new ID for each record, this is best done using an auto_increment key. This means you table definition is...

CREATE TABLE `booking_api` (
`id` int(150) NOT NULL AUTO_INCREMENT,
`username` varchar(300) NOT NULL,
`service` varchar(200) NOT NULL,
`employee` varchar(200) NOT NULL,
`date` varchar(150) NOT NULL,
`booking_time` varchar(150) NOT NULL,
`duration` varchar(150),
 PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Then you insert would become...

$sql = "INSERT INTO `booking_api` (`username`, `service`, `employee`, `date`,`booking_time`, `duration`) 
    VALUES ('$username', '$service','$employee', '$date', '$booking_time','$duration');";
echo $sql;
$qur = mysqli_query($conn, $sql);
 if ($qur) {
    // Fetch id
    $id = mysqli_insert_id($conn);

mysqli_insert_id() fetches the new id generated for the last INSERT on the connection.

People are also looking for solutions to the problem: php - Carbon parse date format

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.