Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

प्राथमिक कुंजी के अनुसार html फॉर्म को php पेज से जोड़ना

सबसे पहले, यदि वह कोड उत्पादन में उपयोग किया जाना है, तो कृपया सुनिश्चित करें कि आप अपने कथन में प्लग इन करने से पहले अपने SQL पैरामीटर से बच रहे हैं। कोई भी SQL इंजेक्शन हमले का आनंद नहीं लेता है। मैं इसके बजाय पीडीओ का उपयोग करने की सलाह दूंगा क्योंकि यह तैयार बयानों और पैरामीटर बाइंडिंग का समर्थन करता है जो कि अधिक सुरक्षित है।

मैं PHP में SQL इंजेक्शन को कैसे रोक सकता हूं?

तो आपके पास एक फॉर्म है...

[title]

[details]

[submit]

और वह आपके डेटाबेस में सम्मिलित हो जाता है...

INSERT INTO questions (title, details) VALUES (?, ?)

आप mysql_insert_id . का उपयोग करके अंतिम इंसर्ट आईडी प्राप्त कर सकते हैं , http://php.net/manual/en/function। mysql-insert-id.php

$id = mysql_insert_id();

तब आप रिकॉर्ड प्राप्त कर सकते हैं...

SELECT title, details FROM questions WHERE id = ?

और इसे एक पूर्वावलोकन पृष्ठ में आउटपुट करें।

मैंने मूल mysql फ़ंक्शन के बजाय PDO का उपयोग करके एक उदाहरण लिखा है।

form.php :

<form action="process.php" method="post">
    <label for="question_title">Title</label>
    <input id="question_title" name="title"/>
    <label for="question_detail">Detail</label>
    <input id="question_detail" name="detail"/>
    <button type="submit">Submit</button>
</form>

process.php :

<?php

// Create a database connection
$pdo = new PDO("mysql:dbname=test");
// Prepare the insert statement and bind parameters
$stmt = $pdo->prepare("INSERT INTO questions (title, detail) VALUES (?, ?)");
$stmt->bindValue(1, $_POST["title"], PDO::PARAM_STR);
$stmt->bindValue(2, $_POST["detail"], PDO::PARAM_STR);
// Execute the insert statement
$stmt->execute();
// Retrieve the id
$id = $stmt->lastInsertId();

// Prepare a select statement and bind the id parameter
$stmt = $pdo->prepare("SELECT title, detail FROM questions WHERE id = ?");
$stmt->bindValue(1, $id, PDO::PARAM_INT);
// Execute the select statement
$stmt->execute();
// Retrieve the record as an associative array
$row = $stmt->fetch(PDO::FETCH_ASSOC);

?>

<h1><?php echo htmlspecialchars($row["title"]);?></h1>
<p><?php echo htmlspecialchars($row["detail"]);?></p>

पीडीओ के बिना...

form.php :

<form action="process.php" method="post">
    <label for="question_title">Title</label>
    <input id="question_title" name="title"/>
    <label for="question_detail">Detail</label>
    <input id="question_detail" name="detail"/>
    <button type="submit">Submit</button>
</form>

process.php :

<?php

// Create a database connection
$conn = mysql_connect();
// Execute the insert statement safely
mysql_query("INSERT INTO questions (title, detail) VALUES ('" . 
    mysql_real_escape_string($_POST["title"]) . "','" .
    mysql_real_escape_string($_POST["detail"]) . "')", $conn);
// Retrieve the id
$id = mysql_insert_id($conn);
// Close the connection
mysql_close($conn);

header("Location: question_preview.php?id=$id");

question_preview.php :

<?php

// Create a database connection
$conn = mysql_connect();
// Execute a select statement safely
$result = mysql_query("SELECT title, detail FROM questions WHERE id = " .
    mysql_real_escape_string($_GET["id"]), $conn);
// Retrieve the record as an associative array
$row = mysql_fetch_assoc($result);
// Close the connection
mysql_close($conn);

?>

<h1><?php echo htmlspecialchars($row["title"]);?></h1>
<p><?php echo htmlspecialchars($row["detail"]);?></p>


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. त्रुटि:अपस्ट्रीम [uWSGI/Django/NGINX] से प्रतिक्रिया शीर्षलेख पढ़ते समय समय से पहले बंद कनेक्शन

  2. MySQL प्रदर्शन धोखा पत्र

  3. स्ट्रीमिंग बड़े परिणाम MySQL के साथ सेट होते हैं

  4. MySQL:पसंद नहीं है

  5. भ्रष्ट xampp 'mysql.user' तालिका की मरम्मत कैसे करें?