我正在尝试将表单数据保存到mysql数据库最近2天,但是,表单数据没有保存到mysql数据库,而是每次我将User Info.php代码文件下载到我的计算机按提交按钮。需要帮助! 这是html格式;
<form class="form-horizontal" role="form" action="User Info.php" method="post"/>
<div class="form-group">
<label class="control-label col-sm-2" for="First Name">First Name:</label>
<div class="col-sm-10">
<input type="text" name="FirstName" class="form-control" id="First Name" placeholder="First Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="Last Name">Last Name:</label>
<div class="col-sm-10">
<input type="Last Name" name="LastName" class="form-control" id="Last Name" placeholder="Last Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="Company Name">Company Name:</label>
<div class="col-sm-10">
<input type="Company Name" name="CompanyName" class="form-control" id="Company Name" placeholder="Company Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="email">Email:</label>
<div class="col-sm-10">
<input type="Email" name="Email" class="form-control" id="Email" placeholder="Email">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="Message">Message</label>
<div class="col-sm-10">
<textarea type="Message" name="Message" class="form-control" rows="3" columns="50" id="Message" placeholder="Message"></textarea></div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button name="submit" class="submit-btn" type="submit">Submit</button>
这是php代码db.php;
<?php
$conn = mysql_connect('localhost','root','....', 'testforlivesite') or die("error while connecting to the database");
$db = mysql_select_db('testforlivesite', $conn) or die("error while connecting to the database");
?>
用户信息.php
<?php
include_once('db.php');
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$CompanyName = $_POST['CompanyName'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];
if(mysql_query("INSERT INTO ContactUs (FirstName, LastName, CompanyName, Email, Message) VALUES ('$FirstName', '$LastName', '$CompanyName', '$Email', '$Message')"))
echo "Thanks for contacting!";
else
echo "Please rewrite the message.";
?>
答案 0 :(得分:0)
将文件名从User Info.php更改为UserInfo.php删除空格,并将文件从User Info.php重命名为UserInfo.php。
<form class="form-horizontal" role="form" action="UserInfo.php" method="post"/>
<div class="form-group">
<label class="control-label col-sm-2" for="First Name">First Name:</label>
<div class="col-sm-10">
<input type="text" name="FirstName" class="form-control" id="First Name" placeholder="First Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="Last Name">Last Name:</label>
<div class="col-sm-10">
<input type="Last Name" name="LastName" class="form-control" id="Last Name" placeholder="Last Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="Company Name">Company Name:</label>
<div class="col-sm-10">
<input type="Company Name" name="CompanyName" class="form-control" id="Company Name" placeholder="Company Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="email">Email:</label>
<div class="col-sm-10">
<input type="Email" name="Email" class="form-control" id="Email" placeholder="Email">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="Message">Message</label>
<div class="col-sm-10">
<textarea type="Message" name="Message" class="form-control" rows="3" columns="50" id="Message" placeholder="Message"></textarea></div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button name="submit" class="submit-btn" type="submit">Submit</button>