0

我正在尝试更新记录,我发现我将做很多 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 语句以检查数据库中是否已经存在单个值并且不更新该特定列。请指教谢谢

4

1 回答 1

1

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:

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

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

于 2012-07-10T02:50:41.747 回答