Snatch Posted August 13, 2007 Share Posted August 13, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/64714-i-keep-getting-2/ Share on other sites More sharing options...
akitchin Posted August 13, 2007 Share Posted August 13, 2007 that is, flat-out, way too much code for most people to wade through. snippet your code to the relevant portions, and we'd be happy to assist. also, please use the code tags when posting code. Quote Link to comment https://forums.phpfreaks.com/topic/64714-i-keep-getting-2/#findComment-322723 Share on other sites More sharing options...
lemmin Posted August 13, 2007 Share Posted August 13, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/64714-i-keep-getting-2/#findComment-322728 Share on other sites More sharing options...
Snatch Posted August 13, 2007 Author Share Posted August 13, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/64714-i-keep-getting-2/#findComment-322729 Share on other sites More sharing options...
Snatch Posted August 13, 2007 Author Share Posted August 13, 2007 Lemmin, if $type is being used in an integer case comparison then it's unintentional. How would I correct this? Quote Link to comment https://forums.phpfreaks.com/topic/64714-i-keep-getting-2/#findComment-322742 Share on other sites More sharing options...
Edgewalker81 Posted August 13, 2007 Share Posted August 13, 2007 (nvm, misunderstood problem) Quote Link to comment https://forums.phpfreaks.com/topic/64714-i-keep-getting-2/#findComment-322745 Share on other sites More sharing options...
akitchin Posted August 13, 2007 Share Posted August 13, 2007 @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. Quote Link to comment https://forums.phpfreaks.com/topic/64714-i-keep-getting-2/#findComment-322748 Share on other sites More sharing options...
Snatch Posted August 13, 2007 Author Share Posted August 13, 2007 Thanks akitchin that's resolved it. I feel stupid I didn't notice that! Thank goodness there's people like you around! Quote Link to comment https://forums.phpfreaks.com/topic/64714-i-keep-getting-2/#findComment-322842 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.