Php 未定义的偏移量:数据库表id错误

Php 未定义的偏移量:数据库表id错误,php,html,sql,phpmyadmin,Php,Html,Sql,Phpmyadmin,我正在开发一个登录系统,用户在注册和登录后必须填写他们的个人资料信息。在注册部分,我使用主键user\u id在名为users的表中获取了他们的详细信息。下面是该表的屏幕截图 接下来,在用户登录后,我创建一个不同的表名students,用户在其中填写关于其个人资料的表单,但是对于这个表,我试图获得与表用户相同的用户id,作为主键 因此,我要做的是从变量$ident=$user->user\id中的第一个表(users)中获取用户id然后尝试用代码将此变量的值插入“students”表user

我正在开发一个登录系统,用户在注册和登录后必须填写他们的个人资料信息。在注册部分,我使用主键
user\u id
在名为
users
的表中获取了他们的详细信息。下面是该表的屏幕截图

接下来,在用户登录后,我创建一个不同的表名
students
,用户在其中填写关于其个人资料的表单,但是对于这个表,我试图获得与表
用户相同的
用户id
,作为主键

因此,我要做的是从变量
$ident=$user->user\id中的第一个表(users)中获取用户id
然后尝试用代码将此变量的值插入“students”表user_id中

$sql = "INSERT INTO students (user_id,full_name, gender, dob, present_add, contact_add, interest, qualification, course_date, board_name, marks, phone)
VALUES ('".$_POST[$ident]."', '".$_POST["full_name"]."', '".$_POST["gender"]."', '".$_POST["dob"]."', '".$_POST["present_add"]."', '".$_POST["contact_add"]."', '".$_POST["interest"]."', '".$_POST["qualification"]."' ,'".$_POST["course_date"]."','".$_POST["board_name"]."', '".$_POST["marks"]."', '".$_POST["phone"]."')";
但我在执行此操作时出错(“未定义的偏移量:1) 请帮忙 这是完整的代码

<?php
/**
 * Tutorial: PHP Login Registration system
 *
 * Page : Profile
 */

// Start Session
session_start();

// check user login
if(empty($_SESSION['user_id']))
{
    header("Location: index.php");
}

// Database connection
require __DIR__ . '/database.php';
$db = DB();

// Application library ( with DemoLib class )
require __DIR__ . '/lib/library.php';
$app = new DemoLib();


$user = $app->UserDetails($_SESSION['user_id']); // get user details
$ident = $user->user_id;






if(isset($_POST["submit"])){
$hostname='localhost';
$username='root';
$password='';

try {
$dbh = new PDO("mysql:host=$hostname;dbname=college",$username,$password);

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "INSERT INTO students (user_id,full_name, gender, dob, present_add, contact_add, interest, qualification, course_date, board_name, marks, phone)
VALUES ('".$_POST[$ident]."', '".$_POST["full_name"]."', '".$_POST["gender"]."', '".$_POST["dob"]."', '".$_POST["present_add"]."', '".$_POST["contact_add"]."', '".$_POST["interest"]."', '".$_POST["qualification"]."' ,'".$_POST["course_date"]."','".$_POST["board_name"]."', '".$_POST["marks"]."', '".$_POST["phone"]."')";
if ($dbh->query($sql)) {
echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
}
else{
echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
}

$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}

}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Profile</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <div class="well">
            <h2>
                Profile
            </h2>
            <h3>Hello <?php echo $ident ?>,</h3>
            <p>
                Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur deserunt dolore fuga labore magni maxime, quaerat reiciendis tenetur? Accusantium blanditiis doloribus earum error inventore laudantium nesciunt quis reprehenderit ullam vel?
            </p>
            <a href="logout.php" class="btn btn-primary">Logout</a>
        </div>





<form action="" method="post">
<label>Full Name :</label>
<input type="text" name="full_name" id="full_name" required="required" placeholder="First Middle Last"/><br /><br />
<label>Gender</label>  
  <input type="radio" name="gender" id="gender" value="male" checked> Male<br>
  <input type="radio" name="gender" id="gender" value="female"> Female<br>
  <input type="radio" name="gender" id="gender" value="other"> Other<br /><br />
<label>Date Of Birth :</label>
<input type="date" name="dob" id="dob" required="required" placeholder="yyyy-mm-dd"/><br /><br />
<label>Present Address :</label>
<input type="text" name="present_add" id="present_add" required="required" placeholder="Enter Your Present Address"/><br /><br />
<label>Contact Address :</label>
<input type="text" name="contact_add" id="contact_add" required="required" placeholder="Enter Your Contact Address"/><br /><br />
<label>Programme you Wish to Apply for :</label>
  <select name="interest" id="interest">
    <option value="B.tech">B.tech</option>
    <option value="MBBS">MBBS</option>
    <option value="B.Arch">B.Arch</option>
    <option value="B.Pharm">B.Pharm</option>
    <option value="B.A">B.A</option>
    <option value="B.Sc">B.Sc</option>
    <option value="M.Pharm">M.Pharm</option>
    <option value="M.tech">M.tech</option>
    <option value="M.A">M.A</option>
    <option value="MBA">MBA</option>
    <option value="M.Sc">M.Sc</option>
  </select><br /><br />
<label>Name of Secondary School/High School/College/University :</label>
<input type="text" name="qualification" id="qualification" required="required" placeholder="Please Enter The Relevant Exam Name You Recently Appeared For"/><br /><br />
<label>Dates Attended From and To :</label>
<input type="text" name="course_date" id="course_date" required="required" placeholder="yyyy-mm-dd to yyyy-mm-dd"/><br /><br />
<label>Board Name :</label>
<input type="text" name="board_name" id="board_name" required="required" placeholder="Name of Board"/><br /><br />
<label>Marks :</label>
<input type="text" name="marks" id="marks" required="required" placeholder="Grades Achieved"/><br/><br />
<label>phone :</label>
<input type="text" name="phone" id="phone" required="required" placeholder="Please Enter your Phone Number"/><br /><br />
<input type="submit" value=" Submit " name="submit"/><br />
</form>



</div>
<?php

?>      
</body>
</html>

将$\u POST[$ident]更改为$ident。用户id在$ident中,而不是在$\u POST[$ident]中。我希望这会有所帮助。

它告诉您,
$\u POST[$ident]
不存在。
var\u dump
您的
$\u POST
数组,请查看
$\u POST[$ident]
-什么是
$ident
?很可能是
1
。你为什么认为
$\u POST[1]
会存在?你的代码容易受到MySQL注入的攻击。请使用更改你的
$\u POST[$ident]
到您的SQL语句中的
$ident
。我看到您正在使用
PDO
,但您的代码仍然可以接受SWL注入,请尝试将代码转换为使用prepare语句。如果您自己无法修复像这样的简单问题,如果您甚至无法正确地研究这样一个问题,而这个问题在一次测试之前已经讨论过无数次lready…那么,也许在知识/技能层面上,您首先不应该使用登录系统,这肯定会导致麻烦。我强烈建议您使用现有的、已建立的解决方案。非常感谢您的帮助,但如果我尝试在数据库中重新输入值,则会产生错误完整性nt冲突:1062键“PRIMARY”的重复项“1”是否有任何方法将旧值替换为新值根据您当前的结构,student表中每个用户\u id仅允许1行。如果您想要超过1行,则必须删除用户\u id作为主键。如果您希望保持这种方式,并在用户到来时更新该行d时间你可以使用mysql的insert on duplicate key update语句。你可以在这里查看更多详细信息。我希望这会有所帮助。我对php非常陌生,我正在从事这个项目,因为没有其他人可以为我做后端,所以请不要介意我问…我必须在什么地方插入才能在重复key上使用。update t1 SET c=c+1其中a=1;对我来说毫无意义…请帮助在插入查询结尾处添加此内容=>“在重复键更新时全名='$\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\[“资格”]、课程日期=“$”职位[“课程日期”]、董事会名称=“$”职位[“董事会名称”]、分数=“$”职位[“分数”]”、电话=“$”职位[“电话”]”。