Php Code Shortening

Hi to all SitePoint Members,

I have successfuly developed a small CMS to manage some records. Now I wana re-develop this CMS by improving its codes. This time i am focusing on “Code Shortening”
Friends please suggest me ways how can I shorten this code


<?php
require_once("config.php");
require_once("functions.php");
require_once("startsession.php");
$sr_no = (!isset($_POST['sr_no']) || trim($_POST['sr_no']) == "") ? die ('ENTER SERIAL NO') : 
mysql_real_escape_string(trim($_POST['sr_no']));

$bill_no = (!isset($_POST['bill_no']) || trim($_POST['bill_no']) == "") ? die ('ENTER BILL NO') : 
mysql_real_escape_string(trim($_POST['bill_no']));

$bill_date = (!isset($_POST['bill_date']) || trim($_POST['bill_date']) == "") ? die ('ENTER BILL DATE') :
parseDate(mysql_real_escape_string(trim($_POST['bill_date'])));

$ref_no = (!isset($_POST['ref_no']) || trim($_POST['ref_no']) == "") ? die ('ENTER REF NO') :
mysql_real_escape_string(trim($_POST['ref_no']));

$vehicle = (!isset($_POST['vehicle']) || trim($_POST['vehicle']) == "") ? die ('ENTER CLAIM OF') : 
mysql_real_escape_string(trim($_POST['vehicle']));

$insured_name = (!isset($_POST['insured_name']) || trim($_POST['insured_name']) == "") ? die ('ENTER INSURED NAME') :
mysql_real_escape_string(trim($_POST['insured_name']));

$vehicle_no = (!isset($_POST['vehicle_no']) || trim($_POST['vehicle_no']) == "") ? die ('ENTER VEHICLE NO') :
mysql_real_escape_string(trim($_POST['vehicle_no']));

$date_of_loss = (!isset($_POST['date_of_loss']) || trim($_POST['date_of_loss']) == "") ? die ('ENTER DATE OF LOSS') :
parseDate(mysql_real_escape_string(trim($_POST['date_of_loss'])));

$date_of_survey = (!isset($_POST['date_of_survey']) || trim($_POST['date_of_survey']) == "") ? die ('ENTER DATE OF SURVEY') :
parseDate(mysql_real_escape_string(trim($_POST['date_of_survey'])));

$claim_no = (!isset($_POST['claim_no']) || trim($_POST['claim_no']) == "") ? die ('ENTER CLAIM NO') :
mysql_real_escape_string(trim($_POST['claim_no']));

$policy_no = (!isset($_POST['policy_no']) || trim($_POST['policy_no']) == "") ? die ('ENTER POLICY NO') :
mysql_real_escape_string(trim($_POST['policy_no']));

$insurers = (!isset($_POST['insurers']) || trim($_POST['insurers']) == "") ? die ('ENTER INSURANCE COMPANY NAME') :
mysql_real_escape_string(trim($_POST['insurers']));

$issuing_office = (!isset($_POST['issuing_office']) || trim($_POST['issuing_office']) == "") ? die ('ENTER INSURANCE POLICY ISSUING OFFICE') :
mysql_real_escape_string(trim($_POST['issuing_office']));

$survey_fees = (!isset($_POST['survey_fees']) || !is_numeric($_POST['survey_fees'])) ? die ('ENTER SURVEY FEES AMOUNT (numeric value)') :
mysql_real_escape_string(trim($_POST['survey_fees']));

$type_of_report = (!isset($_POST['type_of_report']) || trim($_POST['type_of_report']) == "") ? die ('ENTER TYPE OF REPORT') :
mysql_real_escape_string(trim($_POST['type_of_report']));

$remarks = (!isset($_POST['remarks']) || trim($_POST['remarks']) == "") ? die ('ENTER REMARKS') :
mysql_real_escape_string(trim($_POST['remarks']));

$user_name = $_SESSION['username'];


$query = "INSERT INTO ledger (sr_no,bill_no,bill_date,ref_no,insured_name,vehicle,vehicle_no,date_of_loss,date_of_survey,type_of_report,claim_no,policy_no,insurers,issuing_office,survey_fees,remarks,now,user)
VALUES ('NULL','$bill_no','$bill_date','$ref_no','$insured_name','$vehicle','$vehicle_no','$date_of_loss','$date_of_survey','$type_of_report','$claim_no','$policy_no','$insurers','$issuing_office','$survey_fees','$remarks',NOW(),'$user_name')";
if (!mysql_query($query,$con))
 {
 die('Could not update database' . mysql_error());
 }
?>

By making use of a function.


function trimAndEscapePost($name, $error)
{
    $value = '';
    if (isset($_POST['remarks'])) {
        $value = $_POST['remarks'];
    }
    $value = trim($value);
    if ($value == '') {
        die ($error);
    }
    return mysql_real_escape_string($value); 
}
...
$sr_no = trimAndEscapePost('sr_no', 'ENTER SERIAL NO');
$bill_no = trimAndEscapePost('bill_no', 'ENTER BILL NO');

Yes you can still use the same ternary operations within the function as before, I prefer leaving the code more readable for others. That’s a stylistic choice for you to make.

Further on from that, you may want to use an array to store the post name and error message.


$posts = array(
    'sr_no' => 'ENTER SERIAL NO',
    'bill_no' => 'ENTER BILL NO',
    ...
);
foreach ($posts as $name => $error) {
    $$name = trimAndEscapePost($name, $error);
}

The old MySQL functions…ewww.


$index = array( 'sr_no', 'bill_no', 'bill_date', 'ref_no', 'vehicle',
  'insured_name', 'vehicle_no', 'date_of_loss', 'date_of_survey', 'claim_no',
  'policy_no', 'insurers', 'issuing_office', 'survey_fees', 'type_of_report',
  'remarks' );
$index = array_fill_keys( $index, null );

foreach ( $index as $key => $val ) {
  
  if ( isset( $_POST[ $key ] ) && trim( $_POST[ $key ] ) !== "" ) {
    $index[ $key ] = trim( $_POST[ $key ] );
    continue;
  }
  
  // Do some kind of error handling...
  // DO NOT USE "DIE" OR "EXIT"
  // ...
  
}

$index['username'] = $_SESSION['username'];

$sql = "INSERT INTO ledger (
          sr_no
        , bill_no
        , bill_date
        , ref_no
        , insured_name
        , vehicle
        , vehicle_no
        , date_of_loss
        , date_of_survey
        , type_of_report
        , claim_no
        , policy_no
        , insurers
        , issuing_office
        , survey_fees
        , remarks
        , now
        , user
      ) VALUES (
          'NULL'
        , :bill_no
        , :bill_date
        , :ref_no
        , :insured_name
        , :vehicle
        , :vehicle_no
        , :date_of_loss
        , :date_of_survey
        , :type_of_report
        , :claim_no
        , :policy_no
        , :insurers
        , :issuing_office
        , :survey_fees
        , :remarks
        , NOW()
        , :username
      )";

try {
  
  $db = new PDO( "mysql:stuff", "user", "pass" );
  $stm = $db->prepare( $sql );
  $sth->execute( $index );
  
} catch ( Exception $e ) {
  
  // More Error handling.
  var_dump( $e );
  
}

thank you so much friends for providing me these short codes. It will help me learn php programming. Surely I will use these codes.
Thanks again.