views:

726

answers:

1

I have a form with two fields with the name attribute of 'photo_title' and 'photographer_name', and a hidden field named 'photo_id'. When the user pushes the submit button, i want it to update two separate tables in the database. I can get it to update a single table, but as soon as I try to leftjoin the second table, it doesn't like it.

I think there may be something wrong with my query string or the binding. How can I update two separate values in two separate tables in my Mysql database while still using prepared statements?

Here's the PHP:

if (array_key_exists('update', $_POST)) { 

$sql = 'UPDATE photos SET photos.photo_title = ?, photographers.photographer_name = ?
    LEFT JOIN photographers ON photos.photographer_id = photographers.photographer_id
    WHERE photo_id = ?';

$stmt = $conn->stmt_init();
if ($stmt->prepare($sql)) { 
    $stmt->bind_param('ssi', $_POST['photo_title'], $_POST['photographer_name'], $_POST['photo_id']);  
    $done = $stmt->execute();
    }
}

Here's the form:

<form id="form1" name="form1" method="post" action="">
   <input name="photo_title" type="text" value=""/>
   <textarea name="photographer_name"></textarea>

   <input type="submit" name="update" value="Update entry" />
   <input name="photo_id" type="hidden" value="<?php echo $photo_id ?>"/>
</form>
A: 

Here's an answer so folks who read this question see it, instead of finding it in your comment above. I'll mark this CW so I don't get any points for it.

UPDATE photos LEFT JOIN photographers 
  ON photos.photographer_id = photographers.photographer_id 
SET photos.photo_title = ?, photographers.photographer_name = ? 
WHERE photos.photo_id = ?

FWIW, the documentation for MySQL's UPDATE syntax is illustrative.

Bill Karwin