Jump to content
Larry Ullman's Book Forums

sorin

Members
  • Content Count

    29
  • Joined

  • Last visited

Community Reputation

0 Neutral

About sorin

  • Rank
    Member
  1. This is the page from chapter 12 with view users. If in the database I have a lot of users and I want to put them for example each 10 users on a column and to make 2-3-4 columns how can I do that? Like this: foo foo foo foo foo foo foo foo foo foo foo foo ect <?php $page_title = 'View the Current Users'; include ('includes/header.php'); echo '<h1>Registered Users</h1>'; require ('mysqli_connect.php'); // Define the query: $q = "SELECT last_name, first_name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr, user_id FROM users ORDER BY registration_date ASC"; $r = @mysqli_query ($dbc, $q) or die("Error: ".mysqli_error($dbc)); // Count the number of returned rows: $num = mysqli_num_rows($r); if ($num > 0) { // If it ran OK, display the records. // Print how many users there are: echo "<p>There are currently $num registered users.</p>\n"; // Table header: echo '<table align="center" cellspacing="3" cellpadding="3" width="75%"> <tr> <td align="left"><b>Edit</b></td> <td align="left"><b>Delete</b></td> <td align="left"><b>Last Name</b></td> <td align="left"><b>First Name</b></td> <td align="left"><b>Date Registered</b></td> </tr> '; // Fetch and print all the records: while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { echo '<tr> <td align="left"><a href="edit_user.php?id=' . $row['user_id'] . '">Edit</a></td> <td align="left"><a href="delete_user.php?id=' . $row['user_id'] . '">Delete</a></td> <td align="left">' . $row['last_name'] . '</td> <td align="left">' . $row['first_name'] . '</td> <td align="left">' . $row['dr'] . '</td> </tr> '; } echo '</table>'; mysqli_free_result ($r); } else { // If no records were returned. echo '<p class="error">There are currently no registered users.</p>'; } mysqli_close($dbc); include ('includes/footer.html'); ?>
  2. It's from ch17, from forum.php I put it online so you can see it http://www.viatasisanatatea.ro/ch17/forum.php How come that you answer to this question but the one with the flags you didn't ? I asked for an idea not code. <?php # Script 17.4 - forum.php // This page shows the threads in a forum. include ('includes/header.php'); // Retrieve all the messages in this forum... // If the user is logged in and has chosen a time zone, // use that to convert the dates and times: if (isset($_SESSION['user_tz'])) { $first = "CONVERT_TZ(p.posted_on, 'UTC', '{$_SESSION['user_tz']}')"; $last = "CONVERT_TZ(p.posted_on, 'UTC', '{$_SESSION['user_tz']}')"; } else { $first = 'p.posted_on'; $last = 'p.posted_on'; } // The query for retrieving all the threads in this forum, along with the original user, // when the thread was first posted, when it was last replied to, and how many replies it's had: $q = "SELECT t.thread_id, t.subject, username, COUNT(post_id) - 1 AS responses, MAX(DATE_FORMAT($last, '%e-%b-%y %l:%i %p')) AS last, MIN(DATE_FORMAT($first, '%e-%b-%y %l:%i %p')) AS first FROM threads AS t INNER JOIN posts AS p USING (thread_id) INNER JOIN users AS u ON t.user_id = u.user_id WHERE t.lang_id = {$_SESSION['lid']} GROUP BY (p.thread_id) ORDER BY last DESC"; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) > 0) { // Create a table: echo '<table width="100%" border="0" cellspacing="2" cellpadding="2" align="center"> <tr> <td align="left" width="50%"><em>' . $words['subject'] . '</em>:</td> <td align="left" width="20%"><em>' . $words['posted_by'] . '</em>:</td> <td align="center" width="10%"><em>' . $words['posted_on'] . '</em>:</td> <td align="center" width="10%"><em>' . $words['replies'] . '</em>:</td> <td align="center" width="10%"><em>' . $words['latest_reply'] . '</em>:</td> </tr>'; // Fetch each thread: while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { echo '<tr> <td align="left"><a href="read.php?tid=' . $row['thread_id'] . '">' . $row['subject'] . '</a></td> <td align="left">' . $row['username'] . '</td> <td align="center">' . $row['first'] . '</td> <td align="center">' . $row['responses'] . '</td> <td align="center">' . $row['last'] . '</td> </tr>'; print_r($row); } echo '</table>'; // Complete the table. } else { echo '<p>There are currently no messages in this forum.</p>'; } // Include the HTML footer file: include ('includes/footer.php'); ?>
  3. Larry I tried to think how do do your example from ch17 with language flags, without users login, but is beyond my knowledge. This is my first example that I learn how to select language using the database and I want to learn it very well. So I put 2 links with flags images but I'm not sure how and where to start . Please give an idea. This is the link where is the ch17 folder online: http://www.viatasisanatatea.ro/ch17/forum.php
  4. This is what I get from print_r using $row from while loop: Array ( [thread_id] => 7 [subject] => Sample Thread [username] => troutster [responses] => 2 [last] => [first] => ) Can you tell me why?
  5. In the header you put a default timezone, $_SESSION['user_tz'] = 'America/New_York'; but can please tell me how to GET the real timezone? My guess you have to get that on the login page, right? I'm talking about a php function, if there is one, to get the timezone and not have it on database already.
  6. Larry this is the link that I have from email whern I clicked to activate the account: http://www.aproapetot.com/ch18/activate.php?x=sorin21us%40yahoo.com&y=e123adeb2f8837ae6968483526e491b9 Why I get server not foud?
  7. A friend that knows js and php helped me. He told me this is Ajax. After your answer and after his help, my question to you is: Can you, please, recommend me a js with php book? $(document).ready(function() { //var q = $('#categ_id').val(); $('.categ_id').on("change", function () { q = $(this).val(); $.ajax({ url: 'http://aproapetot.ro/ajax/get_subcateg.php', data: 'categ_id='+q, success: function (data) { $('#subcateg_id').html(data); } }).error(function() { alert ('An error occured'); }); }); }); <?php require_once ('../includes/mysql.inc.php'); $categ_id = (isset($_GET['categ_id'])&&is_numeric($_GET['categ_id']))?$_GET['categ_id']:die("The category is not set or it has a wrong value."); ?> <select name="subcategory"> <option>Select One</option> <?php $q = 'SELECT `subcategory_id`, `subcategory` FROM `subcategories` WHERE `category_id`='.$categ_id.' ORDER BY `subcategory` ASC'; $r = mysqli_query ($dbc, $q); while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) { echo "<option value=\"$row[0]\">$row[1]</option>\n"; } ?>
  8. Larry, on the add_page I have to have the categories, subcategories, title and description of the page. When I do the sql SELECT categories and bring in the select tag, then SELECT subcategories and bring in the select tag, I do that because I have to know from that a specific page belongs to an category and what subcategory. So the database is in that way because when I do the insert INSERT INTO pages (category_id, subcategory_id, title, content_page, user_id) VALUES ($cat, '$subcat', '$t', '$c', 1 I can't insert in pages only the category without the subcategory. So this is the add_page live, of course I didn't put the css yet. http://aproapetot.ro/add_page.php In the left you can see all the categories that I brought from database. Those are jokes categories. So when I click on Bancuri(only in this I inserted) I will see the subcategories. Click on subcategory Albanezi an view the page. So in the end you tell me not to put a subcategory Select tag? Because my question was about that tag not the database . If it's like that, I am confused. I don't know how to do the insert then and secondly to do that 'route' on php pages: categories->subcategories->view page
  9. Having this database: CREATE TABLE `categories` ( `category_id` SMALLINT NOT NULL AUTO_INCREMENT, `category` VARCHAR(30) NOT NULL, PRIMARY KEY (`category_id`), UNIQUE KEY `category` (`category`) ) ; CREATE TABLE `subcategories` ( `subcategory_id` SMALLINT NOT NULL AUTO_INCREMENT, `category_id` SMALLINT NOT NULL, `subcategory` VARCHAR(30) NOT NULL, PRIMARY KEY (`subcategory_id`), UNIQUE KEY `subcategory` (`subcategory`) ) ; CREATE TABLE `pages` ( `id` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, `category_id` SMALLINT UNSIGNED NOT NULL, `subcategory_id` SMALLINT UNSIGNED NOT NULL, `user_id` SMALLINT UNSIGNED NOT NULL, `title` VARCHAR(100) NOT NULL, `content_page` LONGTEXT NOT NULL, `date_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `category_id` (`category_id`), KEY `creation_date` (`date_created`) ); CREATE TABLE `users` ( `user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `type` ENUM('member','admin', 'superadmin') NOT NULL, `username` VARCHAR(30) NOT NULL, `email` VARCHAR(80) NOT NULL, `pass` VARBINARY(32) NOT NULL, `first_name` VARCHAR(20) NOT NULL, `last_name` VARCHAR(40) NOT NULL, `date_expires` DATE NOT NULL, `date_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `date_modified` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`user_id`), UNIQUE KEY `username` (`username`), UNIQUE KEY `email` (`email`) ) ; I want to perform a second Select tag into the form. This second Select tag will bring the items from database, depending on the first Select. I am not sure how to do the sql WHERE for the second Select tag. i hope you understand what I say. When I click on the first select and select on item, in the second select I want to have only the items related to the item_id that I selected. So here is the add_page.php with the form and 2 select tags, from your book: <?php require_once ('includes/config.inc.php'); require ('includes/form_functions.inc.php'); // Redirect non-administrators: //redirect_invalid_user('user_admin'); $page_title = 'Add a Site Content Page'; include ('includes/header.php'); // Require the database connection: //require(MYSQL); // Create an array for storing errors: $add_page_errors = array( ); // Validate the page title: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Validate the category: if (filter_var($_POST['category'], FILTER_VALIDATE_INT, array('min_range' => 1))) { $cat = $_POST['category']; } else { $add_page_errors['category'] = 'Please select a category!'; } // Validate the subcategory: if (filter_var($_POST['subcategory'], FILTER_VALIDATE_INT, array('min_range' => 1))) { $subcat = $_POST['subcategory']; } else { $add_page_errors['subcategory'] = 'Please select a subcategory!'; } // Validate the title: if (!empty($_POST['title'])) { $t = mysqli_real_escape_string($dbc, strip_tags($_POST['title'])); } else { $add_page_errors['title'] = 'Please enter the title!'; } // Validate the content: if (!empty($_POST['content_page'])) { $allowed = '<div><p><span><br><a><img><h1><h2><h3><h4><ul><ol><li><blockquote>'; $c = mysqli_real_escape_string($dbc, strip_tags($_POST['content_page'], $allowed)); } else { $add_page_errors['content_page'] = 'Please enter the content!'; } // If there are no errors, add the record to the database: if (empty($add_page_errors)) { // If everything's OK. $q = "INSERT INTO pages (category_id, subcategory_id, title, content_page, user_id) VALUES ($cat, '$subcat', '$t', '$c', 1)"; $r = mysqli_query ($dbc, $q); if (mysqli_affected_rows($dbc) == 1) { // If it ran OK. echo '<h4>The page has been added!</h4>'; $_POST = array( ); } else { // If it did not run OK. trigger_error('The page could not be added due to a system error. We apologize for any inconvenience.'); } } // End of $add_page_errors IF. } // End of the main form submission conditional. ?> <form action="add_page.php" method="post" accept-charset="utf-8"> <fieldset><legend>Completeaza toate campurile:</legend> <p><label for="category"><strong>Categorie</strong></label><br /> <select name="category"<?php if (array_key_exists('category', $add_page_errors)) echo ' class="error"'; ?>> <option>Select One</option> <?php // Retrieve all the categories and add to the pull-down menu: $q = "SELECT category_id, category FROM categories ORDER BY category ASC"; $r = mysqli_query ($dbc, $q); while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) { echo "<option value=\"$row[0]\""; // Check for stickyness: if (isset($_POST['category']) && ($_POST['category'] == $row[0]) ) echo ' selected="selected"'; echo ">$row[1]</option>\n"; } ?> </select><?php if (array_key_exists('category', $add_page_errors)) echo ' <span class="error">' . $add_page_errors['category'] . '</span>'; ?></p> <p><label for="subcategory"><strong>Subcategorie</strong></label><br /> <select name="subcategory"<?php if (array_key_exists('subcategory', $add_page_errors)) echo ' class="error"'; ?>> <option>Select One</option> <?php // Retrieve all the subcategories and add to the pull-down menu: $q = "SELECT categories.category_id, categories.category, subcategories.subcategory_id, subcategories.category_id, subcategories.subcategory FROM categories INNER JOIN subcategories ON categories.category_id = subcategories.category_id ORDER BY subcategory ASC"; $r = mysqli_query ($dbc, $q); while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) { echo "<option value=\"$row[0]\""; // Check for stickyness: if (isset($_POST['subcategory']) && ($_POST['subcategory'] == $row[0]) ) echo ' selected="selected"'; echo ">$row[1]</option>\n"; } ?> </select><?php if (array_key_exists('subcategory', $add_page_errors)) echo ' <span class="error">' . $add_page_errors['subcategory'] . '</span>'; ?></p> <p><label for="title"><strong>Titlu</strong> </label><br /><?php create_form_input('title', 'text', $add_page_errors); ?></p> <p><label for="content_page"><strong>Continut</strong></label> <br /><?php create_form_input('content_page', 'textarea', $add_page_errors); ?></p> <p><input type="submit" name="submit_button" value="Adauga" id="submit_button" class="formbutton" /></p> </fieldset> </form> <?php include('includes/footer.php'); ?>
  10. There is still an error. If you open a record to edit and you don't edit anything if you click submit you get this error: The user could not be edited due to a system error. We apologize for any inconvenience. Query: UPDATE users SET first_name='Melissa', last_name='Bank', email='melissa@authors.com' WHERE user_id=13 LIMIT 1
  11. Same problem Lary like in the past. Friday I got the error, today when I check it again was working. Now I know that when I get an error I will clean the history complete for all day.
  12. I get this error: The user could not be edited due to a system error. We apologize for any inconvenience. Query: UPDATE users SET first_name='John', last_name='Lennonn', email='john@beatles.com' WHERE user_id=3 LIMIT 1 And this is the page: $page_title = 'Edit a User'; include ('includes/header.html'); echo '<h1>Edit a User</h1>'; // Check for a valid user ID, through GET or POST: if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // From view_users.php $id = $_GET['id']; $id = $_GET['id']; } elseif ( (isset($_POST['id'])) &&(is_numeric($_POST['id'])) ) { // Form submission. $id = $_POST['id']; } else { // No valid ID, kill the script. echo '<p class="error">This page has been accessed in error.</p>'; include ('includes/footer.html'); exit( ); } require_once ('mysqli_connect.php'); // Check if the form has been submitted: if ($_SERVER['REQUEST_METHOD'] == 'POST') { $errors = array( ); // Check for a first name: if (empty($_POST['first_name'])) { $errors[] = 'You forgot to enter your first name.'; } else { $fn = mysqli_real_escape_string($dbc, trim($_POST['first_name'])); } // Check for a last name: if (empty($_POST['last_name'])) { $errors[] = 'You forgot to enter your last name.'; } else { $ln = mysqli_real_escape_string($dbc, trim($_POST['last_name'])); } // Check for an email address: if (empty($_POST['email'])) { $errors[] = 'You forgot to enter your email address.'; } else { $e = mysqli_real_escape_string($dbc, trim($_POST['email'])); } if (empty($errors)) { // If everything's OK. // Test for unique email address: $q = "SELECT user_id FROM users WHERE email='$e' AND user_id != $id"; $r = @mysqli_query($dbc, $q) or die("Error: ".mysqli_error($dbc)) ; if (mysqli_num_rows($r) == 0) { // Make the query: $q = "UPDATE users SET first_name='$fn', last_name='$ln', email='$e' WHERE user_id=$id LIMIT 1"; $r = @mysqli_query ($dbc, $q) or die("Error: ".mysqli_error($dbc)) ; if (mysqli_affected_rows($dbc) == 1) { // If it ran OK. // Print a message: echo '<p>The user has been edited.</p>'; } else { // If it did not run OK. // Public message. echo '<p class="error">The user could not be edited due to a system error. We apologize for any inconvenience.</p>'; echo '<p>' . mysqli_error($dbc). '<br />Query: ' . $q . '</p>'; // Debugging message. } } else { // Already registered. echo '<p class="error">The email address has already been registered.</p>'; } } else { // Report the errors. echo '<p class="error">The following error(s) occurred:<br />'; foreach ($errors as $msg) { // Print each error. echo " - $msg<br />\n"; } echo '</p><p>Please try again.</p>'; } // End of if (empty($errors)) IF. } // End of submit conditional. // Always show the form... // Retrieve the user's information: $q = "SELECT first_name, last_name, email FROM users WHERE user_id=$id"; $r = @mysqli_query ($dbc, $q) or die("Error: ".mysqli_error($dbc)) ; if (mysqli_num_rows($r) == 1) { // Valid user ID, show the form. // Get the user's information: $row = mysqli_fetch_array ($r, MYSQLI_NUM); // Create the form: echo '<form action="edit_user.php" method="post"> <p>First Name: <input type="text" name="first_name" size="15" maxlength="15" value="' . $row[0] . '" /></p> <p>Last Name: <input type="text" name="last_name" size="15" maxlength="30" value="' . $row[1] . '" /></p> <p>Email Address: <input type="text" name="email" size="20" maxlength="60" value="' . $row[2] . '" /> </p> <p><input type="submit" name="submit" value="Submit" /></p> <input type="hidden" name="id" value="' . $id. '" /> </form>'; } else { // Not a valid user ID. echo '<p class="error">This page has been accessed in error.</p>'; } mysqli_close($dbc); include ('includes/footer.html');
  13. i understand what you are saying but that means to make a lot of tables if jokes, short films, and stories will have each one a table. If I will have 10 categories that means 10 tables. If I follow this idea will mean that each subcategory from jockes will have a diff table. What will happening if I will have subsubcategories. A lot of tables. If I don't understand exactly what you said please take a simple ex, or mine, and explain. Thank you.
  14. I will show you a better example. Let's say that this is a site for jokes. I will have categories, that will be the menu as well: jokes, short films, stories. So these are the categories. Now the subcategories will be for jokes: sailors, plolice men, blondes, etc shorts films: funny films, crazy films, wow films stories: funny stories, wise stories Subsubcategories stories: funny stories: usa, canada, europe Of course the list for categories and subcategories could be longer then what I put here. So in this direction I made the tables: CREATE TABLE `categories` ( `category_id` SMALLINT NOT NULL AUTO_INCREMENT, `category` VARCHAR(30) NOT NULL, PRIMARY KEY (`category_id`), UNIQUE KEY `category` (`category`) ) ; CREATE TABLE `subcategories` ( `subcategory_id` SMALLINT NOT NULL AUTO_INCREMENT, `category_id` SMALLINT NOT NULL, `subcategory` VARCHAR(30) NOT NULL, PRIMARY KEY (`subcategory_id`), UNIQUE KEY `subcategory` (`subcategory`) ) ; CREATE TABLE `subsubcategories` ( `subsubcategory_id` SMALLINT NOT NULL AUTO_INCREMENT, `subcategory_id` SMALLINT NOT NULL, `subsubcategory` VARCHAR(30) NOT NULL, PRIMARY KEY (`subsubcategory_id`), UNIQUE KEY `subsubcategory` (`subsubcategory`) ) ; CREATE TABLE `pages` ( `id` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, `category_id` SMALLINT UNSIGNED NOT NULL, `subcategory_id` SMALLINT UNSIGNED NOT NULL, `user_id` SMALLINT UNSIGNED NOT NULL, `title` VARCHAR(100) NOT NULL, `content_page` LONGTEXT NOT NULL, `date_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `category_id` (`category_id`), KEY `creation_date` (`date_created`) ); CREATE TABLE `users` ( `user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `type` ENUM('member','admin', 'superadmin') NOT NULL, `username` VARCHAR(30) NOT NULL, `email` VARCHAR(80) NOT NULL, `pass` VARBINARY(32) NOT NULL, `first_name` VARCHAR(20) NOT NULL, `last_name` VARCHAR(40) NOT NULL, `date_expires` DATE NOT NULL, `date_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `date_modified` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`user_id`), UNIQUE KEY `username` (`username`), UNIQUE KEY `email` (`email`) ) ; Sometimes I will have subsubcategories, sometimes I will not have. Please tell me if i designed the database corect.
×
×
  • Create New...