简体   繁体   中英

PHP validation to check if record exists

I'm trying to create a validation for a form. When a user fills out the form, it is supposed to run a set of queries. The first is to check if a records already exists in the table. If it does exist, then it doesn't need to run the the next 2 queries, which are to INSERT and UPDATE.

I'm not sure what I am doing wrong, but the table already has an existing record. After checking the table, it still runs the INSERT and UPDATE queries. They should not fire. It should not do anything.

Here is my code: * I'm starting my code from the for loop, which is just taking an array of BOL numbers and CONTAINER numbers that the user manually selected. I exploded the array, but I will not show that code as I do not think it is necessary to show in this case *

<?php 
for($i = 0; $i < $count; $i++)
{
 $bolService = $bolArray[$i];
 $conService = $conArray[$i];

 $checkService = "SELECT * FROM import_service WHERE bol = '" . $bolService . "' AND container = '" . $conService . "'";
 $checkSerRes = mysql_query($checkService);
 $checkSerNum = mysql_num_rows($checkSerRes);

 if($checkSerNum > 0)
 {
   $successService = false;
 }
 elseif($checkSerNum = 0)
 {
   $sql_query_string = mysql_query
   ("INSERT INTO import_service (bol, container) VALUES ('$bolService','$conService')");

   $updateService = mysql_query ("UPDATE import_dispatch_details SET SERVICE = 'Y'
    WHERE BOL_NUMBER = '$bolService' AND CONTAINER = '$conService')");

   $successService = true;
 }
}

// javascript fires an ALERT message in this next set of code

if($successService = true)
{
 echo ("<script language='javascript'>
        window.alert('Record has been saved')
        window.location.href=''
        </script>");
}
// if checkSerNum > 0, then it should skip the INSERT and UPDATE and fire the code below
elseif($successService = false)
{
 echo ("<script language='javascript'>
        window.alert('There was an error saving the record')
        window.location.href=''
        </script>");
}       
?>

I'm not sure why this is not working correctly. I need this validation to work. I'm sure there is an alternative method, but this is what I got.

Please help me make this work.

Thank you in advance.

This elseif($checkSerNum = 0) needs to be elseif($checkSerNum == 0)

You're presently doing an assignment instead of a comparison.

Including if($successService = true) and elseif($successService = false) so add another = sign.

Add error reporting to the top of your file(s) which will help during production testing.

error_reporting(E_ALL);
ini_set('display_errors', 1);

Footnotes:

mysql_* functions deprecation notice:

http://www.php.net/manual/en/intro.mysql.php

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/ .

Documentation for MySQL can be found at » http://dev.mysql.com/doc/ .

This isn't quite efficient (you are selecting * from your table, which you aren't using - waste of memory?). Why don't you do something like this:

for ($i = 0; $i < $count; $i++)
{
    $bolService = $bolArray[$i];
    $conService = $conArray[$i];

    $recordExists = false;
    $result = mysql_query("SELECT COUNT(*) AS recordCount FROM import_service WHERE bol = '" . $bolService . "' AND container = '" . $conService . "'");
    if ($result) {
        $row = mysql_fetch_assoc($result);
        $recordExists = ($row['recordCount'] >= 1);
    }

    if ($recordExists)
    {
        $successService = false;
    }
    else
    {
        $sql_query_string = mysql_query
        ("INSERT INTO import_service (bol, container) VALUES ('$bolService','$conService')");

        $updateService = mysql_query
        ("UPDATE import_dispatch_details SET SERVICE = 'Y' 
        WHERE BOL_NUMBER = '$bolService' AND CONTAINER = '$conService')");

        $successService = true;
    }
}

PS mysql_* is officially deprecated. Please use PDO or MySQLi . Also, your code is potentially open to SQL Injection .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM