【问题标题】:Undefined offset: error for database table id [duplicate]未定义的偏移量:数据库表 ID 错误 [重复]
【发布时间】:2018-02-13 13:38:38
【问题描述】:

我正在开发一个登录系统,用户必须在注册和登录后填写他们的个人资料信息。在注册部分,我在一个名为 users 的表中获取他们的详细信息,主键为 user_id 这是一个表格截图

接下来,用户登录后,我创建了一个不同的表名students,用户在其中填写了有关他的个人资料的表格,但对于这个表,我试图将user_id 作为主键作为它的主键是为了桌子users

所以我要做的是从变量$ident = $user->user_id; 中的第一个表(用户)中获取 user_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>

如何为学生获取与用户相同的 id

【问题讨论】:

  • 它告诉你$_POST[$ident] 不存在。 var_dump你的$_POST数组,看看看看
  • $_POST[$ident] - 什么是$ident?可能1。为什么你认为$_POST[1] 会存在?
  • 您的代码容易受到 MySQL 注入的影响。请使用prepared statements
  • 只需在 SQL 语句中将 $_POST[$ident] 更改为 $ident。我看到你正在使用PDO,但你的代码仍然对 SWL 注入开放尝试将代码转换为使用准备语句。
  • 如果你不能自己解决这样简单的问题,如果你甚至无法正确研究这个之前已经讨论过无数次的问题......那么也许在那个知识/技能水平上首先,您不应该在登录系统上工作,那肯定会遇到麻烦。我强烈建议您使用现有的、成熟的解决方案。

标签: php html sql phpmyadmin


【解决方案1】:

将 $_POST[$ident] 更改为 $ident。用户 ID 在 $ident 中而不是在 $_POST[$ident] 中。我希望这会有所帮助。

【讨论】:

  • 非常感谢,但如果我尝试重新输入数据库中的值,它会给我错误完整性约束违规:1062 Duplicate entry '1' for key 'PRIMARY' 有什么方法可以替换旧值换新
  • 根据您当前的结构,学生表中每个 user_id 只允许 1 行。如果您想要超过 1 行,则必须删除 user_id 作为主键。如果你想保持这种方式并在用户第二次来的时候更新行,你可以在mysql的重复键更新语句上使用插入。您可以在此处查看更多详细信息dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html。我希望这会有所帮助。
  • 我对 php 非常陌生,我正在从事这个项目,因为没有其他人可以为我做后端,所以请不要介意我问....我必须做什么和在哪里插入以在重复键上使用。更新 t1 SET c=c+1 WHERE a=1;对我来说毫无意义...请帮助
  • 将此附加到插入查询的末尾 => "ON DUPLICATE KEY UPDATE full_name = '$_POST["full_name"]', gender = '$_POST["gender"]' , dob = '$_POST["dob"]',present_add = '$_POST["present_add"]',contact_add = '$_POST["contact_add"]',兴趣 = '$_POST["interest"]',资格 = '$ _POST["qualification"]',course_date = '$_POST["course_date"]',board_name = '$_POST["board_name"]',标记 = '$_POST["marks"]',电话 = '$_POST[ "电话"]'"。
猜你喜欢
  • 1970-01-01
  • 2011-03-13
  • 1970-01-01
  • 1970-01-01
  • 2011-01-31
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
相关资源
最近更新 更多