bigup401 has asked for the wisdom of the Perl Monks concerning the following question:

i dont know why my code doesn;t check the id number and phone availability. it doesn;t insert data if phone or id number exits but it still goes to next.pl it insert data very well if phone or id number not exits. but i want to remain on same page if phone or id number exits and print error

if ($PHONE, $ID_NUMBER) { $CHECK_PHONE = $DBH->prepare("SELECT COUNT(*) FROM tbl1 WHERE PHONE = +?"); $CHECK_PHONE->execute($PHONE); $P = $CHECK_PHONE->fetchrow_arrayref(); $CHECK_ID_NUMBER = $DBH->prepare("SELECT COUNT(*) FROM tbl1 WHERE ID_N +UMBER = ?"); $CHECK_ID_NUMBER->execute($ID_NUMBER); $ID = $CHECK_ID_NUMBER->fetchrow_arrayref(); if ($P eq $PHONE) { $error_phone = "phone exits"; } if ($P eq $PHONE) { $error_id = "id exits"; } if ($P ne $PHONE && $ID ne $ID_NUMBER){ $ADD_DATA = $DBH->prepare('INSERT INTO tb11(PHONE, ID_NUMBER) VALUES(?,?)'); $ADD_DATA->execute($PHONE, $ID_NUMBER); $ADD_DATA->finish; # go to next.pl next page } }

Replies are listed 'Best First'.
Re: problem checking availability
by haj (Vicar) on Sep 03, 2018 at 21:52 UTC
    This code has some issues...
    1. if ($PHONE, $ID_NUMBER) - That is equivalent to if ($ID_NUMBER). I guess you want an and or an or instead of the comma, but don't dare to guess.
    2. You assign an arrayref to $P (something like [ 1 ], and then compare this arrayref to the actual phone number. Under no circumstances will this be equal. Probably you want to test if $P->[0].
    3. You test $P eq $PHONE twice. The second test should probably test $ID->[0]
    4. You select from tbl1 and insert into tb11, that are two different tables (lowercase 'L' plus number 1 vs. number 11).
    5. You should always use strict and use warnings.
    6. You should use uppercase variable names only for constants.
    7. You should check for errors after your calls to execute.

      i tried that already before. bt still was getting same results

      if ($PHONE) { $CHECK_PHONE = $DBH->prepare("SELECT COUNT(*) FROM tbl1 WHERE PHONE = +?"); $CHECK_EMAIL->execute($PHONE); $P = $CHECK_PHONE->fetchrow_arrayref(); if($E->[0]) { $error_phone = "phone exits"; } $CHECK_ID_NUMBER = $DBH->prepare("SELECT COUNT(*) FROM tbl1 WHERE ID_N +UMBER = ?"); $CHECK_ID_NUMBER->execute($ID_NUMBER); $ID = $CHECK_ID_NUMBER->fetchrow_arrayref(); if($ID->[0]) { $error_phone = "id exits"; } }

        Eliminating just one error isn't enough for software to work correctly. You need to eliminate all of them, preferably without introducing new ones. In your recent example I see a number of errors which you might have spotted if you had used use strict:

        1. You prepare a statement to $CHECK_PHONE and then execute it on $CHECK_EMAIL with the phone number as parameter. Calling fetchrow_arrayref on a statement which hasn't been executed will not return an array reference.
        2. Next you test for $E->[0] but $E has never been set.
        3. In both error branches you set the variable $error_phone.

        You should really try to provide a complete example, run it on your database and then post it, together with the result. We are still at a level where I just need to look at the code to spot several - new - problems.