表单未提交给MySQL数据库

时间:2013-11-18 18:07:26

标签: php html mysql forms

我在通过php表单向mysql发送数据时遇到问题。

这是我的表格(info.php):

<form action="send.php" class="form-horizontal" method="post" name="application">
<div class="col-md-6">
<select class="form-control" name="title">
<option>Please select </option>
<option>Mr</option>
<option>Mrs</option>
<option>Miss</option>
<option>Ms</option>
</select><br>
<input type="text" class="form-control" placeholder="First name" name="first_name"><br>
<input type="text" class="form-control" placeholder="Surname" name="surname"><br>
Date of birth: <input type ="date" name="dob" class="form-control"><hr>
<h3>Address</h3>
<input type="text" class="form-control" placeholder="House name/ House number"     name="house"><br>
<input type="text" class="form-control" placeholder="Street Name" name="street"><br>
<input type="text" class="form-control" placeholder="Town" name="town"><br>
<input type="text" class="form-control" placeholder="Postcode" name="postcode"><br>
<hr></div>
<div class="col-md-6">
<input type="text" class="form-control" placeholder="Country of birth" name="origin"><br>
<input type="text" class="form-control" placeholder="Mobile/Phone number" name="number">       
<input type="text" class="form-control" placeholder="Email address" name="email"><br>
<input type="hidden" name="plan" value="<? $plan; ?>">
<input type="submit" class="btn btn-success btn-lg" value="Submit application">
<hr>
</form>

这是send.php:

<?php

 $host="localhost"; // Host name 
 $username="******"; // Mysql username 
 $password="******"; // Mysql password 
 $db_name="*****"; // Database name 
 $tbl_name="*****"; // Table name 

 // Connect to server and select database.
 mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");

 // Get values from form 
 $title=$_POST['title'];
 $first_name=$_POST['first_name'];
 $surname=$_POST['surname'];
 $dob=$_POST["dob'];
 $house=$_POST["house'];
 $street=$_POST["street'];
 $town=$_POST["town'];
 $postcode=$_POST["postcode'];
 $origin=$_POST["origin'];
 $number=$_POST["number'];
 $email=$_POST["email'];
 $plan=$_POST["plan'];



 // Insert data into mysql 
 $sql="INSERT INTO $tbl_name(' ',title, first_name, surname, dob, house, street, town,                postcode, origin, number, email, plan)VALUES(' ','$title', '$first_name', '$surname', '$dob',  '$house', '$street', '$town', '$postcode', '$origin', '$number', 
'$email', '$plan')";
 $sql=mysql_real_escape_string($sql);
 $result=mysql_query($sql);

 // if successfully insert data into database, displays message "Successful". 
 if($result){
 echo "Successful";
 echo "<BR>";
 echo "<a href='insert.php'>Back to main page</a>";
 }

 else {
 echo "ERROR";
 }
 ?> 

 <?php 
 // close connection 
 mysql_close();
 ?>

当我将表单提交给send.php时,页面变为空白。这是否意味着我的数据库身份验证有问题?

提前致谢。

4 个答案:

答案 0 :(得分:1)

您的问题是,您实际上从未关闭过大部分报价。您的代码应如下所示:

<?php

 $host="localhost"; // Host name 
 $username="******"; // Mysql username 
 $password="******"; // Mysql password 
 $db_name="*****"; // Database name 
 $tbl_name="*****"; // Table name 

 // Connect to server and select database.
 mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");

 // Get values from form 
 $title=$_POST['title'];
 $first_name=$_POST['first_name'];
 $surname=$_POST['surname'];
 $dob=$_POST['dob'];
 $house=$_POST['house'];
 $street=$_POST['street'];
 $town=$_POST['town'];
 $postcode=$_POST['postcode'];
 $origin=$_POST['origin'];
 $number=$_POST['number'];
 $email=$_POST['email'];
 $plan=$_POST['plan'];



 // Insert data into mysql 
 $sql="INSERT INTO $tbl_name(' ',title, first_name, surname, dob, house, street, town,                postcode, origin, number, email, plan)VALUES(' ','$title', '$first_name', '$surname', '$dob',  '$house', '$street', '$town', '$postcode', '$origin', '$number', 
'$email', '$plan')";
 $sql = mysql_real_escape_string($sql);
 $result=mysql_query($sql);

 // if successfully insert data into database, displays message "Successful". 
 if($result){
 echo "Successful";
 echo "<BR>";
 echo "<a href='insert.php'>Back to main page</a>";
 }

 else {
 echo "ERROR";
 }
 ?> 

 <?php 
 // close connection 
 mysql_close();
 ?>

答案 1 :(得分:0)

试试这个:

<form action="send.php" class="form-horizontal" method="post" name="application">
<div class="col-md-6">
  <p>
  <select class="form-control" name="title">
    <option value="" disabled>Please select </option>
    <option value="Mr">Mr</option>
    <option value="Mrs">Mrs</option>
    <option value="Miss">Miss</option>
    <option value="Ms">Ms</option>
  </select>
  </p>
  <p><br>
    <input type="text" class="form-control" placeholder="First name" name="first_name">
  </p>
  <p><br>
    <input type="text" class="form-control" placeholder="Surname" name="surname">
  </p>
  <p>Date of birth:<br><input type ="date" name="dob" class="form-control">
  </p>
</div><hr>
<h3>Address</h3>
<p>
  <input type="text" class="form-control" placeholder="House name/ House number"     name="house">
  </p>
<p><br>
  <input type="text" class="form-control" placeholder="Street Name" name="street">
</p>
<p><br>
  <input type="text" class="form-control" placeholder="Town" name="town">
</p>
<p><br>
  <input type="text" class="form-control" placeholder="Postcode" name="postcode"><br>
</p>
<hr>
<div class="col-md-6">
  <p>
    <input type="text" class="form-control" placeholder="Country of birth" name="origin">
    </p>
  <p><br>
    <input type="text" class="form-control" placeholder="Mobile/Phone number" name="number"><form action="send.php" class="form-horizontal" method="post" name="application">
<div class="col-md-6">
  <p>
  <select class="form-control" name="title">
    <option value="" disabled>Please select </option>
    <option value="Mr">Mr</option>
    <option value="Mrs">Mrs</option>
    <option value="Miss">Miss</option>
    <option value="Ms">Ms</option>
  </select>
  </p>
  <p><br>
    <input type="text" class="form-control" placeholder="First name" name="first_name">
  </p>
  <p><br>
    <input type="text" class="form-control" placeholder="Surname" name="surname">
  </p>
  <p>Date of birth:<br><input type ="date" name="dob" class="form-control">
  </p>
</div><hr>
<h3>Address</h3>
<p>
  <input type="text" class="form-control" placeholder="House name/ House number"     name="house">
  </p>
<p><br>
  <input type="text" class="form-control" placeholder="Street Name" name="street">
</p>
<p><br>
  <input type="text" class="form-control" placeholder="Town" name="town">
</p>
<p><br>
  <input type="text" class="form-control" placeholder="Postcode" name="postcode"><br>
</p>
<hr>
<div class="col-md-6">
  <p>
    <input type="text" class="form-control" placeholder="Country of birth" name="origin">
    </p>
  <p><br>
    <input type="text" class="form-control" placeholder="Mobile/Phone number" name="number">
  </p>
  <p>       
    <input type="text" class="form-control" placeholder="Email address" name="email">
  </p>
  <p><br>
    <input type="hidden" name="plan" value="<?php $plan; ?>">
    <input type="submit" class="btn btn-success btn-lg" value="Submit application">
  </p>
</div><hr>
</form>

这是send.php:

<?php

 $host="localhost"; // Host name 
 $username="******"; // Mysql username 
 $password="******"; // Mysql password 
 $db_name="*****"; // Database name 
 $tbl_name="*****"; // Table name 

 // Connect to server and select database.
 mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");

 // Get values from form 
 $title         =$_POST['title'];
 $first_name    =$_POST['first_name'];
 $surname       =$_POST['surname'];
 $dob           =$_POST['dob'];
 $house         =$_POST['house'];
 $street        =$_POST['street'];
 $town          =$_POST['town'];
 $postcode      =$_POST['postcode'];
 $origin        =$_POST['origin'];
 $number        =$_POST['number'];
 $email         =$_POST['email'];
 $plan          =$_POST['plan'];



 // Insert data into mysql 
 $sql="INSERT INTO `$tbl_name` (title, first_name, surname, dob, house, street, town, postcode, origin, number, email, plan) VALUES ('$title', '$first_name', '$surname', '$dob',  '$house', '$street', '$town', '$postcode', '$origin', '$number','$email', '$plan')";
 //$sql=mysql_real_escape_string($sql);
 $result=mysql_query($sql);

 // if successfully insert data into database, displays message "Successful". 
 if($result){
 echo "Successful";
 echo "<BR>";
 echo "<a href='insert.php'>Back to main page</a>";
 }

 else {
 echo "ERROR";
 }
 ?> 

 <?php 
 // close connection 
 mysql_close();
 ?>

答案 2 :(得分:0)

你输入的POST都错了。 你的:$_POST["somename']; // begin with double quote inside and finish with single quote.
正确:$_POST[""]; or $_POST['somename']; // begin with the same end end with the same. 因此,您的变量未正确分配。

答案 3 :(得分:0)

$sql="INSERT INTO " . $tbl_name . "(`title`, `first_name`, `surname`, `dob`, `house`, `street`, `town`, `postcode`, `origin`, `number`, `email`, `plan`) VALUES($title, $first_name, $surname, $dob, $house, $street, $town, $postcode, $origin, $number, $emai, $plan)";
$sql = mysql_real_escape_string($sql);
$result=mysql_query($sql);