Search code examples
phpmysqljoomla

Checking if a record exists in a MYSQLDatabase using joomla


I am using a Joomla form extension to collect the data from the user. It is supposed to collect the fields named transaction id and amount. Now, I've been trying to program the form to check if the record already exists in the database but I keep getting a blank error in my page. Here is my code:

<?php
$db = JFactory::getDbo();
$app = JFactory::getApplication();
$transaction = $app->input->get("transaction");
$query = $db->getQuery(true);
$query->select($db->quoteName('payment_code'))
      ->from($db->quoteName('#__payment_code'))
      ->where($db->quoteName('payment_code') . ' = '. $db->quote('$transaction'));
$db->setQuery($query);
$tg = "zuuu";
$result = $db->loadResult();
  $min_chars =4;
   $max_chars = 15;
$error = "THE transaction ID should not exceed 15 characters.";
$error1="please enter a minimum of 4 characters .";
if (strlen($post["transaction"]) < $max_chars) {
   throw new Exception($result);
}
if (strlen($post["transaction"]) < $min_chars) {
   throw new Exception($error1);
}
if (strlen($post["amount"]) > $max_chars) {
   throw new Exception($error);
}
if (strlen($post["amount"]) < $min_chars) {
   throw new Exception($error1);
}


if ($result !== null){
    throw new 
    Exception($tg);
}
?>

Solution

    1. A blank page = PHP error with error reporting set to OFF. Enable PHP error reporting and set to Maximum to see the error
    2. After you've set the query, but before you pass it to the database object, dump your query to the screen. Feed that SQL code to phpMyAdmin to analyse your query. use:
    echo $query->dump();
    die('Copy the above SQL in phpMyAdmin and run it');
    
    1. You'll notice that you have literal $transaction in your query. So change
    $db->quote('$transaction')
    

    to

    $db->quote($transaction)