首页 > 解决方案 > PHP/SQL - IF conditional not working as intended

问题描述

I'm new to programming and I've been learning a lot recently. Currently I'm working in PHP with SQL tables. I have a function in a system similar to renting where each item "rented" generates a row in the table with the user and item data, but I need to limit it to just one kind of item for each user, for example:

Okay:

Not Okay:

What I have right now inserts the row completely fine, but the problem comes when it checks if the user already has an item of a kind. The first item I insert works, it inserts the row on the table and if you try to insert the same item again it won't let you, but once I try to do it with a second item it keeps letting me insert the same item over and over again without limit. I think it has something to do with the way I'm using the conditionals, but I can't solve it and I've been here for 2 days without any progress.

Here is my code:

function BuscaPREST($rut,$serial){
        include("conexion.mydb.php");

        //Gets user info
        $sql="SELECT * FROM ALUMNO WHERE ALU_RUT=".$rut;

        $result=odbc_exec($Conx_Web, $sql);
        if(odbc_fetch_row($result)){
            $ALU_RUT=odbc_result($result, "ALU_RUT");
            $ALU_DIG_VER=odbc_result($result, "ALU_DIG_VER");
            $ALU_NOM=odbc_result($result, "ALU_NOM");
            $ALU_APE=odbc_result($result, "ALU_APE_PAT")." ".odbc_result($result, "ALU_APE_MAT");
            $ALU_MAIL=odbc_result($result, "ALU_MAIL");

            include("conexion.db.php");

            //Gets items info
            $sql2="SELECT * FROM ARTICULOS WHERE ART_SER='".$serial."'";

            $result2=odbc_exec($Conx_DB, $sql2);    
            if(odbc_fetch_row($result2)){
                $ART_TIPO=odbc_result($result2, "ART_TIPO");
                $ART_MOD=odbc_result($result2, "ART_MOD");
                $ART_SER=odbc_result($result2, "ART_SER");

                //Gets user and item info from table where I'm inserting the rentals
                $sql3="SELECT * FROM PRESTAMOS WHERE ALU_RUT=".$ALU_RUT;

                $res=odbc_exec($Conx_DB, $sql3);
                $ALU_RUT2=odbc_result($res, "ALU_RUT");
                $ART_TIPO2=odbc_result($res, "ART_TIPO");

                //Checks if the user already has an item of the same kind
                if($ALU_RUT==$ALU_RUT2 && $ART_TIPO==$ART_TIPO2){   
                    $msg="El alumno ya tiene un artículo de este tipo.";
                    echo "<script type='text/javascript'>alert('$msg');</script>";
                }else{
                    $sql4=odbc_prepare($Conx_DB,"INSERT INTO PRESTAMOS (ALU_RUT,ALU_NOM,ALU_APE,ALU_MAIL,FEC_PRE,ART_SER,ART_MOD,ART_TIPO,ALU_DIG_VER) VALUES ('".$ALU_RUT."','".$ALU_NOM."','".$ALU_APE."','".$ALU_MAIL."','".date('d-m-Y')."','".$ART_SER."','".$ART_MOD."','".$ART_TIPO."','".$ALU_DIG_VER."');");   
                    odbc_execute($sql4);

                    $message="PRÉSTAMO INGRESADO CORRECTAMENTE";
                    echo "<script type='text/javascript'>alert('$message');</script>";
                }
            }
            odbc_free_result($result2);
            odbc_close ($Conx_DB);
         }
         odbc_free_result($result);
         odbc_close ($Conx_Web);    
    } 

I think my code is quite messy, but remember I'm still learning.

Any suggestion would be appreciated, thanks!

标签: phphtmlsql

解决方案


I suspect the "SELECT * FROM PRESTAMOS WHERE ALU_RUT=".$ALU_RUT will return multiple rows (all rows for the user if I understand correctly - not easy with non-english names).

You need to either limit the search further

"SELECT * FROM PRESTAMOS WHERE ALU_RUT=".$ALU_RUT`." AND ART_TIPO=".$ART_TIPO

or check all returned rows (i.e. use a loop instead of only first row).

Also note that if you modify the query to only return rows for the given user and type, you don't need to check the returned row again, just whether there is a row or not. In real deployment, you'd most likely instead use

"SELECT COUNT(1) FROM PRESTAMOS WHERE ALU_RUT=".$ALU_RUT`." AND ART_TIPO=".$ART_TIPO

and check for nonzero returned count - for performance reasons.


推荐阅读