无尽的If-Else陈述

时间:2012-07-10 02:44:56

标签: php sqlite

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

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语句以检查单个值是否已经存在于数据库中并且不更新该特定列。 请建议谢谢

1 个答案:

答案 0 :(得分:1)

我的表格代码:

<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代码:

<?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 becuase如果相同那么它只会将它更新为相同。

相关问题