Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Pertahankan nilai bidang setelah dikirim

Ya, Anda dapat menerapkan (semacam) sesi flash dalam kasus ini:

Jadi setelah pengiriman:

<?php

session_start(); // don't forget
require_once('../scripts/connect.php');

$con = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME) or die('Could not connect to database server.');

if(isset($_POST['submit'])) {

    $var_Email1 = mysqli_real_escape_string($con, $_POST['field_email1']);
    $var_Email2 = mysqli_real_escape_string($con, $_POST['field_email2']);

    if ($var_Email1 == $var_Email2){

        $sql = mysqli_query($con, "SELECT * FROM membership WHERE Email = '$var_Email1' "); 
        if(mysqli_num_rows($sql) > 0){
            // set session
            $_SESSION['email_exists'] = $var_Email1;
            header('Location: the_starting_php.php');
            exit;
        }
    }

    echo "not in database";

}
?>

Dan kemudian di halaman formulir tambahkan ini juga:

<?php
session_start();

// check if there is
$email = '';
if(isset($_SESSION['email_exists'])) {
    $email = $_SESSION['email_exists'];
    unset($_SESSION['email_exists']); // unset it
    echo "
      <script type='text/javascript'>
      alert('The email address $email already exists');
      </script>
    ";
}

?>


<form action="register_script2.php" method="POST" name="form_register" id="form_registerID" accept-charset="UTF-8">

    <aside class="field_reg_form">
          <input name="field_email1" type="text" required id="field_email1ID" value="<?php echo $email; ?>" />
          <br /><br />
          <input name="field_email2" type="text" required id="field_email2ID" value="<?php echo $email; ?>" />
          <br /><br />
          <input type="submit" value="submit" id="submit" name="submit" />
    </aside>

</form>

Sidenote:Saya sarankan menggunakan pernyataan yang sudah disiapkan.

<?php

session_start(); // don't forget
require_once('../scripts/connect.php');

$con = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME) or die('Could not connect to database server.');

if(isset($_POST['submit'])) {

    $var_Email1 = $_POST['field_email1'];
    $var_Email2 = $_POST['field_email2'];

    if ($var_Email1 == $var_Email2){

        $sql = 'SELECT * FROM membership WHERE Email = ?'; 
        $select = $con->prepare($sql);
        $select->bind_param('s', $var_Email1);
        $select->execute();
        if($select->num_rows > 0){
            // set session
            $_SESSION['email_exists'] = $var_Email1;
            header('Location: the_starting_php.php');
            exit;
        }
    }

    echo "not in database";

}
?>


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Apa pentingnya nama indeks saat membuat indeks di MySQL?

  2. Backup Best Practices untuk MySQL, MariaDB dan Galera Cluster

  3. CodeIgniter Pilih Pernyataan dengan klausa Where

  4. Perilaku aneh MySQL untuk operator perbandingan (!=/<>) yaitu NotEqualTo

  5. MySQL:Berikan **semua** hak istimewa pada database