【问题标题】:How do you use php to insert into mysql database?你如何使用php插入mysql数据库?
【发布时间】:2016-11-10 04:29:01
【问题描述】:

所以我无法将数据连接或插入到我的数据库中。我正在使用表格来收集我需要的信息。然后我尝试插入我的数据库。我不确定问题是什么,但我认为我的连接有问题。我正在使用两个文件来尝试完成此操作。

addQuite.html 文件

<!DOCTYPE html>
<html>
<head>
    <title>AddQuote</title>
    <link href="styles.css" type="text/css" rel="stylesheet" />
</head>

 <body>

<h2> Add a Quote <h2>

    <form action="index.php" method="get">
        <div>
            Quote:<br>

            <textarea  rows="6" cols="60" name="quote" id="quote">

            </textarea>

            <br>

            Author: <input type="text" name="author" id="author"/> <br>

            <input class = "input2" type="submit" value="Save Quotation"/>
        </div>
    </form>
  </body>


   </html>

这是我的 index.php 文件,我正在尝试连接并插入到我的数据库中

  <!DOCTYPE html>
  <html>
    <head>
    <title>Quotes</title>
    <link href="styles.css" type="text/css" rel="stylesheet" />
    </head>

  <body>


<h1> Quotes </h1>

<form action="addQuote.html">
<input class="input1" type="submit" value="Add Quote"/>
</form>

<?php
//connet to server
$db = new PDO("mysql:host=server;dbname=quotes", "root" , "");

//check connections
if($db===false){
    die("ERROR: Could not connect. ");
}

//get name and quote
$name = $_GET['author'];
$quote = $_GET['quote'];

//attemp insert query execution
$sql = "INSERT INTO quotations (name, quote, rating ) VALUES 
('$name', '$quote', 0)";

?>

</body>


</html>

【问题讨论】:

  • 检查您的主机名,如果您在本地系统中运行代码,通常它是“localhost”。
  • 您当前的代码容易受到 sql 注入的攻击。您应该通过准备或逃避来确保不会发生这种情况

标签: php mysql forms post


【解决方案1】:

看看下面的例子

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('John', 'Doe', 'john@example.com')";
    // 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;
?>

【讨论】:

    【解决方案2】:

    您的查询永远不会执行,请将“$db->e​​xec($sql);”添加到您的代码中。

    <?php
    //connet to server
    $db = new PDO("mysql:host=server;dbname=quotes", "root" , "");
    
    //check connections
    if($db===false){
        die("ERROR: Could not connect. ");
    }
    
    //get name and quote
    $name = $_GET['author'];
    $quote = $_GET['quote'];
    
    //attemp insert query execution
    $sql = "INSERT INTO quotations (name, quote, rating ) VALUES 
    ('$name', '$quote', 0)";
    $db->exec($sql);
    ?>
    

    【讨论】:

      【解决方案3】:

      要使用 PDO 将数据插入 MySQL,您需要使用准备好的语句。 exec() 不应用于插入数据。 SQL查询需要单独参数化和数据传递。

      <?php
      
      //connet to server
      $options = [
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
          PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
          PDO::ATTR_EMULATE_PREPARES => false,
      ];
      $db = new PDO("mysql:host=server;dbname=quotes;charset=utf8mb4", "root", "", $options);
      
      //attempt insert query execution
      $db->prepare('INSERT INTO quotations(name, quote, rating ) VALUES(?,?,0)')
          ->execute([
              $_GET['author'], 
              $_GET['quote']
          ]);
      
      ?>
      

      您还应该在 PDO 中使用 PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION 启用异常,使用 utf8mb4 字符集,并且永远不要在 PHP 中使用没有密码的 root

      【讨论】:

        猜你喜欢
        • 2015-06-30
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-02
        • 1970-01-01
        • 2016-12-12
        • 2015-10-24
        相关资源
        最近更新 更多