Dustin
Dustin

Reputation: 4459

How to ignore empty fields when inserting into MySQL database?

I am creating a very basic admin page to control content on another page.

I have a few fields on the admin page, one of which is a file upload field. When the form is submitted the file gets uploaded to a folder and the filename is placed into the database.

The problem is that when somebody goes to the admin page to update the content and they don't upload a file, the filename value gets erased from the database.

Is there a way to do a MySQL query or something else that ignores empty values?

$headerimage=($_FILES['headerimage']['name']); 
$cktest = $_POST['editor1'];
$LikeText = $_POST['editor2'];

$sql = "UPDATE installs SET noLikeText = '$cktest', LikeText = '$LikeText', headerImage = '$headerimage' WHERE pageID = '$page_id'";

Upvotes: 0

Views: 1648

Answers (3)

user1809086
user1809086

Reputation: 11

you can use this...

For example

if($headerimage != "")
{
$sql = "UPDATE installs SET noLikeText = '$cktest', LikeText = '$LikeText', headerImage = '$headerimage' WHERE pageID = '$page_id'";
}else{

$sql = "UPDATE installs SET noLikeText = '$cktest', LikeText = '$LikeText' WHERE pageID = '$page_id'";
}

Upvotes: 1

Kai Qing
Kai Qing

Reputation: 18833

You should start by showing the queries. I would guess you're not writing your update query correctly.

$file = "";
if(isset($_FILES['file']['tmp_name']))
{
    //handle images here
    $file = "`file` = 'some_filename.txt', ";
}
$sql = "UPDATE `table` SET $file `field` = '".mysql_real_escape_string($some_value)."' WHERE `id` = '".mysql_real_escape_string($some_id)."'";

Basic idea that you're building your query as a string based on conditions. This always works for me.

Upvotes: 0

Mike Purcell
Mike Purcell

Reputation: 19989

Sounds like you need to add some logic to your app. Majority of the time what you are asking for can be accomplished by adding a 'delete' checkbox to the form, so if the user clicks the delete checkbox it will remove the row, if they do not, then nothing regarding the file, occurs.

Upvotes: 0

Related Questions