Jump to content

Getting newly entered data's ID


Jurik

Recommended Posts

Hi guys, this is proberly a simple soluition and I should know it by now, but I just cant think today and its getting ridiculas now, what im wanting to do is get the ID of the data I have just recently entered so that the user can get a job number from it. My code is below for you to look at im wanting the ID number to appear where I have typed "Your job number is **" any ideas?

[code]

<?php
if (isset($_POST['submit'])) {
$Name = $_POST['Name'];
$Date = $_POST['Date'];
$Department = $_POST['Department'];
$Room = $_POST['Room'];
$ProbCat  = $_POST['ProbCat'];
$ProbDes = $_POST['ProbDes'];
$submit = $_POST['submit'];
} else {
$Name = "";
$Date = "";
$Department = "";
$Room = "";
$ProbCat  = "";
$ProbDes = "";
$submit = "";
}

$success = 0;


if ($submit == "Save" && $Name != NULL && $Date != NULL && $Department != NULL && $Room != NULL && $ProbCat != NULL && $ProbDes != NULL)
{
$sql = "INSERT INTO help_msg (Name,Date,Department,Room,ProbCat,ProbDes) VALUES ('" . $Name . "', '" . $Date . "', '" . $Department . "' ,'" . $Room . "' ,'" . $ProbCat . "' ,'" . $ProbDes . "')";

include "conn.inc.php";

$result = mysql_query($sql);

if(mysql_affected_rows() == 1)
{
print("New record has been added sucessfuly. Your job number is **");
$success = 1;
}
else
{
print("I'm afraid an error has been detected, please check the information entered again. If the problem persists then contact IT support");
}
}
if($success != 1) {
?>

[/code]
Link to comment
Share on other sites

I'm assuming that the ID is an auto increment in the database, so all you would have to do was select it.

[code]

if(mysql_affected_rows() == 1)
{
              $query = mysql_query("SELECT ID FROM help_msg WHERE name='$name'");
              $row = mysql_fetch_assoc($query);

print("New record has been added sucessfuly. Your job number is {$row['ID']}");
$success = 1;
}

[/code]
Link to comment
Share on other sites

mysql_insert_id() is always a good one to use if the table is autoincremented
[code]
<?php
//after an insert statement
$insert_id = mysql_insert_id(); // this now holds the id value of the last inserted row in the table

$query = "SELECT * FROM table WHERE idtable = '$insert_id'";
?>
[/code]
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.