jacko_162 Posted March 18, 2008 Share Posted March 18, 2008 Can nyone help me with a bit of code please? i want a form where i can input a product code and it jumps to that product code. for instance if i have a product ID of "43" and want to jump to edit it quickly i want to be able to input "43" into a form, click submit and it forward me to the following page: http://www.someaddress.com/edit.php?ID=43 but if no product code is found i want it to return to the previously browsed page. thank you for any help and bear with me a little cause i am still very new at all this (slow learner!) Jacko Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted March 18, 2008 Share Posted March 18, 2008 Well, i assume you're able to create the form which you will input the product ID and you're just not sure where to begin with edit.php ? If thats the case, then maybe this will get you started. It's by no means complete - but it should provide a rough framework for the page. <?php //include your database connection if(!isset($_GET['id']){ header("location:form.php"); } $id = (int) $_GET['id']; if(isset($_POST['submit']){ //update the database } $sql = "SELECT * FROM `tbl` WHERE `id`=$id"; $result = mysql_query($sql) or die(mysql_error()); $num = mysql_num_rows($result); $row = mysql_fetch_assoc($result); if($num == 0){//if no results header("location:form.php"); } //otherwise, show the update form ?> <form action="edit.php?id=<?php echo $id;?>" method="post"> //you'll need to set up your form inputs with the value being taken from the database. For example: <input type="text" value="<?php echo $row['field_name']; ?>" name="fieldname" /> //other fields <input type="submit" name="submit" value="Edit" /> </form> Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted March 18, 2008 Author Share Posted March 18, 2008 this is the code for edit.php <title>Edit A Product</title> <script src="nicEdit.js" type="text/javascript"></script> <script type="text/javascript"> bkLib.onDomLoaded(nicEditors.allTextAreas); </script> <? include("includes/header.php") ?> <table width="100%" border="0" cellspacing="0" cellpadding="5" align="center" bgcolor="FFFFFF" bordercolor="#999999"> <tr> <td> <?php if (isset($submit)) { $name = $_POST['name']; $make = $_POST['make']; $catagory = $_POST['catagory']; $price = $_POST['price']; $description = $_POST['description']; $sold = $_POST['sold']; $colour = $_POST['colour']; $cotw = $_POST['cotw']; $sql = "UPDATE products SET name='$name', make='$make', catagory='$catagory', price='$price', description='$description', sold='$sold', colour='$colour', cotw='$cotw' WHERE ID='$ID'"; $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error()); $upload_dir = "../images/products/"; $insert_id = $ID; if (!empty($imageOne)) { // we have an image in image one check we have a valid type (jpeg only ?) if ($_FILES['imageOne']['type'] != "image/jpeg" && $_FILES['imageOne']['type'] != "image/pjpeg") { die("Image type is not allowed for Image One. Type found : " . $_FILES['imageOne']['type']); } else { // valid image make up a new filename $new_image_name = $insert_id . "one.jpg"; if (!copy($_FILES['imageOne']['tmp_name'], $upload_dir . $new_image_name)) { // file uploaded add to table row die("Unable to upload Image One"); } } } if (!empty($imageTwo)) { // we have an image in image one check we have a valid type (jpeg only ?) if ($_FILES['imageTwo']['type'] != "image/jpeg" && $_FILES['imageTwo']['type'] != "image/pjpeg") { die("Image type is not allowed for Image Two. Type found : " . $_FILES['imageTwo']['type']); } else { // valid image make up a new filename $new_image_name = $insert_id . "two.jpg"; if (!copy($_FILES['imageTwo']['tmp_name'], $upload_dir . $new_image_name)) { die("Unable to upload Image Two"); } } } if (!empty($imageThree)) { // we have an image in image one check we have a valid type (jpeg only ?) if ($_FILES['imageThree']['type'] != "image/jpeg" && $_FILES['imageThree']['type'] != "image/pjpeg") { die("Image type is not allowed for Image Three. Type found : " . $_FILES['imageThree']['type']); } else { // valid image make up a new filename $new_image_name = $insert_id . "three.jpg"; if (!copy($_FILES['imageThree']['tmp_name'], $upload_dir . $new_image_name)) { die("Unable to upload Image Three"); } } } if (!empty($imageFour)) { // we have an image in image one check we have a valid type (jpeg only ?) if ($_FILES['imageFour']['type'] != "image/jpeg" && $_FILES['imageFour']['type'] != "image/pjpeg") { die("Image type is not allowed for Image Four. Type found : " . $_FILES['imageFour']['type']); } else { // valid image make up a new filename $new_image_name = $insert_id . "four.jpg"; if (!copy($_FILES['imageFour']['tmp_name'], $upload_dir . $new_image_name)) { die("Unable to upload Image Four"); } } } if (!empty($imageFive)) { // we have an image in image one check we have a valid type (jpeg only ?) if ($_FILES['imageFive']['type'] != "image/jpeg" && $_FILES['imageFive']['type'] != "image/pjpeg") { die("Image type is not allowed for Image Five. Type found : " . $_FILES['imageFive']['type']); } else { // valid image make up a new filename $new_image_name = $insert_id . "five.jpg"; if (!copy($_FILES['imageFive']['tmp_name'], $upload_dir . $new_image_name)) { die("Unable to upload Image Five"); } } } if (!empty($imageSix)) { // we have an image in image one check we have a valid type (jpeg only ?) if ($_FILES['imageSix']['type'] != "image/jpeg" && $_FILES['imageSix']['type'] != "image/pjpeg") { die("Image type is not allowed for Image Six. Type found : " . $_FILES['imageSix']['type']); } else { // valid image make up a new filename $new_image_name = $insert_id . "six.jpg"; if (!copy($_FILES['imageSix']['tmp_name'], $upload_dir . $new_image_name)) { die("Unable to upload Image Six"); } } } if (!empty($imageThumb)) { // we have an image in image one check we have a valid type (jpeg only ?) if ($_FILES['imageThumb']['type'] != "image/jpeg" && $_FILES['imageThumb']['type'] != "image/pjpeg") { die("Image type is not allowed for Image Thumb. Type found : " . $_FILES['imageThumb']['type']); } else { // valid image make up a new filename $new_image_name = $insert_id . "thumb.jpg"; if (!copy($_FILES['imageThumb']['tmp_name'], $upload_dir . $new_image_name)) { die("Unable to upload Image Thumb"); } } } // check if we need to delete any images if ($imageOneDelete == "yes") { // delete image one unlink($upload_dir . $insert_id . "one.jpg"); } if ($imageTwoDelete == "yes") { // delete image two unlink($upload_dir . $insert_id . "two.jpg"); } if ($imageThreeDelete == "yes") { // delete image three unlink($upload_dir . $insert_id . "three.jpg"); } if ($imageFourDelete == "yes") { // delete image three unlink($upload_dir . $insert_id . "four.jpg"); } if ($imageFiveDelete == "yes") { // delete image three unlink($upload_dir . $insert_id . "five.jpg"); } if ($imageSixDelete == "yes") { // delete image three unlink($upload_dir . $insert_id . "six.jpg"); } if ($imageThumbDelete == "yes") { // delete image three unlink($upload_dir . $insert_id . "thumb.jpg"); } echo "<BR><table width=700 border=0 align=center cellpadding=0 cellspacing=0> <tr class=table3> <td><table width=100% border=0 cellspacing=1 cellpadding=3 align=center> <tr class=table1 align=center> <td><b>Database Updated.</b></td> </tr> </table> </td> </tr> </table>"; } else { $sql = "SELECT * FROM $table1 WHERE ID='$ID'"; $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error()); $result = mysql_fetch_array($query); $catagory = stripslashes($result["catagory"]); $make = stripslashes($result["make"]); $name = stripslashes($result["name"]); $price = stripslashes($result["price"]); $description = stripslashes($result["description"]); $colour = stripslashes($result["colour"]); $sold = stripslashes($result["sold"]); $cotw = stripslashes($result["cotw"]); // check what images exist $images_dir = "../images/products/"; // check for image one if (file_exists($images_dir . $ID . "one.jpg")) { $imageOneExists = 1; } // check for image two if (file_exists($images_dir . $ID . "two.jpg")) { $imageTwoExists = 1; } // check for image three if (file_exists($images_dir . $ID . "three.jpg")) { $imageThreeExists = 1; } // check for image three if (file_exists($images_dir . $ID . "four.jpg")) { $imageFourExists = 1; } // check for image three if (file_exists($images_dir . $ID . "five.jpg")) { $imageFiveExists = 1; } // check for image three if (file_exists($images_dir . $ID . "six.jpg")) { $imageSixExists = 1; } // check for image three if (file_exists($images_dir . $ID . "thumb.jpg")) { $imageThumbExists = 1; } ?> <FORM name=myform method=post action="<? echo $PHP_SELF ?>" enctype="multipart/form-data"> <table wIDth="100%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr class=table3> <td><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" background="images/subbar_bg.gif"> <tr background="images/subbar_bg.gif"> <td width="4" height="52"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b><font size="1"><b><font size="2" face="Verdana, Arial, Helvetica, sans-serif" color="#3399FF"><img src="images/subbar_left.gif" width="4" height="52" /></font></b></font></b></font></td> <td height="52" align="left" valign="middle" background="images/subbar_bg.gif"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="20%"><img src="images/icons/add_product_logo.gif" width="173" height="39" /></td> <td width="80%"><div align="right"><a href="add.php" target="_self"><img src="images/icons/add.gif" alt="Add a product" width="38" height="38" border="0" /></a><a href="help.php" target="_self"><img src="images/icons/help.gif" width="38" height="38" border="0" /></a></div></td> </tr> </table></td> <td width="4" height="52"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b><font size="1"><b><font size="2" face="Verdana, Arial, Helvetica, sans-serif" color="#3399FF"><img src="images/subbar_right.gif" alt="" /></font></b></font></b></font></td> </tr> </table> <br> <table width="98%" border="0" align="center" cellpadding="3" cellspacing="1"> <tr align="left" valign="top"> <td width="20%"><table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/top_left_corner.gif" width="4" height="4" /></td> <td height="4" background="images/top_menu.gif" bgcolor="#f2f3f5"><img src="images/top_menu.gif" width="1" height="4" /></td> <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/top_right_corner.gif" width="4" height="4" /></td> </tr> <tr> <td width="4" background="images/left_menu.gif" bgcolor="#f2f3f5"><img src="images/left_menu.gif" width="4" height="1" /></td> <td bgcolor="#f2f3f5"><? include("includes/menu.php") ?></td> <td width="4" background="images/right_menu.gif" bgcolor="#f2f3f5"><img src="images/right_menu.gif" width="4" height="1" /></td> </tr> <tr> <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/bottom_left_corner.gif" width="4" height="4" /></td> <td height="4" background="images/bottom_menu.gif" bgcolor="#f2f3f5"><img src="images/bottom_menu.gif" width="1" height="4" /></td> <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/bottom_right_corner.gif" width="4" height="4" /></td> </tr> </table></td> <td width="80%"><table width="75%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/top_left_corner.gif" width="4" height="4" /></td> <td height="4" background="images/top_menu.gif" bgcolor="#f2f3f5"><img src="images/top_menu.gif" width="1" height="4" /></td> <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/top_right_corner.gif" width="4" height="4" /></td> </tr> <tr> <td width="4" background="images/left_menu.gif" bgcolor="#f2f3f5"><img src="images/left_menu.gif" width="4" height="1" /></td> <td bgcolor="#f2f3f5"><table wIDth="98%" border="0" cellspacing="1" cellpadding="3" align="center"> <tr> <td width="20%">Catagory:</td> <td width="28%"> <select name="catagory" size="1"> <option value='<?php echo $catagory; ?>'> <?php echo $catagory; ?> </option> <option value=" ">-----------------</option> <? // Query to pull information from the "catagory" Database $result = mysql_query("select * from $table13 order by id DESC"); while ($row = mysql_fetch_object($result)){ ?> <option value='<?php echo $row->name; ?>'> <?php echo $row->name; ?> <?}?> </option> </select> </td> <td width="13%">Manufacturer:</td> <td width="39%"> <select name="make" size="1"> <option value='<?php echo $make; ?>'> <?php echo $make; ?> </option> <option value=" ">-----------------</option> <? // Query to pull information from the "manufacturer" Database $result = mysql_query("select * from $table2 order by id DESC"); while ($row = mysql_fetch_object($result)){ ?> <option value='<?php echo $row->make; ?>'> <?php echo $row->make; ?> <?}?> </option> </select> </td> </tr> <tr> <td valign="top">Product Name:</td> <td colspan="3"> <input type="Text" name="name" value="<?php echo $name ?>" size="40"> </td> </tr> <tr class=table2> <td wIDth="20%" valign="top">Description:</td> <td colspan="3"> <textarea name="description" cols="62" rows="8"><?php echo $description ?></textarea> </td> </tr> <tr> <td>Price:</td> <td><input name="price" type="Text" id="price" value="<?php echo $price ?>" size="20"></td> <td>Colour:</td> <td> <input name="colour" type="Text" id="colour" value="<?php echo $colour ?>" size="25"> </td> </tr> <tr> <td>Product of the week:</td> <td><select name="cotw" size="1"> <option value='<?php echo $cotw ?>'> <?php echo $cotw ?> </option> <option value=" ">-----------------</option> <? // Query to pull information from the "sold" Database $result = mysql_query("select * from sold order by id DESC"); while ($row = mysql_fetch_object($result)){ ?> <option value='<?php echo $row->sold; ?>'> <?php echo $row->sold; ?> <?}?> </option> </select> </td> <td>Sold:</td> <td> <select name="sold" size="1" id="select"> <option value='<?php echo $sold ?>'> <?php echo $sold ?> </option> <option value=" ">-----------------</option> <? // Query to pull information from the "sold" Database $result = mysql_query("select * from sold order by id DESC"); while ($row = mysql_fetch_object($result)){ ?> <option value='<?php echo $row->sold; ?>'> <?php echo $row->sold; ?> <?}?> </option> </select> </td> </tr> <tr class=table2> <td valign=top>IMAGES:<br> <font size="1">Please select the images (jpeg only):</font></td> <td colspan="3"> 1: <? if ($imageOneExists) { ?> Replace : <input name="imageOne" type="file" class="bginput" id="imageOne"> Delete : <input type="checkbox" name="imageOneDelete" value="yes"> <a target="_new" href="../images/products/<? echo $ID ?>one.jpg">View</a> <? } else { ?> Upload : <input name="imageOne" type="file" class="bginput" id="imageOne"> <? } ?> <br> 2: <? if ($imageTwoExists) { ?> Replace : <input name="imageTwo" type="file" class="bginput" id="imageTwo"> Delete : <input type="checkbox" name="imageTwoDelete" value="yes"> <a target="_new" href="../images/products/<? echo $ID ?>two.jpg">View</a> <? } else { ?> Upload : <input name="imageTwo" type="file" class="bginput" id="imageTwo"> <? } ?> <br> 3: <? if ($imageThreeExists) { ?> Replace : <input name="imageThree" type="file" class="bginput" id="imageThree"> Delete : <input type="checkbox" name="imageThreeDelete" value="yes"> <a target="_new" href="../images/products/<? echo $ID ?>three.jpg">View</a> <? } else { ?> Upload : <input name="imageThree" type="file" class="bginput" id="imageThree"> <? } ?> <br> 4: <? if ($imageFourExists) { ?> Replace : <input name="imageFour" type="file" class="bginput" id="imageFour"> Delete : <input type="checkbox" name="imageFourDelete" value="yes"> <a target="_new" href="../images/products/<? echo $ID ?>four.jpg">View</a> <? } else { ?> Upload : <input name="imageFour" type="file" class="bginput" id="imageFour"> <? } ?> <br> 5: <? if ($imageFiveExists) { ?> Replace : <input name="imageFive" type="file" class="bginput" id="imageFive"> Delete : <input type="checkbox" name="imageFiveDelete" value="yes"> <a target="_new" href="../images/products/<? echo $ID ?>five.jpg">View</a> <? } else { ?> Upload : <input name="imageFive" type="file" class="bginput" id="imageFive"> <? } ?> <br> 6: <? if ($imageSixExists) { ?> Replace : <input name="imageSix" type="file" class="bginput" id="imageSix"> Delete : <input type="checkbox" name="imageSixDelete" value="yes"> <a target="_new" href="../images/products/<? echo $ID ?>six.jpg">View</a> <? } else { ?> Upload : <input name="imageSix" type="file" class="bginput" id="imageSix"> <? } ?> <br> 7: <? if ($imageThumbExists) { ?> Replace : <input name="imageThumb" type="file" class="bginput" id="imageThumb"> Delete : <input type="checkbox" name="imageThumbDelete" value="yes"> <a target="_new" href="../images/products/<? echo $ID ?>thumb.jpg">View</a> <? } else { ?> Upload : <input name="imageThumb" type="file" class="bginput" id="imageThumb"> <? } ?> - Thumbnail Image</td> </tr> <!-- end of new code --> <tr class=table1> <td colspan="4" align="center" valign="top"> <input type="image" src="../images/submit.gif" name="submit" value="Done!" class="bginput"> <input type="hidden" name="ID" value="<?php echo $ID; ?>"> <input type="image" src="../images/reset.gif" name="reset" value="Reset" class="bginput"> </td> </tr> </table></td> <td width="4" background="images/right_menu.gif" bgcolor="#f2f3f5"><img src="images/right_menu.gif" width="4" height="1" /></td> </tr> <tr> <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/bottom_left_corner.gif" width="4" height="4" /></td> <td height="4" background="images/bottom_menu.gif" bgcolor="#f2f3f5"><img src="images/bottom_menu.gif" width="1" height="4" /></td> <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/bottom_right_corner.gif" width="4" height="4" /></td> </tr> </table></td> </tr> </table> </td> </tr> </table> </FORM><?php } ?> </td> </tr> </table> <? include("includes/footer.php") ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted March 18, 2008 Share Posted March 18, 2008 Right. Well perhaps stating that you had a large amount of code already written would have been a good idea. So, what exactly is the problem with the above code? What does it do? What doesn't it do? Can you narrow down the bit that doesn't work? Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted March 18, 2008 Author Share Posted March 18, 2008 sorry, right the edit.php code works fine, and i use it to edit products, for example; http://www.someaddress.com/edit.php?ID=43 so all works great. what i want on the index.php page is a faster way to search for products in the database by using product "ID". so if i want to jump to product ID 43 i type it in and click submit, then this takes me to edit.php?ID=43, instead of trawling through lists of hundreds of products... can this be done using a simple form on my index.php? again sorry if i didnt explain properly. Quote Link to comment 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.