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