【问题标题】:Adding my form data to the database using PHP使用 PHP 将我的表单数据添加到数据库
【发布时间】:2014-01-15 00:22:26
【问题描述】:

您好,我正在努力让我的表单将其数据发布到 MySQL 数据库。

我有一个这样的配置文件设置:

 // server info
 $server = 'localhost';
 $user = 'root';
 $pass = '';
 $db = 'cms';

 // connect to the database
 $mysqli = new mysqli($server, $user, $pass, $db);

 // show errors (remove this line if on a live site)
 mysqli_report(MYSQLI_REPORT_ERROR);

表单php页面:

 <?php
 include("config_database.php")
  ?>


 <?php
 include("addproduct.php")
  ?>


<article>

  <section class="Input">

     <fieldset><legend><span> Add a product to the database </span></legend>


         <form action="addproduct.php" method="post">
          <label> product name: </label><input type="text" name="name"><br />
          <label> product quantity: </label><input type="text" name="quantity"><br />
          <label> product description: </label><input type="text" name="description"><br />                    
           <label> product price: </label><input type="text" name="price"><br />
           <input type="submit" class="reg">
           </form>

然后是一个“addproduct.php”,希望将数据发送到数据库:

   require_once ("config_database.php");

    if (isset($_POST['name']) &&
    !empty($_POST["name"]) &&


isset($_POST['quantity']) &&
    !empty($_POST["quantity"]) &&


isset($_POST['description']) &&
    !empty($_POST["description"]) &&


isset($_POST['price']) &&
    !empty($_POST["price"]))

 {
$name      = get_post('name');
$quantity    = get_post('quantity');
$description = get_post('description');
$price = get_post('price');



 $query = "INSERT INTO products VALUES" .
    "('', '$name', '$quantity', '$description', '$price')";


}
function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}

如何将表单中输入的数据导入数据库?

【问题讨论】:

  • 苦苦挣扎?这是一个非常模糊的描述 IMO。此外,您的 $query 只是一个字符串。它需要提供给查询方法。

标签: php html forms mysqli


【解决方案1】:

您尝试使用 mysqli 进行这样的插入查询。

$query = "INSERT INTO products VALUES (NULL, '$name','$quantity', '$description','$price')";
$mysqli->query($query);

并为 mysqli 使用像这样的真正转义字符串。

$mysqli->real_escape_string($_POST[$var]);

【讨论】:

    猜你喜欢
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 2015-05-21
    • 2021-04-16
    • 2011-05-11
    • 1970-01-01
    相关资源
    最近更新 更多