Jurik Posted January 9, 2007 Share Posted January 9, 2007 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]<?phpif (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] Quote Link to comment https://forums.phpfreaks.com/topic/33443-getting-newly-entered-datas-id/ Share on other sites More sharing options...
pocobueno1388 Posted January 9, 2007 Share Posted January 9, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/33443-getting-newly-entered-datas-id/#findComment-156567 Share on other sites More sharing options...
Jurik Posted January 9, 2007 Author Share Posted January 9, 2007 hhhmmm I tried this as well and it just does not print the number. Its just blank after "Your job number is" any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/33443-getting-newly-entered-datas-id/#findComment-156577 Share on other sites More sharing options...
Jurik Posted January 9, 2007 Author Share Posted January 9, 2007 Never mind I have it working now, just needed to make name in code to Name, DOH! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/33443-getting-newly-entered-datas-id/#findComment-156579 Share on other sites More sharing options...
paul2463 Posted January 9, 2007 Share Posted January 9, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/33443-getting-newly-entered-datas-id/#findComment-156586 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.