bu2chlc

(safe) page for entering image url information

Nov 16th, 2019
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.64 KB | None | 0 0
  1. <?php
  2.  // session, formatting, etc.
  3.  include('header.php');
  4.  
  5.  // connect to database, DRY (Dont Repeat Yourself) code
  6.  include('db.php');
  7.  
  8.  // is there a post?
  9.  if( isset($_POST['title']) AND isset($_POST['url']) )
  10.   {
  11.     if( $_POST['title']<>'' AND $_POST['url']<>'')
  12.     {
  13.       // assign title and url variables
  14.       $title=$_POST['title'];
  15.       $url=$_POST['url'];
  16.      
  17.      // validation, sanitation
  18.      // is $url an url?... etc.
  19.      // is $title a string?... etc.
  20.      
  21.       // escaping
  22.      $title=mysqli_real_escape_string($conn,$title);
  23.      $url=mysqli_real_escape_string($conn,$url);    
  24.  
  25.       // the insert query
  26.      $sql_insert = "INSERT INTO urls (id, title, url) VALUES (NULL, '$title', '$url')";
  27.        echo $sql_insert . "<br>";
  28.       // insert image into database:
  29.       if(mysqli_query($conn, $sql_insert))
  30.       {
  31.         echo "string stored successfully";
  32.       } else {
  33.         printf("Error message: %s\n", mysqli_error($conn));
  34.       }
  35.     } else {
  36.       echo 'All the fields are required<br>';
  37.     }
  38.   }
  39.  
  40.   // show the form.
  41.  ?>
  42.  <!DOCTYPE html>
  43.  <html lang="en">
  44.  <head>
  45.    <meta charset="UTF-8">
  46.    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  47.    <meta http-equiv="X-UA-Compatible" content="ie=edge">
  48.    <title>Escaped entries</title>
  49.  </head>
  50.  <body>
  51.    
  52.  </body>
  53.  </html>
  54.   <form action="form.php" method="post" >
  55.     <label for="">title</label>
  56.     <input type="text" name="title" REQUIRED AUTOFOCUS ><br>
  57.    
  58.     <label for="">URL</label>
  59.     <input type="text" name="url" REQUIRED ><br>
  60.    
  61.     <input type="submit" value="submit" >
  62.   </form>
Advertisement
Add Comment
Please, Sign In to add comment