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

Canonical:Bagaimana cara menyimpan data formulir HTML ke dalam database MySQL

Pertama-tama halaman PHP atau HTML Anda harus menghasilkan formulir yang dapat berinteraksi dengan pengguna. Dalam bentuk yang paling sederhana, itu akan menjadi seperti:

<html>
<body>
 <form method="post" action="yourscript.php">
  <input type="text" name="yourfield">
  <input type="submit" name="youraction" value="save">
 </form>
</body>
</html>

Ini akan memberi pengguna Anda formulir sederhana dengan bidang input tunggal dan tombol 'simpan'. Setelah mengklik tombol 'simpan' untuk konten akan dikirim ke 'yourscript.php' Anda menggunakan POST metode.

yourscript.php harus menerapkan hal berikut:

  1. Terima dan proses masukan dari formulir Anda.
  2. Hubungkan ke database MySQL Anda.
  3. Simpan ke dalam database.

Dalam bentuk yang paling sederhana, ini akan menjadi:

<!doctype html>
<html>
 <head>
  <title>Process and store</title>
 </head>
<body>
<?php

// Check that user sent some data to begin with. 
if (isset($_REQUEST['yourfield'])) {

    /* Sanitize input. Trust *nothing* sent by the client.
     * When possible use whitelisting, only allow characters that you know
     * are needed. If username must contain only alphanumeric characters,
     * without puntation, then you should not accept anything else.
     * For more details, see: https://stackoverflow.com/a/10094315
     */
    $yourfield=preg_replace('/[^a-zA-Z0-9\ ]/','',$_REQUEST['yourfield']);

    /* Escape your input: use htmlspecialchars to avoid most obvious XSS attacks.
     * Note: Your application may still be vulnerable to XSS if you use $yourfield
     *       in an attribute without proper quoting.
     * For more details, see: https://stackoverflow.com/a/130323
     */
    $yourfield=htmlspecialchars($yourfield);


} else {
    die('User did not send any data to be saved!');
}


// Define MySQL connection and credentials
$pdo_dsn='mysql:dbname=yourdatabase;host=databasehost.example.com';
$pdo_user='yourdatabaseuser';     
$pdo_password='yourdatabaspassword';  

try {
    // Establish connection to database
    $conn = new PDO($pdo_dsn, $pdo_user, $pdo_password);

    // Throw exceptions in case of error.
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // Use prepared statements to mitigate SQL injection attacks.
    // See https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php for more details
    $qry=$conn->prepare('INSERT INTO yourtable (yourcolumn) VALUES (:yourvalue)');

    // Execute the prepared statement using user supplied data.
    $qry->execute(Array(":yourvalue" => $yourfield));

} catch (PDOException $e) {
    echo 'Error: ' . $e->getMessage() . " file: " . $e->getFile() . " line: " . $e->getLine();
    exit;
}
?>
<form method="post">

 <!-- Please note that the quotes around next <?php ... ?> block are important
      to avoid XSS issues with poorly escaped user input. For more details:
      https://stackoverflow.com/a/2894530
  -->
 <input type="text" name="yourfield" value="<?php print $yourfield; ?>">
 <input type="submit" name="youraction" value="save">
</form>

</body>
</html>

Takeaway utama di sini adalah menggunakan pernyataan yang disiapkan untuk menghindari serangan injeksi SQL .




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Menggunakan pdo di php dengan prosedur tersimpan

  2. Cara Membuat Database di MySQL

  3. SQL Query untuk menemukan jumlah teman bersama yang diberikan tabel orang, pasangan teman

  4. Menghasilkan berbagai angka di MySQL

  5. PHP Mengurutkan koordinat terdekat