Jump to content

TomTom

Member
  • Posts

    4
  • Joined

  • Last visited

Everything posted by TomTom

  1. Ideally, i am trying to figure out a way for the user to pick one of, say 3, options: Search database table by: 1)shop address. 2)contact no 3)contact name and based on that have the information delivered in a table. <form action="select.php" method="get"> Shop Address<input name="1" type="checkbox" value="shop_address" /> Contact No<input name="1" type="checkbox" value="contact_no" /> Contact name<input name="1" type="checkbox" value="contact_name"/> <input name="2" type="text" /> <input type="submit" value="Submit" /> </form> Thought that checkbox's were good that the user could tick the desired checkbox (address) & then enter in the text area (location from address in the table) & get the details echoed out from that. Of course putting the php together for that is way beyond me at this stage. Or is there an easier way that you know of to basically let the user choose the criteria & then search based on that criteria?
  2. Hi Ben, Yes, again, a foolish mistake. I also changed the "id" to "cust_no" in the following: $result = mysql_query("DELETE FROM customer WHERE id=$cust_no") It is working now.Thanks. Much appreciated. I have also been trying to put together a "select" query for my database where the user can decide on the criteria that they want to search the table by. The table has shop_no,shop_ address, contact_no, contact_name. Would check boxes be the best way to go ? <form action="select.php" method="get"> Shop Address<input name="1" type="checkbox" value="shop_address" /> Contact No<input name="1" type="checkbox" value="contact_no" /> Contact name<input name="1" type="checkbox" value="weight"/> <input name="2" type="text" /> <input type="submit" value="Submit" /> </form> Could this be used to tick one box & then enter text to narrow the query further? Not sure where to go from there.
  3. Hi Ben, Thanks for getting back to me on this. As requested here is my "view.php": I have re-checked it against your original & it seems OK....but then again, what do I know. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>View Records</title> </head> <body> <?php /* VIEW.PHP Displays all data from the customer table */ // connect to the database include('connect-db.php'); // get results from database $result = mysql_query("SELECT * FROM customer") or die(mysql_error()); // display data in table echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>"; echo "<table border='1' cellpadding='10'>"; echo "<tr> <th>Customer No</th> <th> Name</th> <th>Address</th> <th>Phone no</th><th></th> <th></th></tr>"; // loop through results of database query, displaying them in the table while($row = mysql_fetch_array( $result )) { // echo out the contents of each row into a table echo "<tr>"; echo '<td>' . $row['cust_no'] . '</td>'; echo '<td>' . $row['name'] . '</td>'; echo '<td>' . $row['address'] . '</td>'; echo '<td>' . $row['phone_no'] . '</td>'; echo '<td><a href="edit.php?cust_no=' . $row['cust_no']. '">Edit</a></td>'; echo '<td><a href="delete.php?cust_no=' . $row['cust_no'] . '">Delete</a></td>'; echo "</tr>"; } // close table> echo "</table>"; ?> <p><a href="new.php">Add a new record</a></p> </body> </html> On the "edit.php" question. I went over it again & did find that stupid mistake that you showed above regarding the "//" Its working great now, thanks. I have left in the last "}" at the end as if I took it out it left an eneven number throughout the document. They are supposed to be even pairs ..aren't they? Regards, TomTom.
  4. Hi Ben, I am totally new to php & found this which has helped me greatly to understand it.Thanks. I have used your MySQL first example in MySQL basic to see how it all works but am having issues with the "delete" & the "edit". I was hoping you could help me with it? The "delete" is just not doing anything when it is clicked. <?php // connect to the database include('connect-db.php'); // check if the 'id' variable is set in URL, and check that it is valid if (isset($_GET['cust_no']) && is_numeric($_GET['cust_no'])) { // get id value $cust_no = $_GET['cust_no']; // delete the entry $result = mysql_query("DELETE FROM customer WHERE id=$cust_no") or die(mysql_error()); // redirect back to the view page header("Location: view.php"); } else // if id isn't set, or isn't valid, redirect back to view page { header("Location: view.php"); } ?> In the "edit.php" I am getting a: Parse error: syntax error, unexpected T_ELSE on line 96, which would appear to be the last "else" in the code below. (Hope I have laid this out right & in the proper order, if not, my appologies. the table is: customer. the elements of the table are: cust_no, name,address, phone_no. <?php // creates the edit record form function renderForm($cust_no, $name, $address, $phone_no, $error) { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Edit Record</title> </head> <body> <?php // if there are any errors, display them if ($error != '') { echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; } ?> <form action="" method="post"> <input type="hidden" name="cust_no" value="<?php echo $cust_no; ?>"/> <div> <p><strong>Customer No:</strong> <?php echo $cust_no; ?></p> <strong>Name: *</strong> <input type="text" name="name" value="<?php echo $name; ?>"/><br/> <strong>Address: *</strong> <input type="text" name="address" value="<?php echo $address; ?>"/><br/> <strong>Phone No: *</strong> <input type="text" name="phone_no" value="<?php echo $phone_no; ?>"/><br/> <p>* Required</p> <input type="submit" name="submit" value="Submit"> </div> </form> </body> </html> <?php } // connect to the database include('connect-db.php'); // check if the form has been submitted. If it has, process the form and save it to the database if (isset($_POST['submit'])) { // confirm that the 'id' value is a valid integer before getting the form data if (is_numeric($_POST['cust_no'])) { // get form data, making sure it is valid $cust_no = $_POST['cust_no']; $name = mysql_real_escape_string(htmlspecialchars($_POST['name'])); $address = mysql_real_escape_string(htmlspecialchars($_POST['address'])); $phone_no = mysql_real_escape_string(htmlspecialchars($_POST['phone_no'])); // check that fields are filled in if ($name == '' || $address == '' || $phone_no == '') { // generate error message $error = 'ERROR: Please fill in all required fields!'; //error, display form renderForm($cust_no, $name, $address, $phone_no, $error); } else { // save the data to the database mysql_query("UPDATE customer SET name='$name', address='$address', phone_no='$phone_no' WHERE cust_no='$cust_no'") or die(mysql_error()); // once saved, redirect back to the view page header("Location: view.php"); } } else { // if the 'id' isn't valid, display an error echo 'Error!'; } } else // if the form hasn't been submitted, get the data from the db and display the form { // get the 'id' value from the URL (if it exists), making sure that it is valid (checking that it is numeric/larger than 0) if (isset($_GET['cust_no']) && is_numeric($_GET['cust_no']) && $_GET['cust_no'] > 0) { // query db $cust_no = $_GET['cust_no']; $result = mysql_query("SELECT * FROM customer WHERE cust_no=$cust_no") or die(mysql_error()); $row = mysql_fetch_array($result); // check that the 'id' matches up with a row in the databse if($row) { // get data from db $name = $row['name']; $address = $row['address']; $phone_no = $row['phone_no']; // show form renderForm($cust_no, $name, $address, $phone_no, ''); } else // if no match, display result { echo "No results"; } } else // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error { echo 'Error'; } } ?> If you can help with these issues, I would be very thankful.
×
×
  • Create New...