Jump to content

Getting last record from the database


ashrafzia

Recommended Posts

I have the following table :

 

----------------------------------

Studnet_id | Reg_no | Std_name | etc....

----------------------------------

 

- Initially all records are Null.

- Student_id is Primary key.

 

I want to read last record from the field Student_id i-e NULL and then add +1 to it to get new Student_id.

This is the code:

include "connection.php";
include "menu.php";
$tbname = "student_info";
$sql = "SELECT student_id FROM $tbname";
$result = @mysql_query($sql, $conn) or die (mysql_error());

if (!mysql_num_rows($result)){
$student_id++;
}
else {	
	while ($row = mysql_fetch_array($result)){
	$student_id = $row['student_id'];

	if ($student_id > 0) { $student_id++; }
	}
}

$form = "<body>
<form action='PHP_SELF' method='post' enctype='multipart/form-data'>
  <table width='394' border='0' align='center' cellpadding='5' cellspacing='5'>
    <tr>
      <td width='135'>Studnet ID:</td>
      <td width='218'><input name='student_id' type='text' size='5' value=$student_id></td>
    </tr>
<input type='image' name='register' src='images/register.gif'>
    etc......
</body>";

echo "$form";

if (isset($_POST['register_x'])){
$sql = "INSERT INTO $tbname (student_id, registration_no, student_name, father_name, programe, picture) VALUES
	('$_POST[student_id]', '$_POST[registration_no]', '$_POST[student_name]', '$_POST[father_name]', '$_POST[programe]',
	 '$_POST[picture]')";
$result = @mysql_query($sql, $conn) or die (mysql_error());
echo "Record Added Successfully.....";
}

 

Problems:

 

Whenever a Record is added, everything goes fine but Student_id doesn't change. It remains the same.

I want whenever someone enters a record, values should be added to database and then Student_id must be updated by reading last record/last value of Student_id and therefore incremented.

The above code is working fine but there's a problem with it, and it's this : Whenever a record is added the Student_id doesn't change for the first time and when Register button is clicked again it gives a primary key error and then it's updated. I don't know what's the problem?

 

I hope you got what i want to say.... If further any Question please ask.

 

 

 

 

Link to comment
Share on other sites

First off, make sure your "STUDENT_ID" column in your MySQL Database is set to "auto_increment".

 

Secondly, in your INSERT statement, where the student_id goes, just leave it blank.

$sql = "INSERT INTO $tbname (student_id, registration_no, student_name, father_name, programe, picture) VALUES

('', '$_POST[registration_no]', '$_POST[student_name]', '$_POST[father_name]', '$_POST[programe]',

'$_POST[picture]')";

 

 

 

Also I recommend validating your post information before putting it in your db... your just asking for someone to hack you...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.