簡體   English   中英

如何從html導入兩個值到我的SQL表

[英]How to import two values from html to my SQL table

我需要從html導入emailaddress和fullname的值到我的SQL表中。

這是我的HTML:

<!DOCTYPE html>

    <head>
        <title>Julian's Newsletter</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />


        <link href="newsletter.css" rel="stylesheet" type="text/css">
            <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
        </head>
    <body>

        <h1>Newsletter</h1>
    <form action="formsubmit.php" method="post">
  <div class="container">
    <h2>Subscribe to my Newsletter</h2>
    <p>Subscribe to my newsletter to recieve recent news, a specialy curated product list, and the Product of the Month.</p>
  </div>

  <div class="container" style="background-color:white">
    <input type="text" placeholder="Name" name="fullname" required>
    <input type="text" placeholder="Email address" name="emailaddress" required>
    <label>
      <input type="checkbox" checked="checked" name="subscribe"> Monthly Newsletter
    </label>
  </div>

  <div class="container">
    <input type="submit" value="Subscribe">
  </div>
</form>
    </body>

到目前為止,這是我的PHP。 我是一個初學者,對PHP的了解很少。

    <?php
$servername = "localhost";
$emailaddress = "emailaddress";
$fullname = "fullname";
$dbname = "email_windowsisslow_com";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $fullname, $emailaddress);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO emaillist (emailaddress, fullname)
    VALUES ('', '')";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>

我是Stack Overflow的新手,我不認為任何人都會為我編寫代碼。 我需要幫助來了解所寫的內容以及如何編寫代碼以執行我需要的操作。

在您的PHP文件中,更改以下兩行:

$emailaddress = "emailaddress";
$fullname = "fullname";

$emailaddress = $_POST["emailaddress"];
$fullname = $_POST["fullname"];

並添加到您的插入語句

$sql = "INSERT INTO emaillist (emailaddress, fullname) VALUES ({$emailaddress}, {$fullname})";
I would suggest you must use prepared statements to avoid SQL injection. 

 $stmt = $conn->prepare("INSERT INTO emaillist (emailaddress, fullname)
        VALUES (:emailaddress , :fullname)");
    $stmt->bindParam(':emailaddress ', $emailaddress );
    $stmt->bindParam(':fullname ', $fullname );
    $stmt->execute();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM