0

I form that edits a table in the database. However a user may not selcect an image so if they dont I do not want to update that field. I have tried a couple of ways to do this but none work. Is there an easy way? Thanks.

if ($_POST["image"]!=='') { 
      $image= "image=%s"; 
      $imageValue =    "GetSQLValueString(".$_POST['image'].", 'text'),";
};

$insertSQL = sprintf("UPDATE taxi SET name=%s, taxi_link=%s, ". $image." WHERE id=%s",
                   GetSQLValueString($_POST['name'], "text"),
                   GetSQLValueString($_POST['link'], "text"),
                   $imageValue,
                   GetSQLValueString($_POST['id'], "int"));
3
  • Your above attempt doesn't work for one reason. The , after taxi_link=%s. Remove that , and change 2nd row to $image= ", image=%s";. Commented May 2, 2012 at 13:15
  • thanks for the pointer, there is still one issue with that in that $imageValue is blank but still puts a blank entry giving an error Commented May 2, 2012 at 13:53
  • I fixed your query in below answer. Commented May 2, 2012 at 14:24

2 Answers 2

2

If I understand you properly, couldn't you put the whole thing in the if statement?

if ($_POST["image"]!=='') { 
      $image= "image=%s"; 
      $imageValue =    "GetSQLValueString(".$_POST['image'].", 'text'),";
      $insertSQL = sprintf("UPDATE taxi SET name=%s, taxi_link=%s, ". $image." WHERE id=%s",
                   GetSQLValueString($_POST['name'], "text"),
                   GetSQLValueString($_POST['link'], "text"),
                   $imageValue,
                   GetSQLValueString($_POST['id'], "int"));
      //execute query
} else {
    //tell the user nothing happened
} //no semicolon required

So now it will only build and execute the query if the image is specified, and you can do something else if it's not specified

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

2 Comments

I though this was what he meant first, but he means is there any more elegant (and functional) way to update the image column then he already does.
yeah thought of this but was wondering was there a way without writing multiple queriies
1

If you use a framework like CodeIgniter you can simply:

$arr_values = array('name' => $_POST['name'],
                    'taxi_link' => $_POST['link']);

if ( ! $_POST["image"])
    $arr_values['image'] = $_POST['image'];

$this->db->update($arr_values, array('id' => $_POST['id']));

The update() simply handles an array, and you can simply add additional values to that array based on conditions.

Personally, I made my own function like this one which works perfect with adding and using array values.

Fixed your query:

$image = '';
if ($_POST["image"] !== '')
      $image= ", image=".GetSQLValueString($_POST['image'], 'text');

$insertSQL = sprintf("UPDATE taxi SET name=%s, taxi_link=%s {$image} WHERE id=%s",
                   GetSQLValueString($_POST['name'], "text"),
                   GetSQLValueString($_POST['link'], "text"),
                   GetSQLValueString($_POST['id'], "int"));

3 Comments

sorry not an option here though
Check comment on your answer. It says what's wrong with your example.
thanks again for the help, it works if I leave the image field blank but when I select an image I get the error Too few arguments in taxi-edit.php on line 49 Query was empty

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.