drt41563 2017-09-05 09:46
浏览 88
已采纳

未定义的偏移量:数据库表id的错误[重复]

I'm working on a login system where users have to fill in their profile info after registration and logging in. During the registration part i get their details in a table named users with primary key user_id here is a screen shot of the table

users

Next after the user logs in I create a different table name students where the users fills up a form about his profile, but for this table I'm trying to get the same user_id as the primary key as it was for the table users

students

So what I do is I get the user_id from the first table (users) in a variable $ident = $user->user_id; and then try to insert the value of this variable in the "students" table user_id with the code

$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"]."')";

but I'm getting a error while doing so ("undefined offset: 1) please help here is the complete code

<?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>

how can I get the same id for students as it is for users

</div>
  • 写回答

1条回答 默认 最新

  • dongwo1234 2017-09-05 09:51
    关注

    Change $_POST[$ident] to $ident. User id is in $ident not in $_POST[$ident]. I hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名