Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.85 KB | None | 0 0
  1. // db_connection.php
  2. <?php
  3.   // Connect
  4.   $server = 'hostname';
  5.   $username = 'username';
  6.   $password = 'password';
  7.   mysql_connect($server, $username, $password);
  8.  
  9.   // Select database
  10.   mysql_select_db('discography');
  11.  
  12.  
  13. // addalbum.php
  14. <?php
  15.  
  16. $error = FALSE;
  17.  
  18. if($_POST)
  19. {
  20.   // Trim album name (take away spaces at the ends)
  21.   $album_name = trim($_POST['album_name']);
  22.  
  23.   // Check that it is not empty or above 150 characters
  24.   if( ! $album_name OR strlen(album_name) > 150)
  25.     $error = 'Album name must not be empty or longer than 150 characters';
  26.  
  27.   // If no errors
  28.   if($error === FALSE)
  29.   {
  30.     // Connect to database
  31.     require('db_connection.php');
  32.  
  33.     // Escape album name (so people can't mess up your db query)
  34.     $album_name = mysql_real_escape_string($album_name);
  35.  
  36.     // Try insert album
  37.     if( ! mysql_query("INSERT INTO albums(`name`) VALUES ('$album_name')"))
  38.     {
  39.       // Get the error to display
  40.       $error = mysql_error();
  41.     }
  42.     else
  43.     {
  44.       // Get the id of the new album
  45.       $id = mysql_insert_id();
  46.       // Redirect and exit this script
  47.       header("Location: addedalbum.php?id=$id");
  48.       exit();
  49.     }
  50. }
  51. ?>
  52.  
  53. <!-- HTML head and all that -->
  54.  
  55. <?php if($error) echo "<p>$error</p>"; ?>
  56.  
  57. <!-- Form -->
  58.  
  59. <input name="album_name" value="<?php echo $_POST['album_name'] ?>" />
  60.  
  61. <!-- rest of page -->
  62.  
  63.  
  64. // addedalbum.php
  65.  
  66. <?php
  67.  
  68. // Get the $id and make sure it is a number
  69. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  70.  
  71. // Connect to database
  72. require('db_connection.php');
  73.  
  74. // Run query to get the album name
  75. $result = mysql_query("SELECT name FROM albums WHERE id=$id LIMIT 1");
  76.  
  77. if($result === FALSE OR mysql_num_rows !== 1)
  78. {
  79.   exit('No such album');
  80. }
  81.  
  82. // Get the album name
  83. $album_name = mysql_result($result, 0, 'name');
  84. ?>
  85.  
  86. <!-- HTML and such would go here -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement