Jump to content

I keep getting "2"!


Snatch

Recommended Posts

Hi, i'm using the following two pages of code to allow an administrator to add new products to my database. It all seems to work apart from the drop down menu. It always inserts "2" into the type field, instead of either PC, Laptop, Peripheral or Software! I've tried lots of different things to no avail. Does anyone have any suggestions? I would be very grateful for any help! Thanks.

 

add_event.php

 

<?php
session_start(); // start the session
if(!isset($_SESSION['user'])){
    // check if the user is logged in
    // the user is not logged in
    header("Location: login.php"); // take him/her to the login page
} else {
    // the user is logged in
}
?>

<div id="content">
  <div id="adminarea">
    Add products using the form below: 
<br />
<br />
<br />
        <form id="form2" name="form2" method="post" enctype="multipart/form-data" action="add_event2.php">
         
              <table width="595" border="0" id="addtable" >
                <tr>
                  <td width="30%" class="mainsection"><div align="left">Product type: </div></td>
                  <td width="70%"><div align="left">
                    <select name="type" id="type">
                      <option value="PC" selected="selected">PC</option>
                      <option value="Laptop">Laptop</option>
                      <option value="Peripheral">Peripheral</option>
                      <option value="Software">Software</option>
                    </select>
                  </div></td>
                </tr>
                <tr>
                  <td class="mainsection" valign="top"><div align="left">Name:</div></td>
                  <td><div align="left">
                    <input name="name" type="text" id="name" size="50" maxlength="50" />
                  </div></td>
                </tr>
                <tr>
                  <td class="mainsection" valign="top" width="30%"><div align="left">Description:</div></td>
                  <td width="70%"><div align="left">
                    <textarea name="description" cols="38" rows="10" id="description"></textarea>
                  </div></td>
                </tr>
                <tr>
                  <td class="mainsection" width="30%"><div align="left">Quote no: </div></td>
                  <td width="70%"><div align="left">
                    <input name="quote_no" type="text" id="quote_no" size="50" maxlength="25" />
                  </div></td>
                </tr>
                <tr>
                  <td class="mainsection" width="30%"><div align="left">Price</div></td>
                  <td width="70%"><div align="left">
                    <input name="price" type="text" id="price" size="50" maxlength="25" />
                  </div></td>
                </tr>
			<tr>
                  <td height="31" class="mainsection"><div align="left">Image</div></td>
                  <td width="70%"><div align="left">
                    <input name="image_filename" type="file" id="image_filename" size="50"/>
                  </div></td>
                </tr>
                <tr>
                  <td class="mainsection"><div align="left"></div></td>
                  <td width="70%"><div align="left">
                    <input type="submit" name="Submit" value="Submit" />
                    <input type="reset" name="Submit2" value="Clear" />
                  </div></td>
                </tr>
              </table>
           
          </label>        
        </form>

<br/>Click <a href="admin/control_panel.php">here</a> to return to the admin area
   
  </div>
  </div>

 

 

add_event2.php

 

<?php
session_start(); // start the session
if(!isset($_SESSION['user'])){
    // check if the user is logged in
    // the user is not logged in
    header("Location: login.php"); // take him/her to the login page
} else {
    // the user is logged in
}
include "connection.php";
?>

<div id="content">
  <div id="adminarea">
   <?php
// Initialise an error string 
  $errorString = ""; 
  // Collect and validate the details 
  $type = $_POST["type"]; 
  $type = trim($type); 
  if (empty($type)) 
        $errorString .= 
          "<br>The type field cannot be blank."; 
  $name = $_POST["name"]; 
  $name = trim($name); 
  if (empty($name)) 
         $errorString = $errorString."<br>The name field cannot be blank.";
  $description = $_POST["description"]; 
  $description = trim($description); 
  if (empty($description)) 
         $errorString = $errorString."\n<br>The description field cannot be blank.";
  $quote_no = $_POST["quote_no"]; 
  $quote_no= trim($quote_no); 
  if (empty($quote_no)) 
        $errorString .= 
          "<br>The quote no. field cannot be blank."; 
  $price = $_POST["price"]; 
  $price = trim($price); 
  if (empty($price)) 
      $errorString  = $errorString."<br>Please supply a price."; 
   
   $image_tempname = $_FILES['image_filename'] ['name'];
   if (empty($image_tempname))
   	$errorString = $errorString."<br>Please supply an image"; 
   
      
    
  // check if there were any errors 
  if (!empty($errorString)) 
  {   
?>
<br/>
<br/>

<?php print "<span style=color:red;>$errorString</span><br/><br/>" ;?>
        <form id="form2" name="form2" method="post" enctype="multipart/form-data" action="addevent2.php">
         
              <table width="595" border="0" id="addtable" >
                <tr>
                  <td width="30%" class="mainsection"><div align="left">Type: </div></td>
<td width="70%"><div align="left">
                    <select name="type" id="type">
				  <option value="PC" selected="selected">PC</option>
                      <option value="Laptop">Laptop</option>
                      <option value="Peripheral">Peripheral</option>
                      <option value="Software">Software</option>
                    </select>
                  </div></td>
			  </tr>
                <tr>
                  <td class="mainsection" valign="top" width="30%"><div align="left">Name:</div></td>
                  <td width="70%"><div align="left">
                    <input name="name" id="name" value="<?php echo $name ?>" size="50" maxlength="50" />
                  </div></td>
                </tr>
                <tr>
                  <td class="mainsection" width="30%"><div align="left">Description:</div></td>
                  <td width="70%"><div align="left">
                    <textarea name="description" cols="38" rows="10" id="description" type="text" value="<?php echo $description ?>"></textarea>
                  </div></td>
                </tr>
                <tr>
                  <td class="mainsection" width="30%"><div align="left">Quote no: </div></td>
                  <td width="70%"><div align="left">
                    <input name="quote_no" type="text" id="quote_no" size="50" maxlength="25" value=" <?php echo $quote_no ?>" />
                  </div></td>
                </tr>
                <tr>
                  <td width="30%" class="mainsection"><div align="left">Price:</div></td>
                  <td width="70%"><div align="left">
                    <input name="price" type="text" id="price" size="50" maxlength="25" value=" <?php echo $price ?>"/>
                  </div></td>
                </tr>
			<tr>
                  <td height="31" class="mainsection"><div align="left">Image:</div></td>
                  <td width="70%"><div align="left">
                    <input name="image_filename" type="file" id="image_filename" size="50"/>
                  </div></td>
                </tr>
                <tr>
                  <td class="mainsection"><div align="left"></div></td>
                  <td width="70%"><div align="left">
                    <input type="submit" name="Submit" value="Submit" />
                    <input type="reset" name="Submit2" value="Clear" />
                  </div></td>
                </tr>
              </table>
           
          </label>        
        </form>



</div>
<?php 
            // There are errors.  Show them and exit. 
      exit;       
  } 
  
  //upload image and check for image type
//make sure to change your path to match your images directory
$ImageDir ="images/";
$ImageName = $ImageDir . $image_tempname;

if (move_uploaded_file($_FILES['image_filename']['tmp_name'], $ImageName)) {

  //get info about the image being uploaded
  list($width, $height, $type, $attr) = getimagesize($ImageName);

  switch ($type) {
    case 1:
      $ext = ".gif";
      break;
    case 2:
      $ext = ".jpg";
      break;
    case 3:
      $ext = ".png";
      break;
    default:
      echo "Sorry, but the file you uploaded was not a GIF, JPG, or " .
           "PNG file.<br>";
      echo "Please hit your browser's 'back' button and try again.";
  }

  
  //Else.. if we made it here, then the data is valid 
//Insert details - we do not insert customer id, because of AUTOINCREMENT 
  $query = "INSERT INTO products set ". 
            "type = '$type', " . 
            "name = '$name', " .
		"description = '$description', " .
		"quote_no  = '$quote_no', " .
            "price = '$price', " .
		"image = '$ImageName' " ; 
// Run the query on the guruticket table 
  if (!(@ mysql_query ($query, $conn))) 
     die("Error with Query1");    
  //find the ID created 
   $productID = mysql_insert_id(); 
   
     $newfilename = $ImageDir . $lastpicid . $ext;



}
   
   //Query the database to double check 
   $query = "SELECT * FROM products WHERE id = $productID"; 
    if (!($result = @ mysql_query ($query, $conn))) 
       die("Error with Query2"); 
    // There is only one matching row 
    $row = @ mysql_fetch_array($result); 
    echo "\n<h3>Product added:<font color=\"red\">" . $row["name"] . "</font></h3>\n"; 
    
?>
<p>
Click <a href="admin_area.php">here</a> to return to the admin area.
</p>
  </div>
  </div>

Link to comment
Share on other sites

I don't understand something. You are putting a string (value of $_POST['type'], like Periferial) into $type, then using a $type in an integer case comparison. I would expect you to get the default "Sorry" message instead of 2. Am I missing something?

Link to comment
Share on other sites

Sorry!

 

 

 

add_event.php

 

<form id="form2" name="form2" method="post" enctype="multipart/form-data" action="add_event2.php">
        
             <table width="595" border="0" id="addtable" >
               <tr>
                 <td width="30%" class="mainsection"><div align="left">Product type: </div></td>
                 <td width="70%"><div align="left">
                   <select name="type" id="type">
                     <option value="PC" selected="selected">PC</option>
                     <option value="Laptop">Laptop</option>
                     <option value="Peripheral">Peripheral</option>
                     <option value="Software">Software</option>
                   </select>

 

add_event2.php

 

// Collect and validate the details 
 $type = $_POST["type"]; 
 $type = trim($type); 
 if (empty($type)) 
       $errorString .= 
         "<br>The type field cannot be blank."; 


<form id="form2" name="form2" method="post" enctype="multipart/form-data" action="addevent2.php">
        
             <table width="595" border="0" id="addtable" >
               <tr>
                 <td width="30%" class="mainsection"><div align="left">Type: </div></td>
<td width="70%"><div align="left">
                   <select name="type" id="type">
				  <option value="PC" selected="selected">PC</option>
                     <option value="Laptop">Laptop</option>
                     <option value="Peripheral">Peripheral</option>
                     <option value="Software">Software</option>
                   </select>


 

Link to comment
Share on other sites

@lemmin: it's because he's assigning $type as one of the variables from getimagesize().

 

@snatch: this line is the offender:

 

list($width, $height, $type, $attr) = getimagesize($ImageName);

 

you're overriding the $type obtained from the $_POST data with the list() command.  change one or the other (probably easiest to change the variable used in list()), and you should start seeing the correct value being inserted.  keep in mind that you'll also need the change the switch() to reflect the new variable name.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.