PHP - Need A Php Code To Delete Selected Items Of Database Items Using Checkbox
I have a webpage where all the database items are displyes in a table format.The table also has a check box.Upon clicking the delete button i need to delete all the items whish has the checkbox checked.
How will i do that Similar TutorialsI've been playing around with this code from http://v3.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cart Objective: I'm trying to make the scripts work but without pulling the data from MySQL Database. Instead, I created an array and inserted some info there and trying to replicate the same results but getting confused. For example, on the Index.php file, I made the following changes: <?php // Start the session session_start(); // Include functions require_once('inc/functions.inc.php'); // Products multidimensional Array $products = array("book" => array( 'id' => '1', 'title' => 'Learn Your ABC\'s', 'author' => 'John Doe', 'price' => '14.95'), "video" => array('id' => '2', 'title' => 'Visual Guide to learn', 'author' => 'Adam Smith', 'price' => '21.38'), "puzzle" => array('id' => '3', 'title' => 'Can You Solve?', 'author' => 'Sara Brown', 'price' => '9.41'), "exam" => array('id' => '4', 'title' => 'Test Your Knowledge', 'author' => 'Kim Carver', 'price' => '11.15')); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>PHP Shopping Cart Demo &#0183; Bookshop</title> <link rel="stylesheet" href="css/styles.css" /> </head> <body> <div id="shoppingcart"> <h1>Your Shopping Cart</h1> <?php echo writeShoppingCart(); ?> </div> <div id="booklist"> <h1>Books In Our Store</h1> <?php /* $sql = 'SELECT * FROM books ORDER BY id'; $result = $db->query($sql); $output[] = '<ul>'; while ($row = $result->fetch()) { $output[] = '<li>"'.$row['title'].'" by '.$row['author'].': $'.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>'; } $output[] = '</ul>'; echo join('',$output); */ ?> <?php $output[] = '<ul>'; foreach ($products as $product => $product_item) { // $flavor is the key and $piece is the value foreach ($product_item as $item => $piece) { if ($item == "id") { # Conditional logic defined to map specific keys to specified name value $row['id'] = $piece; // echo "The id is ". $id."<br />"; // set just to confirm value correct for variable } elseif ($item == "title") { # Conditional logic defined to map 1st key to price value $row['title'] = $piece; // echo "The title is ". $title."<br />"; // set just to confirm value correct for variable } elseif ($item == "author") { # Conditional logic defined to map 1st key to price value $row['author'] = $piece; // echo "The author is ". $author."<br />"; // set just to confirm value correct for variable } elseif ($item == "price") { # Conditional logic defined to map 1st key to shipping value $row['price'] = $piece; // echo "Price: ". $price."<br /><br />"; // set just to confirm value correct for variable } $output[] = '<li>"'.$row['title'].'" by '.$row['author'].': $'.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>'; $output[] = '</ul>'; echo join('',$output); } } ?> </div> </body> </html> And here's the original index.php before my edits which show the sql query to the db: <?php // Include MySQL class require_once('inc/mysql.class.php'); // Include database connection require_once('inc/global.inc.php'); // Include functions require_once('inc/functions.inc.php'); // Start the session session_start(); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>PHP Shopping Cart Demo &#0183; Bookshop</title> <link rel="stylesheet" href="css/styles.css" /> </head> <body> <div id="shoppingcart"> <h1>Your Shopping Cart</h1> <?php echo writeShoppingCart(); ?> </div> <div id="booklist"> <h1>Books In Our Store</h1> <?php $sql = 'SELECT * FROM books ORDER BY id'; $result = $db->query($sql); $output[] = '<ul>'; while ($row = $result->fetch()) { $output[] = '<li>"'.$row['title'].'" by '.$row['author'].': £'.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>'; } $output[] = '</ul>'; echo join('',$output); ?> </div> </body> </html> Also, I included the zipped file of the entire script setup. Again, I'm trying to figure out how to make the cart work with using an array for the product instead of MySQL database. My goal is to remove ALL SQL statements from the scripts but stuck with the looping portion to display the title, author, and price from the array as shown above. Hi guys! I'm trying to make a <select> button for car positions which displays a set of numbers like 1-5 or 1-10 so on.. I need to remove the numbers that is already been selected. Selected numbers are stored in the database and i fetch them in a simple query then compared it to the max number of cars. I have this code, it kinda works but it shows the number 1 which is already been selected. Selected numbers are 1 and 3, it successfully removes number 3 but not 1, The output is 1,2,4,5,6,7,8,9,10 I added some comments for clarity.. Code: [Select] function position_available($event_id, $number_of_cars) // receives id for ref. in the database { $selected_positions = check_selected_positions($event_id); // query to get selected positions while($positions = mysql_fetch_array($selected_positions)) { $position = array(); array_push($position, $positions['car_position']); // 'car position' is the name of the field } for($i = 1; $i <= $number_of_cars; $i++) // $number_of_cars is the total number of cars { if(! in_array($i, $position)) echo "<option value='{$i}'>" . $i . "</option>"; } } Did i miss something simple? thanks for the help in advance! Is it possible to add up a bunch of selected rows? For example, something like this: $sql = "SELECT (and add up) weight FROM store WHERE id LIKE $id" (there are multiple $id's) Obviously that isn't the answer - but hopefully it gives an idea of what I'm after. Thanks Jack Hi first tyvm for reading my help request me and my friend ar trying to build a order form (see png file ^^) so the main idea is some on go to order form.php
in that order form they fil in thear info like name, adress you know ^^
thean here is the treaky part at the and of the form they kan add products to there order by selecting it or search it from the drop down list the items in the dropdown list come from msql
the client can add more or delete items by clicking - or + whean the clint click send the client need to get a mail whit there a list of what theay have orderd and there unic order number wel so far we can do that but we wont to send another list to the company of order where the added products in the order ar sorted out based on the product id number
this is what we have so far http://clientconstru...sion.nl/valeri/
tyvm again voor anyone who read this ^^
Attached Files
bestel-form.png 37.78KB
0 downloads I have say about 7 different interests and beside each interest I want a check box. How would I go about having multiple check boxes checked so I can remove however many interests at one time... For example: [Checked] Hockey Soccer Baseball [Checked] Basketball And the remove all selected button would remove the checked interests. Thanks, Sean Hi guys, Hi all I am having troubles integrating a shopping cart. $action = (isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view'; switch ($action) { case 'add' : addToCart(); break; case 'update' : updateCart(); break; case 'delete' : deleteFromCart(); break; case 'view' : } Allows me to Delete and Update but not Add an item. [php]$action = (isset($_POST['action']) && $_POST['action'] != '') ? $_POST['action'] : 'view'; Allows me to ADD an item to the cart(add action passed from previous page via form) but not Delete or UPDATE Im not sure if I should have attached the pages or just pasted them but heres the code cart.php <?php require_once 'config.php'; require_once 'cart-functions2.php'; require_once 'functions.php'; require_once 'database.php'; $ID = $_POST["ID"]; $pSize = $_POST["size"]; $pcolour = $_POST["colour"]; ?> <?php $action = (isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view'; switch ($action) { case 'add' : addToCart(); break; case 'update' : updateCart(); break; case 'delete' : deleteFromCart(); break; case 'view' : } $ID = $_POST["ID"]; $cartContent = getCartContent(); $numItem = count($cartContent); $pageTitle = 'Shopping Cart'; // show the error message ( if we have any ) displayError(); if ($numItem > 0 ) { ?> <form action="<?php echo $_SERVER['PHP_SELF'] . "?action=update"; ?>" method="post" name="frmCart" id="frmCart"> <table width="780" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable"> <tr class="entryTableHeader"> <td colspan="2" align="center">Item</td> <td align="center">Unit Price</td> <td width="75" align="center">Quantity</td> <td align="center">Total</td> <td width="75" align="center"> </td> </tr> <?php $subTotal = 0; for ($i = 0; $i < $numItem; $i++) { extract($cartContent[$i]); $productUrl = "index.php"; $subTotal += $price * $ct_qty; ?> <tr class="content"> <td width="80" align="center"></td> <td><?php echo $pDesc; ?></td> <td align="right"><?php echo displayAmount($price); ?></td> <td width="75"><input name="txtQty[]" type="text" id="txtQty[]" size="5" value="<?php echo $ct_qty; ?>" class="box" onKeyUp="checkNumber(this);"> <input name="hidCartId[]" type="hidden" value="<?php echo $ct_id; ?>"> <input name="hidProductId[]" type="hidden" value="<?php echo $ID; ?>"> </td> <td align="right"><?php echo displayAmount($price * $ct_qty); ?></td> <td width="75" align="center"> <input name="btnDelete" type="button" id="btnDelete" value="Delete" onClick="window.location.href='<?php echo $_SERVER['PHP_SELF'] . "?action=delete&cid=$ct_id"; ?>';" class="box"> </td> </tr> <?php } ?> cart-functions <?php require_once 'config.php'; /********************************************************* * SHOPPING CART FUNCTIONS *********************************************************/ function addToCart() { $sid = session_id(); $ID = $_GET["ID"]; $pSize = $_GET["size"]; $pcolour = $_GET["colour"]; // current session id // check if the product is already // in cart table for this session $sql = "SELECT ID FROM cart WHERE ID = '$ID' AND ct_session_id = '$sid'"; $result = dbQuery($sql); if (dbNumRows($result) == 0) { // put the product in cart table $sql = "INSERT INTO cart (ID, ct_qty, ct_session_id, ct_date) VALUES ('$ID', 1, '$sid', NOW())"; $result = dbQuery($sql); } else { // update product quantity in cart table $sql = "UPDATE cart SET ct_qty = ct_qty + 1 WHERE ct_session_id = '$sid' AND ID = '$ID'"; $result = dbQuery($sql); } header('Location: cart2.php'); } /* Get all item in current session from shopping cart table */ function getCartContent() { $cartContent = array(); $sid = session_id(); $sql = "SELECT ct.ct_id, ct.ID, ct.ct_size, ct.ct_colour, ct.ct_qty, pd.pDesc, pd.price, pd.ID FROM cart ct, products pd WHERE ct_session_id = '$sid' AND ct.ID = pd.ID"; $result = dbQuery($sql); while ($row = dbFetchAssoc($result)) { $cartContent[] = $row; } return $cartContent; } /* Remove an item from the cart */ function deleteFromCart($cartId = 0) { if (!$cartId && isset($_GET['cid']) && (int)$_GET['cid'] > 0) { $cartId = (int)$_GET['cid']; } if ($cartId) { $sql = "DELETE FROM cart WHERE ct_id = $cartId"; $result = dbQuery($sql); } header('Location: cart2.php'); } /* Update item quantity in shopping cart */ function updateCart() { $cartId = $_POST['hidCartId']; $productId = $_POST['hidProductId']; $itemQty = $_POST['txtQty']; $numItem = count($itemQty); $numDeleted = 0; $notice = ''; for ($i = 0; $i < $numItem; $i++) { $newQty = (int)$itemQty[$i]; if ($newQty < 1) { // remove this item from shopping cart deleteFromCart($cartId[$i]); $numDeleted += 1; } // update product quantity $sql = "UPDATE cart SET ct_qty = $newQty WHERE ct_id = {$cartId[$i]}"; dbQuery($sql); } exit; } function isCartEmpty() { $isEmpty = false; $sid = session_id(); $sql = "SELECT ct_id FROM cart ct WHERE ct_session_id = '$sid'"; $result = dbQuery($sql); if (dbNumRows($result) == 0) { $isEmpty = true; } return $isEmpty; } /* Delete all cart entries older than one day */ function deleteAbandonedCart() { $yesterday = date('Y-m-d H:i:s', mktime(0,0,0, date('m'), date('d') - 1, date('Y'))); $sql = "DELETE FROM cart WHERE ct_date < '$yesterday'"; dbQuery($sql); } ?> Thanks in advance I have the following session variables for each item in a cart, which gets updated each time an item is added from the catalog // Register the session variables $_SESSION['ses_basket_items']=$ses_basket_items; $_SESSION['ses_basket_name']=$ses_basket_name; $_SESSION['ses_basket_amount']=$ses_basket_amount; $_SESSION['ses_basket_price']=$ses_basket_price; $_SESSION['ses_basket_stockcode']=$ses_basket_stockcode; $_SESSION['ses_basket_image']=$ses_basket_image; $_SESSION['ses_basket_size']=$ses_basket_size; $_SESSION['ses_basket_sizechoice']=$ses_basket_sizechoice; $_SESSION['ses_basket_id']=$ses_basket_id; I have a current cart page which loops through the sessions and displays them in a form. I have a check box for each item with the name="remove[]" and the value is the itemID which is the $ses_basket_id of item in the session. I have a select box with name="size[]" and the value is the users selected size for each item in the session which is $ses_basket_size Finnally I have a quantity text box with name="quantity[]" and the value is the users quantity value selected which is $ses_basket_amount. After all the looping through the products displaying each item in a row I have an update cart submit button name="change" outside the loop. What I want to know is how do I setup to perform each of these actions for each item in the session, how do I create the foreach loops for each array to delete or modify the correct items in the session if they are modified. I have the code to perform each task but dont know how to loop it through for each item in the session affecting only the correct session items. so if I have 3 items in the session for example, and I want to change the size of item 1, remove item 2 and change item 3 for quantity 1 to quantity 3 with only the 1 form and change button for all items and actions? Hope that makes sense any help with this would be appreciated. Hello! We our creating an admin page to display a list of all the items within a mysql database, these items are within a table called "item". Each item is categorized, these categories are in a table called "category" each category has an ID. There is then a table called "links" which links the items to their category using ID's. We want to display each item by their category with an edit and delete button. We have it so all the items display with the two buttons in no specific order however we are not too sure how to make it so they all display via their category. Here is the code we are currently using: Code: [Select] <?php $itemlist = mysql_query("SELECT item.item_name, item.item_description, item.item_image, item.item_price, item.item_id FROM item ORDER BY item.item_id ASC"); $item_count = 0; ?> <?php while( $row = mysql_fetch_array($itemlist) ){?> <?php if(($row['item.item_name'] == 1) && (isset($item_count) && $item_count < 1)) { ?> <?php $item_count ++; } ?> <li class="title"> <h3><?php echo $row['item_name'];?></h3><br /> <form action="ourworkadmin.php" method="get"> <input type="hidden" name="item_id" value="<?php echo $row['item_id'];?>" /> <input name="edit" value="Edit" type="submit" /> <input name="remove" value="Remove" type="submit" /> </form> </li> <?php } ?> </ul> <!--Close-items_list--> </div> Any help would be appreciated! Thank you!!! I am trying to do something like this but the code I have doesn't seem to do what I want... Code: [Select] <?php while($item_rows = mysql_fetch_array($item_result)) { $output = " <tr bgcolor=\"#000000\"> <td align=\"center\"><p><img src=\"../images/".$item_rows['item_id'].".gif\" width=\"45\" height=\"45\" /></p> <p>".$item_rows['name']."<br /> Attack: +".$item_rows['attack']."<br /> <img src=\"../images/credit.gif\" width=\"17\" height=\"17\" />$".$item_rows['sell_price']."</p> <p>[Equipt|Destory]<br /> </p> </td>"; $remainder = $i % 5; echo ($remainder == 0 ? ($i == 0 ? "" : "\n</tr>")."\n$output " : $output); } $colspan = (4 - $remainder); $last_row = $colspan ? " <td ".($colspan? "colspan=$colspan" : "")."></td>" : ""; echo $last_row. " </tr> </table>"; ?> I have a php page where I have posts about different topic. I have a menu listing all this categories of topics . Some topics may belong to more than one category. So when i click on a particular menu item, the topic belonging to that category will be displayed. While displaying it is in this format---Topic Heading,Belongs to which all categories,date posted and the description. All this are taken from database. Now my actual issue is I am not able to give links to the categories dispalyed ,if the category exceeds more than 1. For eg: Global Warming Posted on March 11,2011 in News, Issue -----------description of global warming goes here. In the above eg: News and issue are the two categories,when i click on News it should display all the items in news category,and when i click on issue it should display all items of issue category. But I am not able to give this link to the categories. Code: [Select] while($row = mysql_fetch_array($q)) { if($row[6]=='News'){ $c='<a href="category_disp.php?category=news">News</a>'; } if($row[6]=='Issues'){ $c='<a href="category_disp.php?category=issues">Issues</a>'; } echo "<tr><td><p >$row[1]</p><br></td></tr>"; echo "<tr><td> <div >". "<p><span>$row[4]</span> <span>". "<a href='$row[6]' >$row[6]|<a href='$row[5]'> Post Comments</a></span></p>". "<p> </p></div><br></td></tr>"; echo "<tr><td><p >$row[2]</p><br></td></tr>"; echo "<tr><td><p ><a href='$row[3]' >Click Here to read more...</a></p></td></tr>"; echo "</div></td></tr>"; } echo" </table> I'm trying to make it so that someone can add an item to the shop based upon the current items within the database. How do I go about doing that? I know i have to change <input> to <select>, but beyond that, how do I code it so I can run an array to get all the current items from the table `items` and list them by their `name` field? Thanks in advance! Here is what I have so far: function addshopinv($id) { error_reporting(E_ALL); ini_set('display_errors', 1); if (isset($_POST["submit"])) { extract($_POST); $errors = 0; $errorlist = ""; if ($name == "") { $errors++; $errorlist .= "Name is required.<br />"; } else if ($errors == 0) { $dbh=dbconnect() or die ("Userlist read error: " . mysql_error()."<br>"); mysql_select_db("XXX"); $query = mysql_query("SELECT id FROM items WHERE name='$name'"); while ($result = mysql_fetch_array($query)){ $item_id = $result['id']; $query1 = mysql_query("INSERT INTO sale SET shop_id='$id', item_id='$item_id'"); } admindisplay("Inventory Item Added.","Add New Inventory Item"); } else { admindisplay("<b>Errors:</b><br /><div style=\"color:red;\">$errorlist</div><br />Please go back and try again.", "Add New Item to Shop"); } } $page = <<<END <b><u>Add New Inventory Item</u></b><br /><br /> <form action="admin_panel.php?do=addshopinv:$id" method="post"> <table width="90%"> <tr><td width="20%">Name:</td><td><input type="text" name="name" size="30" maxlength="255" value="" />*255 character max</td></tr> </table> <input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" /> </form> END; $page = parsetemplate($page, $row); admindisplay($page, "Add New Inventory Item"); } I want to retrieve items from the DB and seperate them using a comma... while i know how to actually do that, what i want is to automatically have stop adding comas at the last item... so if there are 4 items i want it to show a comma after each item except the last one (1, 2, 3, 4). Does anyone know of a function or can give me an example on how i can do this. I coded the following but some parts are not working (the "curveball" mentioned at the end). How would YOU do this? I have a page (A) displaying a form with a textarea field using a WYSIWYG interface. The user will enter a list of unordered items and upon submit the string will look something like this: This is my list of food items: <ul> <li> Lettuce </li> <li> Tomatoes </li> <li> Eggs </li> <ul> Upon submit, I need to give each list item an unique id and store this id along with the text next to it in a separate table: List_item_id List_item_text List_item_state food_1 Lettuce food_2 Tomatoes food_3 Eggs The user will then land in another page with a form displaying the list. The text is now in the page itself and not in a textarea. Instead of the bullets a dropdown list appears, and the user can select "buy" or "sell" for each list item. This page (B) looks something like this: <form> This is my list of food items: <br><select name="food_1"> <option value="buy">Buy</option> <option value="sell">Sell</option> </select> Lettuce <br><select name="food_2"> <option value="buy">Buy</option> <option value="sell">Sell</option> </select> Tomatoes <br><select name="food_2"> <option value="buy">Buy</option> <option value="sell">Sell</option> </select> Eggs <input type=submit value="submit"> </form> When the user hits "submit" the table with the list items will be updated with the values selected in the dropdown list: List_item_id List_item_text List_item_state food_1 Lettuce Buy food_2 Tomatoes Buy food_3 Eggs Sell The next time the user goes to Page B the list will remember the states. Here's the curveball: At any point the user may click on "EDIT LIST" on Page B so they may add more items (at the begining, middle or end of the string). On edit mode the list items should appear as bullets again inside the WYSIWYG interface. Keep in mind that some of the text in the string may not be a list item (ie, "this is my list of food...") Hi, What I'm trying to do is create a script that generates a random list of cars whenever it is run. I have a list of available cars in a MySQL database. I'm just not sure how to get php to generate a list of, say, 20 cars, that are randomly picked from the cars in the database. I may also want to add other statistics like MPG (also already in the database). Can anyone show me how to do this? And is it possible to format the returned data in a table or div format? Thanks in advance Unk This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=334480.0 I have an audio mix submitting form on my website. The first part of the form takes the details from the user and inserts a new row in the first table 'mix', auto incrementing to generate a mix_id primary key. The second part then uses a series of check boxes to select which genres the mix contains and a foreach for the resulting array to insert, into the 2nd table 'genres_for_mix', a new row for each genre id (foreign key for the genres table) that is selected and the mix_id that it concerns. Now I'm starting to think this is a bad way to do this because: 1. I don't know how to echo this out when a user edit's the mix to make all the relevant checkboxes selected. 2. When a user deselects a choice, it would be quite long winded to delete the nessesary row. So instead I'm thinking that it would be much wiser to user the serialise functions to put the array into the database However, I still don't know how to make the right checkboxes selected when I echo out the edit form. Do I use something like inarray()? If so how? Also, it's really important that I can query the database for mixes that are associated with a specific genre. Without unserialising the array and therefore echo'ing out all the results, I don't know how to query this. Would I just use a 'WHERE genre_field LIKE 'genre_id'? Can anyone help me with this please? Hi, I am new to php and need help. I am building a library system for an assignment. I want to provide a functionality that registers (adds) all transactions to defferent databases. I am stuck on how to add a selected row (selected by a checkbox) to the database and make sure that 2 users cannot update the same record at the same time. Thanks for your help. bellow is my code: Code: [Select] <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <meta name="generator" content="PSPad editor, www.pspad.com"> <title>Web Works DD - Web Development & Software Solutions</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="css/style.css" /> </head> <body> <!--<div class="utilities"> <!-- UTILITIES MENU -- <a href="index.php" style=text-decoration:none;><img src="images/home.png" border="0" alt="Home Page"/> Home Page</a> <a href="sitemap.php" style=text-decoration:none;><img src="images/sitemap.png" border="0" alt="Sitemap"/> Sitemap</a> </div><!-- END UTILITIES --> <div id="right_bar"> <!-- right bar starts here --> </div> <!-- end of right bar --> <div id="top_bar"> <!-- top bar starts here --> <a href="index.php" style=text-ecoration:none; title="Web Works DD - Web Development & Software Solutions"><img src="images/mainLib.jpg" border=0 style=height:180;/> </a> <img src="images/topText.jpg" border=0 style=height:180;/> </div> <!-- end of top bar --> <div id="container"> <div id="top-logo"> <!--<a href="index.php" style=text-ecoration:none; title="Web Works DD - Web Development & Software Solutions"><img src="images/Main_logo.jpg" border=0 /> </a> --> </div> <ul class="dropdown dav"> <li><a href="index1.php">HOME</a></li> <li><a href="#" class="dir">VIEW RWSOURCES</a> <ul> <li><a href="books.php">BOOKS</a></li> <li><a href="dvds.php">DVDS</a></li> <li><a href="journals.php">JOURNALS</a></li> </ul> </li> <li><a href="staffLogin.php">STAFF ZONE</a></li> <li><a href="studentsLogin.php">STUDENT ZONE</a></li> </ul> <table> <!--This defines a new form --> <form action="" method="post"> <!--This creates the dropdown in html--> <select name="fields"> <option value="1">Please select...</option> <option value="isbnno">isbnno</option> <option value="title">title</option> <option value="description">description</option> <option value="author">author</option> </select> </td> </tr> <tr> <td> <input type="text" name="value" size="40"> </td> </tr> <tr> <td> <!-- Create a button --> <input type="submit" value="Select" name="select"> </td> </tr> </form> </table> <?php session_start(); include("secure/db_open.php"); /* $myusername=$_SESSION['myusername']; echo $myusername; $insert = $checkbox[$i]; $cops = $_POST['copies']; $userid ="SELECT id FROM students WHERE username = '$myusername'"; $uido = mysql_query($userid); $row = mysql_fetch_array($uido); echo $row['id']; */ // get variable after selecting something from the dropdown with name 'fields' $select = $_POST['select']; $valu = $_POST['value']; // if something has been chosen if (!empty($select)) { $fields = $_POST['fields']; //get the chosen value $allbooks="SELECT * FROM books WHERE {$fields} = '$valu'"; //select books that are available - driven by the user's input $copas="SELECT * FROM books WHERE {$fields} = '$valu' AND available_copies > 0"; $copy = mysql_query($copas); //count the number of rows resulting from the query $count=mysql_num_rows($copy); //$copse = mysql_fetch_array($copy); //select books that are not available $notavailable="SELECT * FROM books WHERE {$fields} = '$valu' AND available_copies = 0"; $shownotavailable=mysql_query($notavailable); $count1=mysql_num_rows($shownotavailable); //display a form echo "<table width='1100' border='0' cellspacing='1' cellpadding='0'>"; echo "<tr>"; echo "<td>" ; echo "<form name='form1' method='post' action=''>"; echo "<table width='1100' border='0' cellpadding='3' cellspacing='1' bgcolor='#CCCCCC'>"; echo "<tr>"; echo "<td align='center' colspan='9' bgcolor='#FFFFFF'>"; echo "<strong>Available to Loan </strong>" ; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td align='center' bgcolor='#FFFFFF'>Loan</td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>ISBN No.</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Title</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Description</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Author</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Loan Type</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Location</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Available Copies</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Desired Num of Copies</strong></td>"; echo "</tr>"; while(($row = mysql_fetch_assoc($copy))) { echo "<tr>"; echo "<td align='center' bgcolor='#FFFFFF'><input name='checkbox[]' type='checkbox' id='checkbox[]' value=".$row['bookid']."></td>"; echo "<td>" .$row['isbnno']. "</td>"; echo "<td>" .$row['title']. "</td>"; echo "<td>" .$row['description']. "</td>"; echo "<td>" .$row['author']. "</td>"; echo "<td>" .$row['loan_type']. "</td>"; echo "<td>" .$row['location']. "</td>"; echo "<td>" .$row['available_copies']. "</td>"; echo "<td align='center' bgcolor='#FFFFFF'><input name='copies' type='text' id='copies' size='2'></td>"; echo "</tr>"; } echo "<tr>"; echo "<td colspan='9' align='center' bgcolor='#FFFFFF'><input name='loan' type='submit' id='loan' value='Loan Book'></td>"; echo "</tr>"; echo "</table>"; if (isset($_POST)) { session_start(); $myusername=$_SESSION['myusername']; $cops = $_POST['copies']; $userid ="SELECT id FROM students WHERE username = $myusername"; $uido = mysql_query($userid); $row1 = mysql_fetch_array($uido); $sid = $row1['id']; for ($i=0;$i<$count;$i++) { $insert = $checkbox[$i]; //echo "Welcome, ".$row['id']; $insertloaned="INSERT INTO studentloanedbooks(studentid, bookid, noofcopies) VALUES ($sid, $insert, $cops)"; $loaned = mysql_query($insertloaned); } } if($count1>0) { //display the books details //display a form echo "<br>"; echo "<br>"; echo "<table width='1100' border='0' cellspacing='1' cellpadding='0'>"; echo "<tr>"; echo "<td>" ; echo "<form name='form1' method='post' action=''>"; echo "<table width='1100' border='0' cellpadding='3' cellspacing='1' bgcolor='#CCCCCC'>"; echo "<tr>"; echo "<td align='center' colspan='9' bgcolor='#FFFFFF'>"; echo "<strong>Available to Loan </strong>" ; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td align='center' bgcolor='#FFFFFF'>Loan</td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>ISBN No.</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Title</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Description</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Author</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Loan Type</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Location</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Available Copies</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Desired Num of Copies</strong></td>"; echo "</tr>"; while(($row = mysql_fetch_assoc($shownotavailable))) { echo "<tr>"; echo "<td align='center' bgcolor='#FFFFFF'><input name='checkbox[]' type='checkbox' id='checkbox[]' value='.$row[bookid]'></td>"; echo "<td>" .$row['isbnno']. "</td>"; echo "<td>" .$row['title']. "</td>"; echo "<td>" .$row['description']. "</td>"; echo "<td>" .$row['author']. "</td>"; echo "<td>" .$row['loan_type']. "</td>"; echo "<td>" .$row['location']. "</td>"; echo "<td>" .$row['available_copies']. "</td>"; echo "<td align='center' bgcolor='#FFFFFF'><input name='copies' type='text' id='copies' size='2'></td>"; echo "</tr>"; } echo "<tr>"; echo "<td colspan='9' align='center' bgcolor='#FFFFFF'><input name='reserve' type='submit' id='reserve' value='Reserve Book'></td>"; echo "</tr>"; echo "</table>"; } } // include("secure/db_close.php"); //include("secure/db_close.php"); ?> <!--if($cop>0){ --> <!-- this is my footer div --> <div id="footer"> Copyright © 2010 | <a href="privacy.php">Privacy Policy</a> | <a href="terms.php">Terms of Service</a> <img src="images/mini_logo1.png" align="right" style="margin-right:5" /> </div> <!-- End footer div --> </div> </div> </body> </html> MOD EDIT: [code] . . . [/code] BBCode tags added. i do think is a small mistake i making can u please have a look if u can ok here's the problem i am trying to delete an album within the album should also delete the photos related to that album this what i tried gives this error Delete image failed. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3 my tables are table albums fields album_id, album_name, album_owner, sub_album table photos fields, photo_id, photo_name, photo_extension, photo_proper photo_owner, photo_date, photo_comments, photo_size, album_id photo_proper is the name image stored in folder <?php define('ROOT_DIR', './'); define('PROPER', TRUE); /** * include common files */ include_once(ROOT_DIR. 'includes/common.inc.php'); // No album id has been selected if (isset($_GET['albums'])) // get the album name since we need to display // a message that album 'foo' is deleted $result = mysql_query("SELECT album_id, album_name, album_owner, sub_album FROM albums WHERE album_id = $album_id") or die('Delete image failed. ' . mysql_error()); if (mysql_num_rows($result) == 1) { $row = mysql_fetch_assoc($result); $album_id = $row['album_id']; $album_name = $row['album_name']; // get the image filenames first so we can delete them // from the server $result = mysql_query("SELECT photo_id, photo_id FROM photos WHERE album_id = $album_id") or die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { define("GALLERY_IMG_DIR", "./photos/"); unlink(GALLERY_IMG_DIR . $row['photo_proper']); unlink(GALLERY_IMG_DIR . 'thumbs/' . $row['photo_proper']); } $result = mysql_query("DELETE FROM photos WHERE album_id = $album_id") or die('Delete image failed. ' . mysql_error()); $result = mysql_query("DELETE FROM album WHERE album_id = $album_id") or die('Delete album failed. ' . mysql_error()); // album deleted successfully, let the user know about it echo "<p align=center>Album '$album_name' deleted.</p>"; } else { echo "<p align=center>Cannot delete a non-existent album.</p>"; } ?> try 2 error line 5 <?php define('ROOT_DIR', './'); define('PROPER', TRUE); /** * include common files */ include_once(ROOT_DIR. 'includes/common.inc.php'); // No album id has been selected if (isset($_GET['albums'])) { // get the image file name so we // can delete it from the server $sql = "SELECT album_id, album_name, album_owner, sub_album FROM albums WHERE album_id = {$_GET['albums']}"; $result = mysql_query($sql) or die('Delete photo failed. ' . mysql_error()); if (mysql_num_rows($result) == 1) { $row = mysql_fetch_assoc($result); // get the image filenames first so we can delete them // from the server $sql = "SELECT photo_id, photo_proper FROM photos WHERE photo_id = {$_GET['photos']}"; $result = mysql_query($sql) or die('Delete photo failed. ' . mysql_error()); if (mysql_num_rows($result) == 1) { $row = mysql_fetch_assoc($result); define("GALLERY_IMG_DIR", "./photos/"); // remove the image and the thumbnail from the server unlink(GALLERY_IMG_DIR . $row['photo_proper']); unlink(GALLERY_IMG_DIR . 'thumbs/' . $row['photo_proper']); // and then remove the database entry $sql = "DELETE FROM photos WHERE photo_id = {$_GET['photos']}"; $result = mysql_query("DELETE FROM album WHERE album_id = $album_id") or die('Delete album failed. ' . mysql_error()); // album deleted successfully, let the user know about it echo "<p align=center>Album '$album_name' deleted.</p>"; } else { echo "<p align=center>Cannot delete a non-existent album.</p>"; } } } ?> |