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

时间:2014-09-09 20:35:25

标签: php jquery mysql forms phpmyadmin

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

?>

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

2 个答案:

答案 0 :(得分:0)

在提交表单中,您只有提交按钮。所以到达你的脚本upload_file.php的唯一POST var是$ _POST ['submit']。

答案 1 :(得分:0)

您的脚本有两个错误:

  1. 您要发送到Web服务器的所有内容必须位于相同的表单容器中。
  2. 您的提交按钮必须是input,其类型为提交,而不是button
  3. <强> 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?