Jump to content
Larry Ullman's Book Forums

Roman

Members
  • Posts

    5
  • Joined

  • Last visited

Everything posted by Roman

  1. Sorry for wasting peoples time but I managed to get it sorted by simply removing the line containing the mysqli_stmt_insert_id statrement and setting the $id below to the artist name.
  2. Hi boys and girls, I have an issue with an altered version of add_book.php, which is used to add musical artists to an artists table in database. As far as I can see my code is fine as have altered this script a couple of times for various uses but for the life of me I cannot figure out why it is not working. The error that is being thrown out is : Your submission could not be processed due to a system error. I have tracked through the code slowly but cannot work out what is causing this error. I have attached the code below and if anyone can help then I would be very appreciative. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>Add an Artist</title> </head> <body> <?php # add_artist.php // This page allows the administrator to add a artist (product). require_once ('../mysqli_connect_rock.php'); if (isset($_POST['submitted'])) { // Handle the form. // Validate the incoming data... $errors = array(); // Check for a artist name: if (!empty($_POST['artist_name'])) { $an = trim($_POST['artist_name']); } else { $errors[] = 'Please enter the artist\'s name!'; } // Check for an image: if (is_uploaded_file ($_FILES['image']['tmp_name'])) { // Create a temporary file name: $temp = '../../uploads/' . md5($_FILES['image']['name']); // Move the file over: if (move_uploaded_file($_FILES['image']['tmp_name'], $temp)) { echo '<p>The file has been uploaded!</p>'; // Set the $i variable to the image's name: $i = $_FILES['image']['name']; } else { // Couldn't move the file over. $errors[] = 'The file could not be moved.'; $temp = $_FILES['image']['tmp_name']; } } else { // No uploaded file. $errors[] = 'No file was uploaded.'; $temp = NULL; } // Check for a description (not required): $d = (!empty($_POST['description'])) ? trim($_POST['description']) : NULL; // Validate the genre if (isset($_POST['genre']) && ($_POST['genre'] == 'new') ) { // If it's a new genre, add the genre to the database... // Check for a genre_name... if (!empty($_POST['genre_type'])) { $gt = trim($_POST['genre_type']); // Add the genre to the database: $q = 'INSERT INTO genres (genre_type) VALUES (?)'; $stmt = mysqli_prepare($dbc, $q); mysqli_stmt_bind_param($stmt, 's', $gt); mysqli_stmt_execute($stmt); // Check the results.... if (mysqli_stmt_affected_rows($stmt) == 1) { echo '<p>The genre has been added.</p>'; $gt = mysqli_stmt_insert_id($stmt); // Get the genre ID. } else { // Error! $errors[] = 'The new genre could not be added to the database!'; } // Close this prepared statement: mysqli_stmt_close($stmt); } else { // No genre type value. $errors[] = 'Please enter a genre type!'; } } elseif ( isset($_POST['genre']) && ($_POST['genre'] == 'existing') && ($_POST['existing'] > 0) ) { // Existing genre. $gt = (int) $_POST['existing']; } else { // No genre selected. $errors[] = 'Please enter or select the artist\'s genre!'; } if (empty($errors)) { // If everything's OK. // Add the Artist to the database: $q = 'INSERT INTO artists (artist_name, description, image_name, genres_genre_id) VALUES (?, ?, ?, ?)'; $stmt = mysqli_prepare($dbc, $q); mysqli_stmt_bind_param($stmt, 'ssis', $a, $d, $gi, $i); mysqli_stmt_execute($stmt); // Check the results... if (mysqli_stmt_affected_rows($stmt) == 1) { // Print a message: echo '<p>The artist has been added.</p>'; // Rename the image: $id = mysqli_stmt_insert_id($stmt); // Get the artist ID. rename ($temp, "../../uploads/$id"); // Clear $_POST: $_POST = array(); } else { // Error! echo '<p style="font-weight: bold; color: #C00">Your submission could not be processed due to a system error.</p>'; } mysqli_stmt_close($stmt); } // End of $errors IF. // Delete the uploaded file if it still exists: if ( isset($temp) && file_exists ($temp) && is_file($temp) ) { unlink ($temp); } } // End of the submission IF. // Check for any errors and print them: if ( !empty($errors) && is_array($errors) ) { echo '<h1>Error!</h1> <p style="font-weight: bold; color: #C00">The following error(s) occurred:<br />'; foreach ($errors as $msg) { echo " - $msg<br />\n"; } echo 'Please reselect the artists image and try again.</p>'; } // Display the form... ?> <h1>Add an Artist</h1> <form enctype="multipart/form-data" action="add_artist.php" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="524288" /> <fieldset><legend>Fill out the form to add an artist to the catalog:</legend> <p><b>Artist Name:</b> <input type="text" name="artist_name" size="30" maxlength="60" value="<?php if (isset($_POST['artist_name'])) echo htmlspecialchars($_POST['artist_name']); ?>" /></p> <p><b>Image:</b> <input type="file" name="image" /></p> <div><b>Genre:</b> <p><input type="radio" name="genre" value="existing" <?php if (isset($_POST['genre']) && ($_POST['genre'] == 'existing') ) echo ' checked="checked"'; ?> /> Existing => <select name="existing"><option>Select One</option> <?php // Retrieve all the genres and add to the pull-down menu. $q = "SELECT genre_id, CONCAT_WS(' ', genre_type) FROM genres ORDER BY genre_type ASC"; $r = mysqli_query ($dbc, $q); if (mysqli_num_rows($r) > 0) { while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) { echo "<option value=\"$row[0]\""; // Check for stickyness: if (isset($_POST['existing']) && ($_POST['existing'] == $row[0]) ) echo ' selected="selected"'; echo ">$row[1]</option>\n"; } } else { echo '<option>Please add a new genre.</option>'; } mysqli_close($dbc); // Close the database connection. ?> </select></p> <p><input type="radio" name="genre" value="new" <?php if (isset($_POST['genre']) && ($_POST['genre'] == 'new') ) echo ' checked="checked"'; ?> /> New => Genre Name: <input type="text" name="genre_type" size="10" maxlength="40" value="<?php if (isset($_POST['genre_type'])) echo $_POST['genre_type']; ?>" /></p> </div> <p><b>Description:</b> <textarea name="description" cols="40" rows="5"><?php if (isset($_POST['description'])) echo $_POST['description']; ?></textarea></p> </fieldset> <div align="center"><input type="submit" name="submit" value="Submit" /></div> <input type="hidden" name="submitted" value="TRUE" /> </form> </body> </html> Many thanks in advance Roman EDIT : I think the issue is to do with the : // Rename the image: $id = mysqli_stmt_insert_id($stmt); // Get the artist ID. rename ($temp, "../../uploads/$id"); as its looking for the auto_increment id in the previous query but there isn't one. Is there an alternative to use the artists name? cheers
  3. Rebuilt the script from scratch and it worked perfectly. Thanks for pointing me inright direction.
  4. Thanks for quick reply HartleySan. Have just echoed $pages variable and it returns 1 so obviously that is the problem. Will have a play and try and resolve before posting back.
  5. Hello all. I am a relative newbie to php and mysql but have been progressing through book with great results up until i have hit a wall with a pagination script. The scripts used are a combination of 9.4 - paginate view users and chapter 17 ecommerce from the PHP 6 and MySQL 5 book. The issue involves the script not creating further links for additional pages should the results returned exceed the $display variable. I don't get any next or previous links at all and for the life of me cannot see why. The page displays with whatever the $display number is set out but no links to additional results. My code is below but the site is local so cannot show you direct links. <?php # browse_books.php // This page displays the available books (products). // Set the page title and include the HTML header: $page_title = 'Browse Books'; include ('includes/header.html'); require_once ('../mysqli_connect.php'); // Number of records to show per page: $display = 15; // Determine how many pages there are... if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined. $pages = $_GET['p']; } else { // Need to determine. // Count the number of records: $q = "SELECT COUNT (book_id) FROM books"; $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records = $row[0]; // Calculate the number of pages... if ($records > $display) { // More than 1 page. $pages = ceil ($records/$display); } else { $pages = 1; } } // End of p IF. // Determine where in the database to start returning results... if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } // Default query for this page: $q = "SELECT authors.author_id, CONCAT_WS(' ', first_name, middle_name, last_name) AS author, book_name, price, description, book_id FROM authors, books WHERE authors.author_id = books.author_id ORDER BY authors.last_name ASC, books.book_name ASC LIMIT $start, $display"; // Are we looking at a particular author? if (isset($_GET['aid']) && is_numeric($_GET['aid']) ) { $aid = (int) $_GET['aid']; if ($aid > 0) { // Overwrite the query: $q = "SELECT authors.author_id, CONCAT_WS(' ', first_name, middle_name, last_name) AS author, book_name, price, description, book_id FROM authors, books WHERE authors.author_id = books.author_id AND books.author_id = $aid ORDER BY books.book_name"; } } // Display all the books, linked to URLs: $r = mysqli_query ($dbc, $q); while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) { // Display each record: echo "<div class=\"book_holder\"> <h1><a href=\"view_book.php?pid={$row['book_id']}\">{$row['book_name']}</a></h1> <h2><a href=\"browse_books.php?aid={$row['author_id']}\">{$row['author']}</a></h2> <p>"; $newstring = substr("{$row['description']}",0,100); echo $newstring; echo "....<a href=\"view_book.php?pid={$row['book_id']}\"> more info</a>"; echo "</p> <h2>£{$row['price']}</h2> </div>"; } // End of while loop. mysqli_free_result ($r); mysqli_close($dbc); // Make the links to other pages, if necessary. if ($pages > 1) { // Add some spacing and start a paragraph: echo '<br /><p>'; // Determine what page the script is on: $current_page = ($start/$display) + 1; // If it's not the first page, make a Previous button: if ($current_page != 1) { echo '<a href="browse_books.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> '; } // Make all the numbered pages: for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="browse_books.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> '; } else { echo $i . ' '; } } // End of FOR loop. // If it's not the last page, make a Next button: if ($current_page != $pages) { echo '<a href="browse_books.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>'; } echo '</p>'; // Close the paragraph. } // End of links section. include ('includes/footer.html'); ?> Thanks in advance
×
×
  • Create New...