我是PHP和MySQL的新手。 我创建了一个php表单来保存一些数据到数据库表。 提交表格后,它会给出
“1记录添加”
在php插入字符串中指定。 但是在phpmyadmin中打开表后,它显示添加了一行但没有数据..
php表单是:
<h2>Register Yourself</h2>
<form method="post" action="get-member.php">
<div>
<span><p>First Name:</p></span>
<span><input type="text" class="form-control" id="FirstName"></span>
</div>
<div>
<span>Last Name:</span>
<span><input type="text" class="form-control" id="LastName"></span>
</div>
<div>
<span>Father's Name: </span>
<span><input type="text" class="form-control" id="FatherName"></span>
</div>
<div>
<span>Mother's Name: </span>
<span><input type="text" class="form-control" id="MotherName"></span>
</div>
<div>
<span>Date of Birth: </span>
<span><input type="date" class="form-control" id="DOB"></span>
</div>
<div>
<span>Address: </span>
<span><input type="text" class="form-control" id="Address"></span>
</div>
<div>
<span>City: </span>
<span><input type="text" class="form-control" id="City"></span>
</div>
<div>
<span>District: </span>
<span><input type="text" class="form-control" id="District"></span>
</div>
<div>
<span>Postal Code: </span>
<span><input type="text" class="form-control" id="PostalCode"></span>
</div>
<div>
<span>Personal Mobile #:</span>
<span><input type="number" class="form-control" id="Pmobile"></span>
</div>
<div>
<span>Father's Contact #:</span>
<span><input type="number" class="form-control" id="Fmobile"></span>
</div>
<div>
<span>Mother's Contct #:</span>
<span><input type="number" class="form-control" id="Mmobile"></span>
</div>
<div>
<span>Home contact #:</span>
<span><input type="number" class="form-control" id="Hmobile"></span>
</div>
<div>
<span><input type="submit" value="Register"></span>
</div>
</form>
并且get-member.php文件是:
<?php
//stablising connection
include("../database/connection.php");
//escape variables for security
$FirstName = mysqli_real_escape_string($con, $_POST['FirstName']);
$LastName = mysqli_real_escape_string($con, $_POST['LastName']);
$FatherName = mysqli_real_escape_string($con, $_POST['FatherName']);
$MotherName = mysqli_real_escape_string($con, $_POST['MotherName']);
$DOB = mysqli_real_escape_string($con, $_POST['DOB']);
$Address = mysqli_real_escape_string($con, $_POST['Address']);
$City = mysqli_real_escape_string($con, $_POST['City']);
$District = mysqli_real_escape_string($con, $_POST['District']);
$PostalCode = mysqli_real_escape_string($con, $_POST['PostalCode']);
$Pmobile = mysqli_real_escape_string($con, $_POST['Pmobile']);
$Fmobile = mysqli_real_escape_string($con, $_POST['Fmobile']);
$Mmobile = mysqli_real_escape_string($con, $_POST['Mmobile']);
$Hmobile = mysqli_real_escape_string($con, $_POST['Hmobile']);
$sql="INSERT INTO RUPmembers (FirstName, LastName, FatherName, MotherName, DOB, Address, City, District, PostalCode, Pmobile, Fmobile, Mmobile, Hmobile)
VALUES ('$FirstName','$LastName','$FatherName','$MotherName','$DOB','$Address','$City','$District','$PostalCode','$Pmobile','$Fmobile','$Mmobile','$Hmobile')";
if (!mysqli_query($con,$sql))
{
die ('Error:' . mysqli_error($con));
}
echo "1 Record addedd";
//closing connection
include("../database/close-connection.php");
?>
插入3个数据后的phpmyadmin表:
[抱歉它要求获得10个回复才能添加图片,但这里是截屏链接http://oi60.tinypic.com/n6rtyu.jpg ]
答案 0 :(得分:1)
您正在使用id =“FirstName”
您需要为每个字段设置属性name
。例如:
name="FirstName"
要对此进行调试,您应该使用$_POST
查看var_dump($_POST)
变量,以显示表单中传递的内容。
注意:应保护查询不受从用户收到的任何数据的影响,以防止sql注入。请参阅此post以了解如何避免这种情况。
答案 1 :(得分:1)
更改
输入类型=&#34;文字&#34;类=&#34;形状控制&#34; ID =&#34;名字&#34;&GT;
到
输入类型=&#34;文字&#34;类=&#34;形状控制&#34;名称=&#34;名字&#34;&GT;
这可以解决您的问题,但不要将用户未处理的信息插入您的查询。
答案 2 :(得分:0)
要通过php从html表单中捕获数据,应该在$ _post [&#39;&#39;]方法中给出名称而不是id。
使用name="FirstName"
name="LastName"
name="DOB"
等。
它将解决问题..