我在html中设计了一个注册表单。并尝试使用此PHP代码在mysql数据库中输入。但我无法在数据库中添加记录。 php文件的代码如下所示:
注意:数据库名称为rku_database_main
<?php
$con = mysqli_connect("localhost","root","","rku_database_main");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$us_student_id= $_POST['student_id'];
$us_user_id= $_POST['user_id'];
$us_name= $_POST['name'];
$us_permanent_address= $_POST['permanent_address'];
$us_local_address= $_POST['local_address'];
$us_birthdate= $_POST['birthdate'];
$us_gender= $_POST['gender'];
$us_email_address= $_POST['email_address'];
$us_student_cell_number= $_POST['student_cell_number'];
$us_parents_cell_number= $_POST['parents_cell_number'];
$us_password1 = $_POST['password1'];
$us_password2 = $_POST['password2'];
mysqli_query($con,"INSERT INTO student_profile(student_id,user_id,name,permanent_address,local_address,password,birthdate,gender,email_address,student_cell_number,parents_cell_number,password1,password2) VALUES ('".$us_student_id."','".$us_user_id."','".$us_name."','".$us_permanent_address."','".$us_local_address."','".$us_birthdate."','".$us_gender."','".$us_email_address."','".$us_student_cell_number."','".$us_parents_cell_number."','".$us_password1."','".$us_password2.")");
mysqli_close($con);
if (!mysqli_query($con,$sql))
{
die('Error: redcord not added try again' . mysqli_error($con));
}
echo "1 record added";
?>`
这是我为数据库中的make条目编写的代码。表名是student_profile。字段如代码所示。
我无法将记录输入到数据库表中。输出中的错误显示为“未定义的索引”。
答案 0 :(得分:3)
尝试以下代码。
mysqli_query($con,"INSERT INTO `student_profile` (`student_id`,`user_id`,`name`,`permanent_address`,`local_address`,`password`,`birthdate`,`gender`,`email_address`,`student_cell_number`,`parents_cell_number`,`password1`,`password2`) VALUES ('$us_student_id','$us_user_id','$us_name','$us_permanent_address','$us_local_address','$us_birthdate','$us_gender','$us_email_address','$us_student_cell_number','$us_parents_cell_number','$us_password1','$us_password2')");
答案 1 :(得分:2)
以这种方式编写查询有助于调试和查找查询错误,正如Fred-ii所提到的,你错过了引用
$qry = "INSERT INTO
`student_profile`
(
`student_id`,
`user_id`,
`name`,
`permanent_address`,
`local_address`,
`password`,
`birthdate`,
`gender`,
`email_address`,
`student_cell_number`,
`parents_cell_number`,
`password1`,
`password2`
)
VALUES
(
'".$us_student_id."',
'".$us_user_id."',
'".$us_name."',
'".$us_permanent_address."',
'".$us_local_address."',
'".$us_birthdate."',
'".$us_gender."',
'".$us_email_address."',
'".$us_student_cell_number."',
'".$us_parents_cell_number."',
'".$us_password1."',
'".$us_password2."'
)" ;
mysqli_query($con,$qry);
答案 2 :(得分:2)
首先创建connection.php页面并使用下面的代码。
<?php
$con = mysql_connect("localhost","root","") or die("can not connect");
$db = mysql_select_db("rku_database_main");
?>
现在在页面中添加包含上述代码的连接文件...使用下面的代码添加连接文件。
include('connection.php');
包含此文件后,在下一行使用下面的代码。
<?php
$us_student_id= $_POST['student_id'];
$us_user_id= $_POST['user_id'];
$us_name= $_POST['name'];
$us_permanent_address= $_POST['permanent_address'];
$us_local_address= $_POST['local_address'];
$us_birthdate= $_POST['birthdate'];
$us_gender= $_POST['gender'];
$us_email_address= $_POST['email_address'];
$us_student_cell_number= $_POST['student_cell_number'];
$us_parents_cell_number= $_POST['parents_cell_number'];
$us_password1 = $_POST['password1'];
$us_password2 = $_POST['password2'];
$sql1="INSERT INTO `student_profile` (`student_id`,`user_id`,`name`,`permanent_address`,`local_address`,`password`,`birthdate`,
`gender`,`email_address`,`student_cell_number`,`parents_cell_number`,`password1`,`password2`)
VALUES ('$us_student_id','$us_user_id','$us_name','$us_permanent_address','$us_local_address','$us_birthdate','$us_gender','$us_email_address','$us_student_cell_number','$us_parents_cell_number','$us_password1','$us_password2')";
$query1=mysql_query($sql1) or die("query failed1".mysql_error());
答案 3 :(得分:1)
如果您的数据库表名称和行名称正确,那么这应该有效:
mysqli_query($con,"INSERT INTO `student_profile` (`student_id`,`user_id`,`name`,`permanent_address`,`local_address`,`password`,`birthdate`,`gender`,`email_address`,`student_cell_number`,`parents_cell_number`,`password1`,`password2`) VALUES ('".$us_student_id."','".$us_user_id."','".$us_name."','".$us_permanent_address."','".$us_local_address."','".$us_birthdate."','".$us_gender."','".$us_email_address."','".$us_student_cell_number."','".$us_parents_cell_number."','".$us_password1."','".$us_password2."')");
只需在表格和行名称周围添加'标记,并且您的上一个值已丢失'。
可以肯定的是,您正在从表单中获取值,POST方法。
答案 4 :(得分:1)
这可能是由于HTML元素的名称,请检查HTML元素名称和$ _POST ['元素名称']是否相同。
答案 5 :(得分:1)
试试这个:
<?php
mysqli_query($con,"INSERT INTO student_profile(student_id,user_id,name,permanent_address,local_address,password,birthdate,gender,email_address,student_cell_number,parents_cell_number,password1,password2) VALUES ('$us_student_id','$us_user_id','$us_name','$us_permanent_address','$us_local_address','$us_birthdate','$us_gender','$us_email_address','$us_student_cell_number','$us_parents_cell_number','$us_password1','$us_password2')");
答案 6 :(得分:1)
嗨未定义索引意味着数组中缺少某些内容。这意味着,无论你想从$ _POST数组得到什么都不可用。
执行print_r($ _ POST)并检查是否有正确的数组元素调用。