3

I am trying to submit data to a database. I have an update form that has several fields on it that pulls data from the database into the fields. Submitting it updates the data in the database. The two fields i am concerned with our two image uploaders that i have in the form. I have been doing this successfully when i have only one image uploader on the page, but can't wrap my head around the conditional syntax when i add the second one. Below is the code that i am using for the forms that have only one image uploader. It looks to see if that submitted element is empty. If so, it runs an update statement and leaves out the image variable. If the image variable is not empty it runs an update statement with that field added to the statement.

if($imgProfileFull == ""){
$query = "update students set `nameFirst` = '$nameFirst', `nameLast` = '$nameLast', `profile` = '$profile', `priority` = '$priority', `active` = '$active', `_modifiedDate` = '$_modifiedDate' where `id` = '$id'";
}else{
if((($_FILES["image"]["type"] == "image/jpeg")
|| ($_FILES["image"]["type"] == "image/pjpeg"))
&& ($_FILES["image"]["size"] < 500000))
{
  if($_FILES["image"]["error"] > 0){
    header("location:students.php?file=error");
  }else{
    move_uploaded_file($_FILES["image"]["tmp_name"],
    "../images/students/full/" . $imgProfileFull);
  }
}else{
  header("location:students.php?file=error");
}
$query = "update students set `imgProfileFull` = '$imgProfileFull', `nameFirst` = '$nameFirst', `nameLast` = '$nameLast', `profile` = '$profile', `priority` = '$priority', `active` = '$active', `_modifiedDate` = '$_modifiedDate' where `id` = '$id'";
}

How would i write my update statement to include another image field. The variable would be $imgProfileThumb. Basically i want to update the two image columns in the database if, and only if, they are NOT empty. This is because the way the form is built, the image field is technically always empty unless an image actually gets uploaded. Any thoughts? Thanks!!

1 Answer 1

4

You can use an if statement to create your query dynamically.

$query = "UPDATE students SET ";

if ($imgProfileFull !== ""){
    $query .= "`imgProfileFull` = '$imgProfileFull', ";
}

if ($imgProfileThumb !== ""){
    $query .= "`imgProfileThumb` = '$imgProfileThumb', ";
}

$query .= "`nameFirst` = '$nameFirst',
           `nameLast` = '$nameLast',
           `profile` = '$profile',
           `priority` = '$priority',
           `active` = '$active',
           `_modifiedDate` = '$_modifiedDate'
           WHERE `id` = '$id'";

You may also wish to use mysql_real_escape_string for your string data if you are not already doing so.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks! I'll give that a try. I'll bump you one point and add the check if it works when i test it. :)
I have tested this and it appears to be working! Thanks @Mark!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.