亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP 表單不會插入到 SQL 數據庫中

PHP 表單不會插入到 SQL 數據庫中

PHP
開心每一天1111 2022-09-12 10:12:45
我正在嘗試測試一個非常簡單的PHP表單,它將輸入插入到SQL數據庫中。連接工作正常,但刷新數據庫時數據未出現在數據庫中。我只有兩個文件,一個索引.html和一個進程.php。索引.html:<html><head>Testing</head><body>    <div id="frm">        <form action="process.php" method=POST>            <p>                <label>Username</label>                <input  type="text" id="stuff" name="stuff">            </p>            <p>                <input type="submit" id="btn" value="Login">            </p>        </form>    </div></body></html>過程.php:<?php    $userinput = $_POST['stuff'];    $servername = "localhost";    $username = "root";    $password = "";    $database = "testing";    $conn = new mysqli($servername, $username, $password, $database);    if ($conn->connect_error)    {        die("connection failed: " . $conn->connect_error);    }    else    {        echo "Connected successfully ";         echo $userinput;        $sql = "INSERT INTO `entries`(`input`) VALUES ('$userinput')";    }?>
查看完整描述

1 回答

?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

問題是您實際上并沒有運行查詢。您剛剛將查詢字符串分配給變量,因此它不會在MySQL中執行。


您的代碼容易受到SQL注入的攻擊,因此我提出了一個解決方案:


<?php

$userinput = $_POST['stuff'];

$servername = "localhost";

$username = "root";

$password = "";

$database = "testing";


$conn = new mysqli($servername, $username, $password, $database);


if ($conn->connect_error)

{

    die("connection failed: " . $conn->connect_error);

}

else

{

    echo "Connected successfully "; 

    echo $userinput;

    $sql = "INSERT INTO `entries` (`input`) VALUES (?)";

    if ($stmt = $conn->prepare($sql)) { // Prepare statement

        $stmt->bind_param("s", $userinput); //Bind the string (s), with the content from $userinput to the statement marker (?)

        $stmt->execute(); // Run (execute) the query

        $stmt->close(); //clean up

}

此代碼應該有效,并且還可以保護您免受SQL注入的影響。


查看完整回答
反對 回復 2022-09-12
  • 1 回答
  • 0 關注
  • 130 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號