Jump to content

[SOLVED] Switch Statement Issue


nayrufyoradin

Recommended Posts

Hello. I am having issues with a switch statement. I was wondering if you tell me if I am doing something wrong in my code.

 

It seems to be uploading everything to img_sketches and ignoring the other type cases.

 

<?php
$type = $_GET['type'];

if(empty($_POST)) {

// create variables for the form:
    $status = 'To add a new gallery item, fill out the form below. When you are finished, click the Add Image button once.';
    $title = '';
    $caption = '';
    $category = '';
} else {

	// change to easier to use variable names
        $title = $_POST['title'];
        $caption = $_POST['caption'];
        $category = $_POST['category'];
       
       
        $destination = '../gallery_images';
        $small_file_name = $_FILES['small']['name'];
        $small_tmp_name = $_FILES['small']['tmp_name'];
        $large_file_name = $_FILES['large']['name'];
        $large_tmp_name = $_FILES['large']['tmp_name'];
       
        $error_list = array();
       
   // Check to see if any are empty:
        if(empty($title)) {
                $error_list[] = 'You did not supply a Title';
        }

        if(empty($small_file_name) && ($category == 'image')) {
                $error_list[] = 'You did not supply a Small Image';
        }
       
        if(empty($large_file_name) && ($category == 'image')) {
                $error_list[] = 'You did not supply a Large Image';
        }
       
        if($large_file_name == $small_file_name) {
                $error_list[] = 'You can not have the same file for both the Small and Large images';
        }
       
       
        if(empty($error_list)) {
		// if no errors

		include '../../connect.php';
		// okay, so let's see what type of image they have
		switch($type) {
			case 'gallery':
				// gallery image, so we should direct them to the correct table
				$tableName = 'img_gallery';
			break;
			case 'sketches':
				// just a sketch, thats okay
				$tableName = 'img_sketches';
			break;
			case 'vector':
				// just a sketch, thats okay
				$tableName = 'img_vector';
			break;
			default:
				// and we'll make this our default case, since it isn't as high on the list
				$tableName = 'img_sketches';
			break;
		}

		// Start the sql statement, INSERT INTO `tablename` (tablename comes from the switch statement above)
		$sql = "INSERT INTO `".$tableName."` "; 

		// continue the sql statement, listing the column names
            $sql.= "(`title`, `caption`, `small_file_name`, `large_file_name`) ";

		// now, let's put the variables in there...
		// of course, I would recommend cleaning the variables first
		$sql.= "VALUES ('".$title."', '".$caption."', '".$small_file_name."', '".$large_file_name."')";

		// for debugging, un-comment this next line:
		// echo 'Current mysql statement: ',$sql;
               
		// Check to see if the file names are empty, if not move the files 
            if(!empty($small_file_name)) {
			move_uploaded_file($small_tmp_name, $destination.'/'.$small_file_name);
            }
            if(!empty($large_file_name)) {
			move_uploaded_file($large_tmp_name, $destination.'/'.$large_file_name);
		}


		// if there was a good query, and everything went okay
		if(mysql_query($sql)) {
			// let's go to a new location
			header('Location: index.php');
            } else {
			// otherwise, we need to show a message
			$status = 'Uh-oh, mysql error! '.mysql_error().'<br />Unable to add your images.';
            }
        } else {
		// create an error message, in a list format
            $status = '<ul>';
            foreach($error_list as $error_message) {
                $status .= "<li>$error_message</li>";
            }
            $status .= '</ul>';
        }
}

?>
                       
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xlmns="http://www.w3.org/1999/xhtml" xml:lang=en-US" lang="en-US">
<head>
<title>Add Image -- Administrative Area</title>
<meta http-equiv="Content-Type" content="text/html"; charset=iso-8859-1" />
</head>
<body>
        <h1>Administrative Area</h1>   
        <h2>Add Image</h2>
        <h3><?php echo $type; ?></h3>
        <h3><?php echo $tableName; ?>Blah</h3>
        <div><?php echo $status; ?></div>
        <form action="image_add.php" method="post" enctype="multipart/form-data">
                <dl>
                        <dt><label for="title">Title</label></dt>
                        <dd><input type="text" name="title" id="title" value="<?php echo $title; ?>" /></dd>
                       
                       
                        <dt><label for="caption">Caption</label></dt>
                        <dd><textarea name="caption" id="caption" rows="3" cols="35"><?php echo $caption; ?></textarea></dd>
                       
                        <dt><label for="small">Small Image (150x180)</label></dt>
                        <dd><input type="file" name="small" id="small" /></dd>
                       
                        <dt><label for="large">Large Image</label></dt>
                        <dd><input type="file" name="large" id="large" /></dd>
                       
                </dl>
               
                <div>
                        <input type="submit" name="submit" value="Add Image" />
                        <input type="reset" name="reset" value="Reset Form" />
                </div>
        </form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/149007-solved-switch-statement-issue/
Share on other sites

does the form submit to the same page ?  If so I dont see you setting the type anywhere in the form, so it will always be empty and hence go to the default switch statement.  where is $_GET getting it from ?

 

On a prior page it passes it through the URL

vector, images, or gallery

       switch($type) {
            case 'gallery':
               // gallery image, so we should direct them to the correct table
               $tableName = 'img_gallery';
            break;
            case 'sketches':
               // just a sketch, thats okay
               $tableName = 'img_sketches';
            break;
            case 'vector':
               // just a sketch, thats okay
               $tableName = 'img_vector';
            break;
            default: // wrong.
               // and we'll make this our default case, since it isn't as high on the list
               $tableName = 'img_sketches';
            break;

    // default: goes at the end mate.
         }
         

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.