I have an update form where there is a student record that has a picture as well as other info. It is with the picture that I am having a problem. The form has a file field with id="picture" name="picture" and then a simple dynamic record call to display the picture right below this file field. The problem is, if I am not updating the picture itself, the entry for the picture in the database gets removed, presumably because I had not entered anything into the file field. The code that controls this is:

((isset($_POST["picture"]))?$_POST["picture"]:"")

It appears that this code says "I there is a posted value from the picture field, add this to the SQL update statement. If not, leave it empty. It removes the entry. What I need is that if I have not selected a new picture, the old picture record is left as is. As it is now, the picture gets removed if I do not reselect it as part of the update.

Can post more code if necessary.

Dave

It sounds like what you are saving is that you are performing a SQL update and you are updating the table with a null (or similiar) value for the picture field thereby removing the existing picture.

probably a bit more code is needed to understand the logic/workflow.

Yes that is correct. Here is the insert code in case this helps.

<?php 
// WA Application Builder Update
if (isset($_POST["Update_x"])) // Trigger
{
  $WA_connection = $rsSayRadio;
  $WA_table = "personalities";
  $WA_redirectURL = "personalities_Detail.php?id=".((isset($_POST["WADAUpdateRecordID"]))?$_POST["WADAUpdateRecordID"]:"")  ."";
  if (function_exists("rel2abs")) $WA_redirectURL = $WA_redirectURL?rel2abs($WA_redirectURL,dirname(__FILE__)):"";
  $WA_keepQueryString = false;
  $WA_indexField = "id";
  $WA_fieldNamesStr = "firstname|lastname|timeslot|day|status|semesteryear|semester|season|bio|daySort|email|facebook|twitter|show_length|picture|bio_link|show_url|show_desc|other_url|other_desc";
  $WA_fieldValuesStr = "".((isset($_POST["firstname"]))?$_POST["firstname"]:"")  ."" . "|" . "".((isset($_POST["lastname"]))?$_POST["lastname"]:"")  ."" . "|" . "".((isset($_POST["timeslot"]))?$_POST["timeslot"]:"")  ."" . "|" . "".((isset($_POST["day"]))?$_POST["day"]:"")  ."" . "|" . "".((isset($_POST["status"]))?$_POST["status"]:"")  ."" . "|" . "".((isset($_POST["semesteryear"]))?$_POST["semesteryear"]:"")  ."" . "|" . "".((isset($_POST["semester"]))?$_POST["semester"]:"")  ."" . "|" . "".((isset($_POST["season"]))?$_POST["season"]:"")  ."" . "|" . "".((isset($_POST["bio"]))?$_POST["bio"]:"")  ."" . "|" . "".((isset($_POST["daySort"]))?$_POST["daySort"]:"")  ."" . "|" . "".((isset($_POST["email"]))?$_POST["email"]:"")  ."" . "|" . "".((isset($_POST["facebook"]))?$_POST["facebook"]:"")  ."" . "|" . "".((isset($_POST["twitter"]))?$_POST["twitter"]:"")  ."" . "|" . "".((isset($_POST["show_length"]))?$_POST["show_length"]:"")  ."" . "|" . "".((isset($_POST["picture"]))?$_POST["picture"]:"")  ."" . "|" . "".((isset($_POST["bio_link"]))?$_POST["bio_link"]:"")  ."" . "|" . "".((isset($_POST["show_url"]))?$_POST["show_url"]:"")  ."" . "|" . "".((isset($_POST["show_desc"]))?$_POST["show_desc"]:"")  ."" . "|" . "".((isset($_POST["other_url"]))?$_POST["other_url"]:"")  ."" . "|" . "".((isset($_POST["other_desc"]))?$_POST["other_desc"]:"")  ."";
  $WA_columnTypesStr = "',none,''|',none,''|',none,''|',none,''|',none,''|none,none,NULL|none,none,NULL|',none,''|',none,''|',none,''|',none,''|',none,''|',none,''|none,none,NULL|',none,''|',none,''|',none,''|',none,''|',none,''|',none,''";
  $WA_comparisonStr = " LIKE | LIKE | LIKE | LIKE | LIKE | = | = | LIKE | LIKE | LIKE | LIKE | LIKE | LIKE | = | LIKE | LIKE | LIKE | LIKE | LIKE | LIKE ";
  $WA_fieldNames = explode("|", $WA_fieldNamesStr);
  $WA_fieldValues = explode("|", $WA_fieldValuesStr);
  $WA_columns = explode("|", $WA_columnTypesStr);

  $WA_where_fieldValuesStr = "".((isset($_POST["WADAUpdateRecordID"]))?$_POST["WADAUpdateRecordID"]:"")  ."";
  $WA_where_columnTypesStr = "none,none,NULL";
  $WA_where_comparisonStr = "=";
  $WA_where_fieldNames = explode("|", $WA_indexField);
  $WA_where_fieldValues = explode("|", $WA_where_fieldValuesStr);
  $WA_where_columns = explode("|", $WA_where_columnTypesStr);
  $WA_where_comparisons = explode("|", $WA_where_comparisonStr);

  $WA_connectionDB = $database_rsSayRadio;
  mysql_select_db($WA_connectionDB, $WA_connection);
  if (!session_id()) session_start();
  $updateParamsObj = WA_AB_generateInsertParams($WA_fieldNames, $WA_columns, $WA_fieldValues, -1);
  $WhereObj = WA_AB_generateWhereClause($WA_where_fieldNames, $WA_where_columns, $WA_where_fieldValues,  $WA_where_comparisons );
  $WA_Sql = "UPDATE `" . $WA_table . "` SET " . $updateParamsObj->WA_setValues . " WHERE " . $WhereObj->sqlWhereClause . "";
  $MM_editCmd = mysql_query($WA_Sql, $WA_connection) or die(mysql_error());
  if ($WA_redirectURL != "")  {
    if ($WA_keepQueryString && $WA_redirectURL != "" && isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !== "" && sizeof($_POST) > 0) {
      $WA_redirectURL .= ((strpos($WA_redirectURL, '?') === false)?"?":"&").$_SERVER["QUERY_STRING"];
    }
    header("Location: ".$WA_redirectURL);
  }
}
?>

This code is from the Web Assist app builder, hence the WA references in the code.

Dave

So, the issue seems to be:

((isset($_POST["picture"]))?$_POST["picture"]:"")

It is inserting "" into the picture field on the update when a new picture has not been selected. Unlike my other fields, I have a file field to facilitate uploading pics when I am not able to populate with the field content info when the user opens the update page. So I think I need one of two solutions: either some other value to pass to the db other than "" if no new picture is selected or, a way to put the filename of the pic into the fileupload field when the user opens the update page. I think the first option would be better ...unless someone out there has a much better idea than mine? (likely :-)

I think the first option would be better

I would beleive that the best approach is not to include that field in the SQL UPDATE statement when you detect that $_POST["picture"] is null or "". I can't think of any of way to handle this scenario.

Isn't that what this code is doing though?

((isset($_POST["picture"]))?$_POST["picture"]:"")

Dave

Yes, but if its null, then you dont want a null value to be included in the UPDATE statement.

You wouldnt want that field to be included at all in your SQL update statement.

Member Avatar
diafol

The picture should be $_FILES['picture'] surely?

Anyway, as Jorge says, if the field is blank/null whatever, just leave it off the SQL. You can build up your SQL in steps as you know.

//Say we have this...
$setList = "`field1` = '$value1', `field2` = '$value2', `field3` = '$value3'";
//let's say that $newimage contains the path of a new image - but is empty if no image uploaded
if($newimage) $setList .= ",`image` = '$newimage'";

$sql = "UPDATE table SET $setList WHERE ...";