Trying to perform an update

I have a user table of which im performing an update query on and I want to make sure I got it set up correctly
Heres my logic…

  1. Check to see if a SESSION variable exists, if it does fill out a form usintg a SELECT statement
//query the database to fin the record for that user (SELECT)
  1. Then If I get a result from that query OR if the form has been submitted, a HTML form will be displayed (pre-filled with the user info)
  2. Then if ONLY the form is submitted (meaning someone tried to change a users info) an UPDATE will be performed
   if (($count==1) || ($_SERVER['REQUEST_METHOD'] == 'POST'))
		   try {
			//perform UPDATE on database

			catch(PDOException $e) {
	   	      //an update wasn't done
//query the database to fin the record for that user (SELECT)

//display form

But I think im over thinking it…
If a user logs in, a SESSION is set. So now if they try to edit there profile, the database will be queried 2 times (SELECT)
and the form will be displayed.
Then, once they make changes and submit the form, an UPDATE query will be performed on the database an then a SELECT query will be run and the form will be displayed again with the new data.

My problem is I think my logic may be flawed as I think I dont need to do two SELECT queries if they are identical.
I’ve seen this done in the past and was wondering how to set it up?


good catch… yes, doing two queries that are identical seems unnecessary

I only need to run 1 query once the page opens (SELECT), and 2 if the form is submitter (UPDATE then SELECT)
So as far as the logic goes, is this the way I do this?

if (isset($_SESSION['id']) OR ($_SERVER['REQUEST_METHOD'] == 'POST']))
      if ($_SERVER['REQUEST_METHOD'] == 'POST')
      //perform an UPDATE
      //can I use a try/catch block here

   //perform a SELECT
   //can I use a  try/catch block here as well?
   //display the form

Would this work?

what happened when you tested it? ™

it works, but just wanted to check with you if its ok

Nice,job keep it up and keep sharing…

I have a question about my SESSION variable.
So when a new user registers, the fill out there; name, email, and a password, then the user table INSERTs a new record.
Then when they go to edit there profile, they are taken to this page (where I print all SESSION variables,

the id one is the PK of the user table, email, and name is also saved from the record so I only have the three.
When I add a phone number, about, and an avatar, I get

But then I look at the database and

So the update worked, and the image was uploaded
But I am trying to figure out why,the avatar is not seen when the form has been updated.
In the first screenshot, you can see where the image is coming from, so I try to do …

<div class="fileinput fileinput-new" data-provides="fileinput">
              <div class="fileinput-new thumbnail" style="width: 223px; height: 250px; ">
                <img data-src="images/users/<?=(!isset($_SESSION['avatar'])) ? "default.jpg" : $_SESSION['avatar']?>" alt="...">
              <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 250px;"></div>
                <span class="btn btn-default btn-file"><span class="fileinput-new">Select image</span><span class="fileinput-exists">Change</span><input type="file" name="avatar"></span>
                <a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput">Remove</a>

Do you see the problem?

Moved to PHP forum.

can I use a try/catch block here

No. You shouldn’t use any try…catch blocks anywhere in this code.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.