【问题标题】:form input submitting to DB, but not showing content表单输入提交到数据库,但不显示内容
【发布时间】:2014-11-03 09:05:14
【问题描述】:

我有一些表单提交到我的数据库表而没有打嗝,但表单中的数据不是。因此,生成了新行,但它们是空白的。我在本地开发中使用 MAMP (Mac Apache MySQL PHP) 堆栈。

这是我的html页面的结尾举个例子。我已经包含了我的电子邮件表单和提交按钮。

<div class="col-md-4">
<div class="div1">
<h2>Step 6</h2><p>Email: REQUIRED </p>
<form action="upload_file.php" method="post">  
<input type="text" id="email" name="email" class="form-control" placeholder="kookmeyer@gmail.com"></br>
</form>
</div>
</div>


<div class="row">
<div class="col-md-3">
</div>
<div class="col-md-4">
<div class="submit">
<form action="upload_file.php" method="post"> 
<button type="submit" class="btn btn-primary btn-lg btn-block" name="submit" >Submit</button>
</form>
</div> <!--End div col-md-5-->
</div> <!--End div Submit-->

php页面(upload_file.php)是,

<?php
//connecting to db
$dbc = mysqli_connect('localhost', 'root', 'root', 'surfboardhub')
or die('Error connecting to MySQL server');
//Get values from  
$email = "";
$brand = "";
$model = "";
$height ="";
$width = "";
$thick = "";
$price = "";
$location = "";
if(isset($_POST['location'])){ $location = $_POST['location']; }
if(isset($_POST['price'])){ $price = $_POST['price']; }
if(isset($_POST['thick'])){ $thick = $_POST['thick']; }
if(isset($_POST['width'])){ $width = $_POST['width']; }
if(isset($_POST['height'])){ $height = $_POST['height']; }
if(isset($_POST['model'])){ $model = $_POST['model']; }
if(isset($_POST['brand'])){ $brand = $_POST['brand']; }
if(isset($_POST['email'])){ $email = $_POST['email']; }  


$query = "INSERT INTO uploads (brand, model, height, width, thick, price, location, email) 
VALUES ('$brand', '$model', '$height', '$width', '$thick', '$price', '$location', '$email')";


$result = mysqli_query($dbc,$query)
or die('Error querying database.');

mysqli_close($dbc);


echo 'Thanks for submitting your stick! An email has been sent to you with a link to your ad!<br    />';
echo 'Clicking here will send you back to the homepage';

?>

非常感谢任何帮助。提前致谢!!! 丹

【问题讨论】:

    标签: php jquery mysql forms phpmyadmin


    【解决方案1】:

    你的脚本有两个问题:

    1. 您要发送到 Web 服务器的所有内容都必须位于同一个表单容器中。
    2. 您的提交按钮必须是类型为 submitinput,而不是 button

    HTML

    <form action="upload_file.php" method="post"> 
        <input type="text" id="email" name="email" class="form-control" placeholder="kookmeyer@gmail.com" />
        <input type="submit" class="btn btn-primary btn-lg btn-block" name="submit" value="Submit" />
    </form>
    

    还要在将变量插入 SQL 查询之前对其进行转义以防止注入攻击,请参见此处:How can I prevent SQL injection in PHP?

    【讨论】:

    • 感谢您的建议。但是我可以将 div 类放在表单容器中吗?
    【解决方案2】:

    在您的提交表单中,您只有提交按钮。因此,到达您的脚本 upload_file.php 的唯一 POST 变量是 $_POST['submit']。

    【讨论】:

      猜你喜欢
      • 2021-06-02
      • 2015-06-28
      • 1970-01-01
      • 1970-01-01
      • 2020-02-29
      • 1970-01-01
      • 2016-07-23
      • 2012-03-20
      • 1970-01-01
      相关资源
      最近更新 更多