Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This seems like a perfect time to use try/catch block to control flow execution. It also appears that you are missing a big part of the puzzle, which is to save the image path created during the image save to the user, within the user table.</p> <p>Following code is untested, but should put you on the right track:</p> <pre><code>Class User{ static public function insert($name, $image, $ext) { $conn = DB_config::get(); // This will force any PDO errors to throw an exception, so our following t/c block will work as expected // Note: This should be done in the DB_config::get method so all api calls to get will benefit from this attribute $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { $conn-&gt;beginTransaction(); $sth = $conn-&gt;prepare(" INSERT INTO users (name) values(:name);" ); $sth-&gt;execute(array(":name" =&gt; $name)); $imagePath = Image::saveImage($image, IMAGE_PATH . $conn-&gt;lastInsertId(). $ext)); // Image path is an key component of saving a user, so if not saved lets throw an exception so we don't commit the transaction if (false === $imagePath) { throw new Exception(sprintf('Invalid $imagePath: %s', $imagePath)); } $sth = $conn-&gt;prepare("UPDATE users SET image_path = :imagePath WHERE id = :userId LIMIT 1"); $sth-&gt;bindValue(':imagePath', $imagePath, PDO::PARAM_STR); $sth-&gt;bindValue(':userId', $conn-&gt;lastInsertId(), PDO::PARAM_INT); $sth-&gt;execute(); // If we made this far and no exception has been thrown, we can commit our transaction $conn-&gt;commit(); return $conn-&gt;lastInsertId(); } catch (Exception $e) { error_log(sprintf('Error saving user: %s', $e-&gt;getMessage())); $conn-&gt;rollback(); } return 0; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload