简体   繁体   English

无尽的If-Else语句

[英]Endless If-Else statements

I'm trying to update the records whereby I found out that I will be doing alot of If-Else Statements for checking. 我正在尝试更新记录,据此发现我将进行大量If-Else语句检查。 For example, now i have 4 upload buttons inside my form. 例如,现在我在表单中有4个上传按钮。 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();
}

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). 所以我的问题是如何缩短我的ife-else语句,以检查单个值是否已确实存在于数据库中,并且不更新该特定列。 Kindly advise thanks 请告知谢谢

Code for my form: 我的表格代码:

<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 code: Update.php代码:

<?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();
}
?>

use empty() 使用empty()

you dont need the $test == $test case becuase if the same then it will just update it to be the same. 您不需要$ test == $ test用例,因为如果相同,则将其更新为相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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