表单输入提交到DB,但不显示内容


form input submitting to DB, but not showing content

我有一些表单提交到我的数据库表,没有出现问题,但表单中的数据没有。因此,会生成新行,但它们是空白的。我在本地开发中使用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';
?>

非常感谢您的帮助。提前感谢!!!Dan

脚本有两个错误:

  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查询之前,还要对其进行转义,以防止注入攻击,请参阅此处:如何在PHP中防止SQL注入?

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