I keep getting the following error message when executing this code:
Notice: Trying to get property of non-object in C:\wamp\www\HoneysProject\function.php on line 1178
Line 1178 is referring to the following line of code:
$number_photos = $photo_exists->num_rows ;
Not sure exactly why it is throwing this error because it's a duplicate of a line of code I've been using pretty frequently on this project. Doesn't seem to have any real influence on the code running to completion since I proceed to the successful output. Would Just like the error message to stop popping up.
The following is the source code in it's entirety:
function create_album()
{
try
{
if( isset( $_SESSION['session_id'] ) && $_SESSION['permissions'] == 0 )
{
if( isset( $_POST['album_name'] ) && ( file_exists( $_FILES['cover_photo']['tmp_name'] ) || is_uploaded_file($_FILES['cover_photo']['tmp_name'] ) ) )
{
$db = honneyconnect( ) ;
if( mysqli_connect_error() )
{
throw new Exception( "Could not connect to the database") ;
}
else
{
$unique = false ;
while( $unique == false )
{
$key = rand( ) ;
$query = "select * from albums where album_id = '".$key."'";
$album_exists = $db->query( $query ) ;
$number_albums = $album_exists->num_rows ;
if( $number_albums == 0 )
{
$unique = true ;
}
}
if( !mkdir( "c:\\wamp\\www\\honeysproject\\".$_POST['album_name'] ) )
{
throw new Exception( "Failed to create the album. Please try again." ) ;
}
else
{
$file_name = $_FILES["cover_photo"]["name"] ;
if( !move_uploaded_file($_FILES["cover_photo"]["tmp_name"], "C:/wamp/www/HoneysProject/".$_POST['album_name']."/" . $_FILES["cover_photo"]["name"]) )
{
throw new Exception( "There was a problem uploading the file" ) ;
}
else
{
$query = 'insert into albums values ("'.$key.'","'.$_POST['album_name'].'", "'.$_FILES['cover_photo']['name'].'")';
$album = $db->query( $query ) ;
if( !$album )
{
throw new Exception( "Failed to create the ".$_POST['album_name']." album. Please check your input and try again." ) ;
}
else
{
$unique = false ;
while( $unique == false )
{
$picture_key = rand();
$query = "select * from photos where photo_id = '".$picture_key."'" ;
$photo_exists = $db->query( $query ) ;
$number_photos = $photo_exists->num_rows ;
if( $number_photos == 0 )
{
$unique = true ;
}
}
$query = "insert into photos values ('".$key."', '".$picture_key."', '".$file_name."')" ;
$picture_query = $db->query( $query ) ;
if( !$picture_query )
{
throw new Exception( "Failed to add photo to the photos table. Please check your syntax and try again." ) ;
}
else
{
echo "<table><tr><td><img src='/HoneysProject/".$_POST['album_name']."/".$_FILES['cover_photo']['name']."'></td><td><a class ='button' href='/HoneysProject/uploadphotos.php?album_id=".$key."'>Upload Photos</a><br><a class='button' href='/HoneysProject/albumedit.php?album_id=".$key."'>Edit Album</a></td></tr></table>" ;
}
}
}
}
}
}
else
{
echo '<div class="data_entry">
<form id="new_album" method="post" action="createalbum.php" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="50000000" />
<table>
<tr><td>Album Name:</td><td><input type="text" size="10" name="album_name" /></td></tr>
<tr><td>Choose a Default Photo:</td><td><input type="file" name="cover_photo" id="photo" /></td></tr>
<tr><td><input type="submit" value="Submit Data" /></td></tr>
</table>
</form></div>' ;
}
}
}
catch( Exception $error )
{
echo "<div class='error'>".$error."</div>" ;
echo '<div class="data_entry">
<form id="new_album" method="post" action="createalbum.php" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="50000000" />
<table>
<tr><td>Album Name:</td><td><input type="text" size="10" name="album_name" value="'.$_POST['album_name'].'"/></td></tr>
<tr><td>Choose a Default Photo:</td><td><input type="file" name="cover_photo" id="photo" /></td></tr>
<tr><td><input type="submit" value="Submit Data" /></td></tr>
</table>
</form></div>' ;
}
}
The error is due to an unsuccessful query.
I wonder why you are manually generating the entities keys? Doesn't your DBMS support auto incremented keys or sequences?
Obviously the query failed and $photo_exists
is not an object it's either null
or false
. As per the documentation mysqli::query:
You should check for errors before trying to use an object returned by the mysqli
functions.
$photo_exists = $db->query($query) ;
if($number_photos) {
$number_photos = $photo_exists->num_rows ;
}
Or even:
if($photo_exists = $db->query($query)) {
$number_photos = $photo_exists->num_rows ;
}
$photo_exists is not an object and is most likely null. Double check where you initialize it to ensure the capitalization and spelling is correct.
Also you should add some debugging print statements to check the state of $photo_exists just before you try to use it, you will most likely be surprised that it doesn't contain what you want.
you can use var_dump($photo_exists) to try to help you output the object.