php - 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:
- User1 / Car
- User1 / Truck
- User1 / Bike
Not Okay:
- User1 / Car
- User1 / Car
- User1 / Truck
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!
解决方案
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.
推荐阅读
- html - r Google 新闻结果链接
- python - 当我设置freq="W"时,为什么输出变成freq="W-Sun"?
- python - 使用具有多个目标变量和多个搜索条件的 KDtree 在 sklearn 中查找 Kneighbors
- jsp - Dropzone 不适用于任何浏览器
- java - 任务不可序列化 - Java 1.8 和 Spark 2.1.1
- c# - 在 asp core 2.1 中上传后无法访问图像
- sql - 相关产品的 SQL Server 查询
- java - 如何打印新对象的参数?
- javascript - 创建具有动态内容的图像
- java - Java JButton 不会遍历容器中的 JLabel(带有图像)