Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8080591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:27:28+00:00 2026-06-05T16:27:28+00:00

This code is not pretty, but I want to print out the last generated

  • 0

This code is not pretty, but I want to print out the last generated Auto Incremented ID from the table studentcourseplan

<?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.";
    }
}
?>

It whines on line 24 which is:

$cpl_id = $row['cpl_id'];

I get:

Undefined index: cpl_id in \Path\To\Page.php on line 24

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:27:30+00:00Added an answer on June 5, 2026 at 4:27 pm

    Because you have selected only LAST_INSERT_ID() in the first select query:

    $result = mysql_query("SELECT LAST_INSERT_ID() FROM studentcourseplan");
    $row = mysql_fetch_assoc($result);
    // Doesn't exist, only $row['LAST_INSERT_ID()']
    $cpl_id = $row['cpl_id'];
    // Probably you meant
    $cpl_id = $row['LAST_INSERT_ID()'];
    

    But the above is unnecessary.

    Instead, you can just call mysql_insert_id() rather than executing a query to retrieve it.

    $cpl_id = mysql_insert_id();
    

    Note about security:

    Your query is vulnerable to SQL injection in its current form. Be sure to call mysql_real_escape_string() over all the $_POST vars used as query inputs.

    $course_id = mysql_real_escape_string($_POST['course_id']);
    $student_id = mysql_real_escape_string($_POST['student_id']);
    $start_date = mysql_real_escape_string($_POST['start_date']);
    $end_date = mysql_real_escape_string($_POST['end_date']);
    

    In the long run, it’s recommended to migrate away from the mysql_*() functions and instead learn to use a more modern API supporting prepared statements like PDO or MySQLi.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does this code not print an exception stack trace? public class Playground {
This code does not throw an error but the query fails, that is, the
runat=server /> But this code is not working How can I add multiple controls
This is probably a quicky. Why does this code not return anything? import java.util.Scanner;
Possible Duplicate: Comparing Strings in Cocoa Why is this code not recognising the NSString
This code does not function as expected: // $field contains the name of a
this code will not pass the query string to default3.aspx on Image Button ..
Why this code does not compile (Cygwin)? #include <vector> template <class Ttile> class Tilemap
The error shows this code can not rollback: class AddCountToTag < ActiveRecord::Migration def change
I am unsure why this code will not work. When I click a button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.