Jump to content

Where do I find the class objects that can be used with an update command?


Conjurer

Recommended Posts

I am a bit rusty with mySQL and I tried to borrow some other query code and modify it into an update command...apparently I am trying to use an invalid property in my error checking.

 

Basically I just want to check and see if there were NO rows updated so I can let the user no that nothing was changed.  There are probably easier ways to do this?  :confused:

 

The page where I set the variable values is based on a form post.  I am not sure how to write my validation tests.  Right now it is running the update successfully but it is reporting back an error based on hitting the "else if" test where I am trying to see if no rows were updated. 

 

The error message is:

Encountered error: 8 in /home/omgma/public_html/member_community/mbr_profiles/mbr_profile_updt_post.php, line 85: Trying to get property of non-object

No fields were updated!

 

Error: 0

 

//create a SQL statement
	$updt_cmd = "UPDATE users t1, directory t2 " 
				."SET t1.email = '$email', "
				."t2.first_name='$first_name', "
				."t2.last_name='$last_name', "
				."t2.suffix='$suffix', "
				."t2.website_url='$website_url' "
				."WHERE t1.username ='$username' AND t2.directory_id = t1.directory_id";

	$conn = db_connect();

	$result = $conn->query($updt_cmd);
	if (!$result)
		{//could not execute query
		  throw new Exception('<h2>Could not execute member profile update!</h2><p>Error: '
								.mysqli_errno($conn)
								.mysqli_error($conn)
								.'</p>' );
		}
	else if ($result->num_rows==0)
		{// no rows so nothing was updated.  
		  throw new Exception('<h2>No fields were updated! </h2><p>Error: '
								.mysqli_errno($conn)
								.mysqli_error($conn)
								.'</p>' );
		}
	else
		{
		  $mbr_profile = $result->fetch_object();
		  print_r($mbr_profile);			 	
		  return $mbr_profile;
		}


 

Am I using the right functions here to do this and if so where can I find the class properties to use to test if nothing was updated?

 

Thanks

Link to comment
Share on other sites

Am I using the right functions here to do this and if so where can I find the class properties to use to test if nothing was updated?

 

db_connect() is not a native php function, where have you defined it?

Link to comment
Share on other sites

Sorry, I had db_connect defined in an include file:

function db_connect()
{
    
$conn = new mysqli("localhost", "xyz_xyzuser", "x213", "xyz_members");
     if (mysqli_connect_errno() > 0) 
  {
       $errmsg = "Failure - Could not connect to database with db_connect.<br />mysqli_connect_error message: ".mysqli_connect_error()."<br /><br />"; 
       throw (new Exception($errmsg));
     } 
     else
     {
//echo "Connection worked! <br>";
     return $conn;
     }	  
} 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.