Empty Array On Submit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jzmwebdevelopment
Forum Commoner
Posts: 32
Joined: Mon Nov 01, 2010 1:45 pm

Empty Array On Submit

Post by jzmwebdevelopment »

Hello,

I am getting my drop down menu data from an array from the database. Were I am stuck is I am not sure how to link the "text" from the drop down menu into the query. When I click delete it should delete the image row from the database. Any help on how to delete the file will be very welcome.

Jess

Code: Select all

<?php
include('includes/session.php');

include('includes/header.php');

include('includes/class/class.form.php');

include('includes/class/class.image.php');

include('includes/class/class.imageManager.php');



$formImageDelete = new Form('deleteimage.php', 'Delete','return CheckDelete();', '');

$imImageManager = new imageManager();

$aImageObjects = $imImageManager->getallImages();



$aImageName = array();

for($iCount=0; $iCount<count($aImageObjects);$iCount++){
    $imImage = $aImageObjects[$iCount];
    $aImageName[] = $imImage->getImageName();
}


if(isset($_POST["submit"])){
         
    $formImageDelete->setStickyData($_POST);
    
    
   $formImageDelete->checkNotEmpty("DeleteImage");

    
    
    if($formImageDelete->getValid() == true){
	
	$removeImage = new Image();
		
	$removeImage->removeImage($_POST["DeleteImage"]);
	
	$Message = "Image Deleted";
	
    
    }else{
	
	$Message = "Error";
    }
    

}


$formImageDelete->openFieldset();
$formImageDelete->makeDropDownList("Delete","DeleteImage",$aImageName);
$formImageDelete->makeSubmitButton("submit","Delete");
$formImageDelete->closeFieldset();

$newNavigation = new Navigation();

?>

    <?php echo $newNavigation->mainMenu();?>
    
    
       	<h1  class="Heading">Delete Image</h1>
	
	<?php echo $Message; ?>
        
        <?php echo $formImageDelete->getHTML();?>
        
    
<?php include('includes/footer.php') ?>
Image Class

Code: Select all

<?php

require_once('../includes/database.php');

class Image{
    
    private $iID;
    
    private $sImageName;
    
    private $sImagePath;
    
    public function loadImage($iID){
        
        global $database;
        
        $Query = "SELECT * FROM Gallery WHERE id =" .$iID;
        
        $resResult = $database->query($Query);
    
        //Fetch The Row
        
        $aImage = $database->fetch_array($resResult);
        
        $this->iID = $aImage["id"];
        $this->sImageName = $aImage["imageName"];
        $this->sImagePath = $aImage["imagePath"]; // Was get maybe typo
        
        $this->bExisting = true;
    
    }
    
    public function saveImage(){
        
        global $database;
        
        if($this->bExisting == false){
            
            $Query = "INSERT INTO Gallery(id,imageName,imagePath) VALUES ('".$this->iID."', '".$this->sImageName."','".$this->sImagePath."')";
            
            $bResult = $database->query($Query);
            
            if($bResult){
                
                $this->iID = $database->get_last_insert_id();
                
                $this->bExisting = true;
                
            }
            else{
                
                die("Save Failed");
            }
            
        }
    }
    
    public function removeImage(){
    
         global $database;
        
        $Query = "DELETE FROM Gallery WHERE id =".$this->iID;
        
        $resResult = $database->query($Query);
    
    
        //Fetch The Row
        
        $aImage = $database->fetch_array($resResult);
        
        $this->iID = $aImage["id"];
        $this->sImageName = $aImage["imageName"];
        $this->sImagePath = $aImage["imagePath"];    
    }
        
    
    
                // Get Functions
        
    public function getImageID(){
            return $this->iID;
    }    
        
    public function getImageName(){
            return $this->sImageName;
    }
    
    public function getImagePath(){
            return $this->sImagePath;
    }
        
    // Set Functions
    
    public function setImageID($iID){
        
        global $database;
        
        $iID = $database->escape_value($iID);
        
        $this->iID = $iID;
    }    
        
      
    public function setImageName($sImageName){
        
        global $database;
        
        $sImageName = $database->escape_value($sImageName);
        
        $this->sImageName = $sImageName;
    }   
        
    public function setImagePath($sImagePath){
        
        global $database;
        
        $sImagePath = $database->escape_value($sImagePath);
        
        $this->sImagePath = $sImagePath;
    }
    
    
}


//Testing

//Save

//$newImageUpload = new Image();
//$newImageUpload->setImageID(8);
//$newImageUpload->setImageName("Image Class Works");
//$newImageUpload->setImagePath("Image Path Success");
//$newImageUpload->saveImage();



?>


User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Empty Array On Submit

Post by Darhazer »

Code: Select all

$removeImage = new Image();
                
        $removeImage->removeImage($_POST["DeleteImage"]);
Has to be:

Code: Select all

$removeImage = new Image();
$removeImage->setImageID($_POST["DeleteImage"]);
$removeImage->removeImage();
jzmwebdevelopment
Forum Commoner
Posts: 32
Joined: Mon Nov 01, 2010 1:45 pm

Re: Empty Array On Submit

Post by jzmwebdevelopment »

Thanks, That looks like the image I have in my head just could not get it out.

I now have the following:

Fatal error: Call to a member function fetch_array() on a non-object in /home/devnoo/public_html/Nyken/includes/database.php on line 40

Is there an object that I have forgotten to declare?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Empty Array On Submit

Post by Darhazer »

jzmwebdevelopment wrote:Thanks, That looks like the image I have in my head just could not get it out.

I now have the following:

Fatal error: Call to a member function fetch_array() on a non-object in /home/devnoo/public_html/Nyken/includes/database.php on line 40

Is there an object that I have forgotten to declare?
Well, it is (as the error shows) in database.php and we have no the code :mrgreen:
jzmwebdevelopment
Forum Commoner
Posts: 32
Joined: Mon Nov 01, 2010 1:45 pm

Re: Empty Array On Submit

Post by jzmwebdevelopment »

:)

Here is is but the fetch array is working on the front end of the site:

Code: Select all


<?php
//subcontracor
class MySQLiPluggin{
	
	private $sqliConnection;
	private $Query; //STRING
	private $getResult;
	
	//the constructor: creat connection to the database
	public function __construct(){
	
		//1: Make connection
		$this->sqliConnection = new mysqli("localhost",","",""; // Removed
		
		if ($this->sqliConnection->connect_error) {
			die("Connection Error " . $this->sqliConnection->connect_error);
		}
	
	}
	
	//call this method to execute a query
	//this method will return a result set to be used in the fetch_array method
	public function query($Query){
	
		$getResult = $this->sqliConnection->query($Query);
		
		if (!$getResult) {
			die( "!! WARNING: Database query failed " . $Query);
		}

		return $getResult;
		
	}
	
	//call this to fetch a record from a result set as an associtive array
	//you must pass in the returned result set of the execute_query method
	//databse neutral fetch
	
	public function fetch_array($getResult){
		return $getResult->fetch_array(MYSQLI_ASSOC);
	}
	
	// returns a auto generated ID used in the last query
	public function get_last_insert_id(){
		return $this->sqliConnection->insert_id;
	}
	
	//call this method to close the connection opened in the contructor
	public function close_connection(){
		$this->sqliConnection->close();	
	}
	
	//method for filtering input and output
	public function escape_value( $value ) {

		$magic_quotes_active = get_magic_quotes_gpc();
		$new_enough_php = function_exists( "mysqli_real_escape_string" ); // i.e. PHP >= v4.3.0

		if( $new_enough_php ) { // PHP v4.3.0 or higher
			// undo any magic quote effects so mysql_real_escape_string can do the work
		if( $magic_quotes_active ) { $value = stripslashes( $value ); }
			$value = $this->sqliConnection->real_escape_string( $value );
			} else { // before PHP v4.3.0
			// if magic quotes aren't already on then add slashes manually
			if( !$magic_quotes_active ) { $value = addslashes( $value ); }
				// if magic quotes are active, then the slashes already exist
		}
		return $value;
	}
	

}
//instantiate a database object
$database = new MySQLiPluggin
?>


User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Empty Array On Submit

Post by Darhazer »

10x, now I see the problem. In removeImage() function

Code: Select all

 //Fetch The Row
        
        $aImage = $database->fetch_array($resResult);
        
        $this->iID = $aImage["id"];
        $this->sImageName = $aImage["imageName"];
        $this->sImagePath = $aImage["imagePath"]; 
Since $resResult is a result from DELETE query and not from SELECT, fetch_array can't be used
Additionally, it is pointless to try to set the data for a deleted image. This code should be removed or maybe just to set null values to those variables.
jzmwebdevelopment
Forum Commoner
Posts: 32
Joined: Mon Nov 01, 2010 1:45 pm

Re: Empty Array On Submit

Post by jzmwebdevelopment »

Thanks,

Would I just remove that row?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Empty Array On Submit

Post by Darhazer »

jzmwebdevelopment wrote:Thanks,

Would I just remove that row?
I think so
jzmwebdevelopment
Forum Commoner
Posts: 32
Joined: Mon Nov 01, 2010 1:45 pm

Re: Empty Array On Submit

Post by jzmwebdevelopment »

I had to comment out that section and just have the query because otherwise what will $aImage use.

When I click submit it shows the deleted image message but its still in the database weird.

Jess
Post Reply