×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Peter Hempel
Added: Sep 24, 2017 4:02 PM
Views: 6
datafield_01 "and" is_ID "in" the_table_in_the_database
  1. <?php // sqltest.php
  2.   require_once 'login.php';
  3.   $conn = new mysqli($hn, $un, $pw, $db);
  4.   if ($conn->connect_error) die($conn->connect_error);
  5.  
  6.   if (isset($_POST['delete']) && isset($_POST['is_ID']))
  7.   {
  8.     $is_ID   = get_post($conn, 'is_ID');
  9.     $query  = "DELETE FROM the_table_in_the_database WHERE is_ID='$is_ID'";
  10.     $result = $conn->query($query);
  11.         if (!$result) echo "DELETE failed: $query<br>" .
  12.       $conn->error . "<br><br>";
  13.   }
  14.  
  15.   if (isset($_POST['datafield_01'])   &&
  16.       isset($_POST['is_ID']))
  17.   {
  18.     $datafield_01   = get_post($conn, 'datafield_01');
  19.     $is_ID     = get_post($conn, 'is_ID');
  20.     $query    = "INSERT INTO the_table_in_the_database VALUES" .
  21.       "('$datafield_01', '$is_ID')";
  22.     $result   = $conn->query($query);
  23.  
  24.         if (!$result) echo "INSERT failed: $query<br>" .
  25.       $conn->error . "<br><br>";
  26.   }
  27.  
  28.   echo <<<_END
  29.   <form action="sqltest.php" method="post"><pre>
  30.     datafield_01 <input type="text" name="datafield_01">
  31.       is_ID <input type="text" name="is_ID">
  32.            <input type="submit" value="ADD RECORD">
  33.   </pre></form>
  34. _END;
  35.  
  36.   $query  = "SELECT * FROM the_table_in_the_database";
  37.   $result = $conn->query($query);
  38.   if (!$result) die ("Database access failed: " . $conn->error);
  39.  
  40.   $rows = $result->num_rows;
  41.  
  42.   for ($j = 0 ; $j < $rows ; ++$j)
  43.   {
  44.     $result->data_seek($j);
  45.     $row = $result->fetch_array(MYSQLI_NUM);
  46.  
  47.     echo <<<_END
  48.   <pre>
  49.     datafield_01 $row[0]
  50.          is_ID $row[1]
  51.   </pre>
  52.   <form action="sqltest.php" method="post">
  53.   <input type="hidden" name="delete" value="yes">
  54.   <input type="hidden" name="is_ID" value="$row[1]">
  55.   <input type="submit" value="DELETE RECORD"></form>
  56. _END;
  57.   }
  58.  
  59.   $result->close();
  60.   $conn->close();
  61.  
  62.   function get_post($conn, $var)
  63.   {
  64.     return $conn->real_escape_string($_POST[$var]);
  65.   }
  66. ?>
  67.