这段代码不漂亮,但我想从表中打印出最后生成的自动增量 IDstudentcourseplan
<?php
session_start();
if (!isset($_SESSION['session_username']) ) { header ("Location: login.php");exit;} //If user in not signed in, redirect to login page
include("dbconfig.php");
if (isset($_POST['course_id'])) {
$course_id = $_POST['course_id'];
$student_id = $_POST['student_id'];
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
$studentcourseplan_update = mysql_query("
INSERT INTO studentcourseplan (student_id,cpl_startdate, cpl_enddate, cpl_coursename, cpl_pricevalue)
SELECT '$student_id','$start_date', '$end_date', course_name, price_value
FROM courses c
INNER JOIN pricegroups p ON p.price_id = c.course_price_id
WHERE c.course_id = '$course_id'");
$result = mysql_query("SELECT LAST_INSERT_ID() FROM studentcourseplan");
$row = mysql_fetch_assoc($result);
$cpl_id = $row['cpl_id'];
$studentcourseplanelements_update = mysql_query("
INSERT INTO studentcourseplanelements (scpe_cpl_id, scpe_name, scpe_days)
SELECT LAST_INSERT_ID(), cp.plan_name, cp.plan_time FROM courses c
INNER JOIN course_to_plan cpl ON cpl.course_id = c.course_id
INNER JOIN courseplans cp ON cp.plan_id = cpl.plan_id
WHERE cpl.course_id = '$course_id'");
$courseinfo = mysql_query("SELECT * FROM studentcourseplan WHERE cpl_id = '$cpl_id'") or die(mysql_error());
if ($studentcourseplan_update === true) {
while ($result = mysql_fetch_assoc($courseinfo)) {
echo $result['cpl_id'];
}
} else if ($studentcourseplan_update === false){
echo "Kunde inte uppdatera.";
}
}
?>
它在第 24 行抱怨:
$cpl_id = $row['cpl_id'];
我得到:
未定义索引:第 24 行 \Path\To\Page.php 中的 cpl_id