0

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

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

JLearner
  • 1,201
  • 9
  • 25
  • 39
  • 1
    See http://stackoverflow.com/questions/418898/sqlite-upsert-not-insert-or-replace – walrii Jul 10 '12 at 02:50
  • @walrii I don't see what's the difference in between upsert and insert. So is upsert = update? – JLearner Jul 10 '12 at 03:06
  • I was more pointing out that you want to use the "INSERT OR REPLACE" statement (nice examples in the link). You can also see http://www.sqlite.org/lang_insert.html – walrii Jul 10 '12 at 03:14
  • oh ya. Is a nice example. But i won't want to insert a new record in when it's in a updated page. Update or replace would be good but don't have – JLearner Jul 10 '12 at 03:20

1 Answers1

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.

Dan Kanze
  • 18,097
  • 28
  • 77
  • 133
  • I can't update to be the same. Because it will give me a undefined index when i wan to update it to be the same. Like if $test == $ test is found then update to database. It will give me a undefined index. – JLearner Jul 10 '12 at 02:53
  • thats becuase u dont have a where statement. – Dan Kanze Jul 10 '12 at 02:55
  • sorry dan kanze. I forgot to add one ID = ? – JLearner Jul 10 '12 at 03:06
  • UPDATE requires a WHERE clause. Yea you need to specify wher eyou are updating too or it will always throw that error. – Dan Kanze Jul 10 '12 at 03:08
  • Yeah, Dan.I already added inside my form previously but it still gives me undefined index. When i do a var_dump of that specified file which was existed previously. It gives me null. – JLearner Jul 10 '12 at 03:14
  • I think i'm wrong in some way :(. I updated my code. If file is available to download, it is in the value of download there? Which is the else statement ? – JLearner Jul 10 '12 at 03:18
  • ok i think what you are missing here is that int he first cas eyou dont even have a where ID = and thats whats causing you to preform an INSERT rather than an UPDATE – Dan Kanze Jul 10 '12 at 03:22
  • I didn't use the insert or update statement. Ok.Now the situation is when I click upload, the file value will go in the 'IF' statement. If there is a file consists in the test, it will go in the 'ELSE' statement. Now i want to check whether 'IF' statement falls in do upload files, 'ELSE' statement do same update or don't udpate. But i have 4 same features which i'll need to if($test == $test || $assign == $ assign || $papers == $papers) { don't update} else if($test != test || $assign == $ assign || $papers == $papers) {don update assign and papers) else if ..... continuouslycheck – JLearner Jul 10 '12 at 03:45