dongnianwo8289 2014-09-09 20:35
浏览 26
已采纳

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

I have some forms submitting to my db table without hiccups but the data within the forms is not. Thus, new rows are generated but they are blank. I am using MAMP (Mac Apache MySQL PHP) stack in local development.

This is the end of my html page to give an example. I've included my email form and submit button.

<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-->

The php page (upload_file.php) is,

<?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';

?>

Any help is much appreciated. Thanks in advance!!! Dan

  • 写回答

2条回答 默认 最新

  • douji6667 2014-09-09 22:14
    关注

    Two things that are wrong with your scripts:

    1. Everything you want to send to your web server must be in the same form container.
    2. Your submit button must be an input with the type submit instead of a 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>
    

    Also escape your variables before inserting them into your SQL query to prevent injection attacks, see here: How can I prevent SQL injection in PHP?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错