Jump to content

MySQL Insert


Jezza

Recommended Posts

  Quote

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..

Link to comment
https://forums.phpfreaks.com/topic/187224-mysql-insert/#findComment-988698
Share on other sites

  Quote

  Quote

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.

Link to comment
https://forums.phpfreaks.com/topic/187224-mysql-insert/#findComment-988717
Share on other sites

http://www.php.net/manual/en/language.basic-syntax.phpmode.php

 

  Quote
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.
Link to comment
https://forums.phpfreaks.com/topic/187224-mysql-insert/#findComment-988722
Share on other sites

  Quote

http://www.php.net/manual/en/language.basic-syntax.phpmode.php

 

  Quote
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

Link to comment
https://forums.phpfreaks.com/topic/187224-mysql-insert/#findComment-988723
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.