Cannot insert data into the database

I have some issues with PDO. my app run smoothly in xamp but when i host it, i cannot insert data into the database

this is the jquery code

$(document).ready(function() {

          $('form.ajaxform').on('submit', function() {

        var that = $(this);
        url = that.attr('action');
        type = that.attr('method');
        data = {};

        that.find('[name]').each(function(index, value){
          var that = $(this);
              name = that.attr('name');
              value = that.val();
          data[name] = value;
        });

        $.ajax({
          url: url,
          type: type,
          data: data,
          success: function(response){
            $('#feedback').html(response);
          }
        });

this is the php code


require_once 'functions.php';
if(!loggedIn()) {
  header('location: ./');
}
if(isset($_POST['submit'])) {
	$agent = $_POST['agent'];
	$agentphone = $_POST['agentphone'];
	$passport = $_POST['passport'];
	$surname = $_POST['surname'];
	$gnames = $_POST['gnames'];
	$nationality = $_POST['nationality'];
	$dob = $_POST['dob'];
	$gender = $_POST['gender'];
	$pobirth = $_POST['pobirth'];
	$poissue = $_POST['poissue'];
	$isdate = $_POST['isdate'];
	$exdate = $_POST['exdate'];
	$phone = $_POST['phone'];
	$email = $_POST['email'];
	$group = $_POST['group'];
	$dependent = $_POST['dependent'];
	$drelation = $_POST['drelation'];
	$dphone = $_POST['dphone'];
	$regBy = $staff->name;
	$branch = $staff->branch;

	$checkClient = $db->getRow("SELECT * FROM clients WHERE passportNumber = ?", [$passport]);

	if($checkClient) {
		echo "
          <div class=' col-md-10 float-left'>
          <div class='alert alert-danger rounded-0'>
        <strong><h5>Error!!! Client Already Exist</h5></strong>
        </div>
        </div>
          ";
	} else {
		$regClient = $db->insertRow("INSERT INTO clients(agent, agentPhone, passportNumber, dateofIssue, expiryDate, placeofissue, placeofbirth, nationality, surname, givename, dobirth, gender, phone, clientGroup, email, dependentName, relationship, 	dphone, registeredBy, 	branch) 
			VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", 
			[$agent, $agentphone, $passport, $isdate, $exdate, $poissue, $pobirth, $nationality, $surname, $gnames, $dob, $gender, $phone, $group, $email, $dependent, $drelation, $dphone, $regBy, $branch]);
		if($regClient) {
			echo "
		<div class=' col-md-10 float-left'>
          <div class='alert alert-success rounded-0'>
        <strong><h5>Successfully Registered</h5></strong>
        </div>
        </div>
        <div><meta HTTP-EQUIV='Refresh' CONTENT='2; URL=dashboard'></div>
        ";
		}
	}

}

Hi @salissadiq and a warm welcome to the forum.

At the top of the page try inserting the following:

<?php
// if and only if using PHP 7
declare(strict_types=1);
ini_set('display_errors', 'true');
error_reporting(-1);
// there are also Mysql validation checks which are helpful
// when I am on the desktop I will search for the script

// your script goes here

Also try echoing variables and __LINE__ to see how far the script works before failure occurs.

Edit:
Spelling is not my forty :frowning:

Edit:
Back on my desktop and found the script form the online manual:

http://php.net/manual/en/pdo.errorinfo.php

<?php
/* Provoke an error -- bogus SQL syntax */
$stmt = $dbh->prepare('bogus sql');
if (!$stmt) {
    echo "\nPDO::errorInfo():\n";
    print_r($dbh->errorInfo());
}

Aside from your problem, you need to stop creating variables for nothing. You already have them, just use it. Also, you need to check the REQUEST METHOD, not depend on the name of a button to be submitted in order for the script to work.

I believe that $_POST may not have all required values and prefer this method of ensuring a value is assigned:

   $agentphone = $_POST['agentphone'] ?? 'agentphone HAS NOT BEEN SET';
   $passport   = $_POST['passport']   ?? 'passport HAS NOT BEEN SET';

   // OR

   $agentphone = $_POST['agentphone'] ?? NULL;
   $passport   = $_POST['passport']   ?? NULL;
  
  if($agentphone && $passport && $MORE_VARIABLES_GO HERE ):
     // Seems that all variables have been set
  else:
   // DEBUG
      echo '<pre>'; print_r( $_POST); echo '</pre>'; // DISPLAY WITH LINEFEEDS
   echo 'APPEARS THAT NOT ALL REQUIRED VARIABLE HAVE BEEN SET ???';
   die; //exit;
endif; 

Edit:
The ?? PHP 7 only syntax is called the Null coalescing operator:

If not PHP 7 use the following:

if( ! isset( $_POST['Variable_name'] ) ):
  $_POST['Variable_name'] = 'Variable_name not set';
endif;

Just a note, code below works only in PHP 7.

1 Like

Wild guess, is the column you referred to in the query as givename actually givenname ?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.