I trying to check in my database if a customer with the same postcode exist already in the customer table and if they do.
I have the PHP code below and the issue I am having is that. I already get the echo message, weather the exist or they don't exist. select_to_array
is a function in the config.php
and it works fine.
<?php
require("config.php");
$validate_value = isset($_POST['value']) ? $_POST['value'] : null;
$validate_Year =isset($_POST['Year']) ? $_POST['value'] : null;
$validate_postcode = isset($_POST['postcode']) ? $_POST['value'] : null;
$select = "SELECT count (*) FROM customers WHERE reg_year= :reg_year
AND reg_code = :reg_name AND reg_value = :reg_value";
$bind_bb = array(':reg_year' => $validate_Year,
':reg_code' => $validate_code,
':reg_value' => $validate_value);
$validate_result = select_to_array($db_connect,$select, $index, $bind_bb);
if ($validate_result[0][0] == 0) {
echo 'Customer Does not no exist';
}
else {
echo 'Already exist, please try a different postcode';
}
?>
You check is incorrect from many points - first you need to know if there's any data, not how much records you have, therefore your SELECT
should feature LIMIT 1
as this is pretty much sufficient. As for
if ($validate_result[0][0] == 0) {
this is not valid too, as if your dataset is empty you will be throwing notices. Replace with
if (empty($validate_result)) {
or
if (count($validate_result) == 0) {
also it looks you may have wrong query as this:
':reg_name' => $validate_name,
may look for empty reg_name
as $validate_name
is not set in your code (while other params are)