Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.70 KB | None | 0 0
  1. <?php
  2.  
  3. //Sanitize input box
  4. $selectInput= htmlspecialchars(strip_tags(trim($_POST['selectInput'])), ENT_QUOTES);
  5. $fromInput= htmlspecialchars(strip_tags(trim($_POST['fromInput'])), ENT_QUOTES);
  6. $whereInput= htmlspecialchars(strip_tags(trim($_POST['whereInput'])), ENT_QUOTES);
  7. $likeInput= htmlspecialchars(strip_tags(trim($_POST['likeInput'])), ENT_NOQUOTES);
  8.  
  9.  
  10. if ( empty($selectInput) || empty($fromInput) || empty($whereInput) || empty($likeInput)){
  11.     echo "Empty fields";
  12.     //also kill the db connection here
  13.     die();
  14. }
  15.  
  16. $likeInput = str_replace(array("\"", "'"), "", $likeInput);
  17.  
  18. $upperVerbs = '/\bcreate\b|\bdrop\b|\balter\b|\bshow\b|\binsert\b|\bload\b|\bselect|b|\bupdate\b|\bdelete\b|;/';
  19. $lowerVerbs = '/\bcreate\b|\bdrop\b|\balter\b|\bshow\b|\binsert\b|\bload\b|\bselect|b|\bupdate\b|\bdelete\b|;/';
  20. //$emptyString = '/""/';
  21.  
  22.  
  23. preg_replace($upperVerbs, '/""/', $selectInput);
  24. preg_replace($lowerVerbs, '/""/', $selectInput);
  25. preg_replace($upperVerbs, '/""/', $fromInput);
  26. preg_replace($lowerVerbs, '/""/', $fromInput);
  27. preg_replace($upperVerbs, '/""/', $whereInput);
  28. preg_replace($lowerVerbs, '/""/', $whereInput);
  29. preg_replace($upperVerbs, '/""/', $likeInput);
  30. preg_replace($lowerVerbs, '/""/', $likeInput);
  31.  
  32.  
  33. require ("../../database.inc.php");
  34.    // Connect to the MySQL server.
  35.    $LinkID = mysqli_connect($host, $user, $password);
  36.    // Die if no connect
  37.    if (!$LinkID) {
  38.       die('Could not connect: ' . mysqli_error($LinkID));
  39.    }
  40.    // Choose the DB and run a query.
  41.    mysqli_select_db($LinkID, "comp170");
  42.  
  43.  
  44.    
  45.    
  46. $queryResult = mysqli_query($LinkID, "SELECT $selectInput FROM $fromInput WHERE $whereInput LIKE '$likeInput'");
  47.  
  48. echo mysqli_error($LinkID);
  49.  
  50. if ($queryResult){
  51.     $match = mysqli_fetch_assoc($queryResult);
  52.     if (empty($match)) {
  53.         echo "Empty result set";
  54.         //also kill the db connection here
  55.         die();
  56.     }  
  57. }else{
  58.     die();
  59. }
  60. mysqli_data_seek ($queryResult, 0);
  61.  
  62. $match=mysqli_fetch_assoc($queryResult); // Print the column labels
  63.       print "<p> Your Query </p>";
  64.       print "<table border=1><tr>";
  65.       foreach (array_keys($match) as $data) {
  66.          print "<td><b>$data</b></td>";
  67.       }
  68.       print "</tr><tr>";
  69.      
  70.      
  71.     // Print the values for the first row
  72.       foreach ($match as $otherData) {
  73.          print "<td>$otherData</td>";
  74.       }
  75.       print "</tr><tr>";
  76.      
  77.      
  78.     // Print the rest of the rows.
  79.       while ($match=mysqli_fetch_row($queryResult)) {
  80.          foreach ($match as $otherData) {
  81.             print "<td>$otherData</td>";
  82.          }
  83.          print "</tr><tr>";
  84.       }
  85. ?>   
  86. </tr></table></body></html>
  87. //Sanitize inputs //Get input from user //Display result of querys chosen based on choice
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement