Jezza Posted January 5, 2010 Share Posted January 5, 2010 When I Insert in MySQL i want to grab the ID it auto assigns, take this for example <? $qry = mysql_query("INSERT INTO `Cars` ( `ID`,`Make`,`Model` ) VALUES ( NULL,'$_POST[Make]','$_POST[Model]' ); "); echo "Car ID is _____"; ?> How do i find out the car ID the MySQL database assigned? Quote Link to comment https://forums.phpfreaks.com/topic/187224-mysql-insert/ Share on other sites More sharing options...
oni-kun Posted January 5, 2010 Share Posted January 5, 2010 When I Insert in MySQL i want to grab the ID it auto assigns, take this for example <? $qry = mysql_query("INSERT INTO `Cars` ( `ID`,`Make`,`Model` ) VALUES ( NULL,'$_POST[Make]','$_POST[Model]' ); "); echo "Car ID is _____"; ?> How do i find out the car ID the MySQL database assigned? This should return the last entered ID: <?php $qry = mysql_query("INSERT INTO `Cars` (`ID`,`Make`,`Model`) VALUES (NULL,'$_POST[Make]','$_POST[Model]');"); echo "Car ID is " . mysql_insert_id(); ?> Note you should never use short tags (<?) in PHP. It is widely disallowed and not compatable. EDIT: You should use mysql_real_escape_string on your query to disallow SQL injection.. Quote Link to comment https://forums.phpfreaks.com/topic/187224-mysql-insert/#findComment-988698 Share on other sites More sharing options...
Jezza Posted January 5, 2010 Author Share Posted January 5, 2010 When I Insert in MySQL i want to grab the ID it auto assigns, take this for example <? $qry = mysql_query("INSERT INTO `Cars` ( `ID`,`Make`,`Model` ) VALUES ( NULL,'$_POST[Make]','$_POST[Model]' ); "); echo "Car ID is _____"; ?> How do i find out the car ID the MySQL database assigned? This should return the last entered ID: <?php $qry = mysql_query("INSERT INTO `Cars` (`ID`,`Make`,`Model`) VALUES (NULL,'$_POST[Make]','$_POST[Model]');"); echo "Car ID is " . mysql_insert_id(); ?> Note you should never use short tags (<?) in PHP. It is widely disallowed and not compatable. EDIT: You should use mysql_real_escape_string on your query to disallow SQL injection.. Oh I didn't know it was that simple haha, thanks. Why are <? disallowed and not compatible? I have always used it on my websites and had no problems. Quote Link to comment https://forums.phpfreaks.com/topic/187224-mysql-insert/#findComment-988717 Share on other sites More sharing options...
Mchl Posted January 5, 2010 Share Posted January 5, 2010 http://www.php.net/manual/en/language.basic-syntax.phpmode.php Note: Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags. Quote Link to comment https://forums.phpfreaks.com/topic/187224-mysql-insert/#findComment-988722 Share on other sites More sharing options...
Jezza Posted January 5, 2010 Author Share Posted January 5, 2010 http://www.php.net/manual/en/language.basic-syntax.phpmode.php Note: Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags. Oh i'm fine then, thanks heaps Quote Link to comment https://forums.phpfreaks.com/topic/187224-mysql-insert/#findComment-988723 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.