Advertisement
Guest User

Untitled

a guest
May 12th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.85 KB | None | 0 0
  1. <?php
  2. /*Program:  mysql_send.php
  3.  *Desc:     PHP program that sends an SQL query to the
  4.  *          MySQL server and displays the results.
  5.  */
  6. echo "<html>
  7.      <head><title>SQL Query Sender</title></head>
  8.      <body>";
  9. if(ini_get("magic_quotes_gpc") == "1")
  10. {
  11.    $_POST['query'] = stripslashes($_POST['query']);
  12. }
  13. $host="localhost";
  14. $user="";
  15. $password="";
  16.  
  17. /* Section that executes query and displays the results */
  18. if(!empty($_POST['form']))
  19. {
  20.   $cxn = mysqli_connect($host,$user,$password,
  21.                         $_POST['database']);
  22.   $result = mysqli_query($cxn,$_POST['query']);
  23.   echo "Database Selected: <b>{$_POST['database']}</b><br>
  24.        Query: <b>{$_POST['query']}</b>
  25.        <h3>Results</h3><hr>";
  26.   if($result == false)
  27.   {
  28.      echo "<h4>Error: ".mysqli_error($cxn)."</h4>";
  29.   }
  30.   elseif(@mysqli_num_rows($result) == 0)
  31.   {
  32.      echo "<h4>Query completed.
  33.            No results returned.</h4>";
  34.   }
  35.   else
  36.   {
  37.    /* Display results */
  38.      echo "<table border='1'><thead><tr>";
  39.      $finfo = mysqli_fetch_fields($result);
  40.      foreach($finfo as $field)
  41.      {
  42.         echo "<th>".$field->name."</th>";
  43.      }
  44.      echo "</tr></thead>
  45.           <tbody>";
  46.      for ($i=0;$i < mysqli_num_rows($result);$i++)
  47.      {
  48.         echo "<tr>";
  49.         $row = mysqli_fetch_row($result);
  50.         foreach($row as $value)
  51.         {
  52.            echo "<td>".$value."</td>";
  53.         }
  54.         echo "</tr>";
  55.      }
  56.      echo "</tbody></table>";
  57.   }
  58.  /* Display form with only buttons after results */
  59.   $query = str_replace("'","%&%",$_POST['query']);
  60.   echo "<hr><br>
  61.      <form action='{$_SERVER['PHP_SELF']}' method='POST'>
  62.        <input type='hidden' name='query' value='$query'>
  63.        <input type='hidden' name='database'
  64.               value={$_POST['database']}>
  65.        <input type='submit' name='queryButton'
  66.               value='New Query'>
  67.        <input type='submit' name='queryButton'
  68.               value='Edit Query'>
  69.      </form>";
  70.   exit();
  71. }
  72.  
  73. /* Displays form for query input */
  74. if (@$_POST['queryButton'] != "Edit Query")
  75. {
  76.    $query = " ";
  77. }
  78. else
  79. {
  80.    $query = str_replace("%&%","'",$_POST['query']);
  81. }
  82. ?>
  83. <form action="<?php echo $_SERVER['PHP_SELF'] ?>"
  84.       method="POST">
  85. <table>
  86.  <tr><td style='text-align: right; font-weight: bold'>
  87.          Type in database name</td>
  88.      <td><input type="text" name="database"
  89.             value=<?php echo @$_POST['database'] ?> ></td>
  90.  </tr>
  91.  <tr><td style='text-align: right; font-weight: bold'
  92.          valign="top">Type in SQL query</td>
  93.      <td><textarea name="query" cols="60"
  94.             rows="10"><?php echo $query ?></textarea></td>
  95.  </tr>
  96.  <tr><td colspan="2" style='text-align: center'>
  97.         <input type="submit" value="Submit Query"></td>
  98.  </tr>
  99. </table>
  100. <input type="hidden" name="form" value="yes">
  101. </form>
  102. </body></html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement