无穷无尽的 If-Else 语句 [英] Endless If-Else statements

查看:27
本文介绍了无穷无尽的 If-Else 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新记录,我发现我将执行大量 If-Else 语句进行检查.例如,现在我的表单中有 4 个上传按钮.如果已附加文档,则不会有任何上传按钮.但是它更新到数据库时,它会显示错误,因为用户没有附加任何文件.也许我会在我的代码中进行解释,并给出更清晰的图片.

I'm trying to update the records whereby I found out that I will be doing alot of If-Else Statements for checking. For example, now i have 4 upload buttons inside my form. If the document has been attached, there would not be any upload button. But it is updated to the database, it will show errors because the user did not attached any document. Maybe I'll explain out in my code and will give a clearer picture.

Code for my form:
<form id = "update" action ="update.php">
//Code is repeated for all upload and download buttons just that one is for test, assign, and papers
<?php
if ($Attached== "No")
{
            echo "<select name=\"Test\" id=\"Test\">";
            echo "<option value=\"No\" selected=\"selected\">No</option>";
            echo "<input name=\"Attached[test]\" id=\"Test\" type=\"file\"/>";
            echo "</select>";

}
else
{ 
         Button to download the document
            $fullpath = "./documents/."$Test"; 
        echo "<input type=\"hidden\" name=\"fullpath\" value=\"$fullpath \"/>";
        echo "<input type=\"submit\" name=\"download\" value=\"download\"/>";
}
?>
</form>

Update.php code:
//So if i wish to update into my database sqlite3, i'll need to check as follows:
$test = $_POST['Attached[test]'];
$ID = 1;
$DB = = new PDO('sqlite:database/Test.DB');
If ($test != "")
{
    $update = $DB->prepare('update test set test =?, assign =?, papers =?);
    $execute = $update-> execute (array($test, $assign, $paper));

}
else if ($test == $test)
{
    $update = $DB->prepare('update test set assign =?, papers =? where ID=?);
    $execute = $update-> execute (array($assign, $paper));
}
else
{
    moveuploaded_files();
}

所以我的问题是如何缩短我的 ife-else 语句以检查单个值是否已经实际存在于数据库中并且不更新该特定列.请指教谢谢

So my question is how can i shorten my ife-else statement to check if the individual value actually exists in database already and don't update that particular column(s). Kindly advise thanks

推荐答案

我的表单代码:

<form id = "update" action ="update.php">
<?php
if ($Attached== "No")
{
            echo "<select name=\"Test\" id=\"Test\">";
            echo "<option value=\"No\" selected=\"selected\">No</option>";
            echo "<input name=\"Attached[test]\" id=\"Test\" type=\"file\"/>";
            echo "</select>";

}
else
{ 
            Button to download the document
            echo "<input type=\"submit\" name=\"download\" value=\"download\"/>";
}
?>
</form>

Update.php 代码:

Update.php code:

<?php
$test = $_POST['Attached[test]'];
$DB = new PDO('sqlite:database/Test.DB');
if (!empty($test))
{
    $update = $DB->prepare('update test set test =?, assign =?, papers =? WHERE idk = you tell me');
    $execute = $update-> execute (array($test, $assign, $paper));

}
else
{
    moveuploaded_files();
}
?>

使用empty()

您不需要 $test == $test case 因为如果相同,那么它只会将其更新为相同.

you dont need the $test == $test case becuase if the same then it will just update it to be the same.

这篇关于无穷无尽的 If-Else 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆