Hi I am creating an hotel reservation site for a friend.I found a nice tutorial on html.it and the code runs fine from the command line, no problem. Now I need to build an interface say to insert clients's data. So I have a form.My problem is that for the site to work I need to save last_insert_id to a variable called @codCliente.Can somebody just write for me that line of code so that I can insert it in the php file that handles the form? Please don't send me to some url looking for the answer, I've tried them all. Thank you.
P.S:If you need the whole code please let me know.

If you want that as a PHP variable you can use mysql_insert_id.

$id = mysql_insert_id();

It has to come after the insert query.

Better use AUTO_INCREMENT in table and while insert use all fields except that.This will result in auto save of id without much problem.
Examples of auto increment can be seen here http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

If you still want the id then you can use this query:-
SELECT LAST_INSERT_ID();

Hi thanks for your answer but did not work.You may wonder why I want to do that.The reason is because I don't know any other way. I have not understood yet how to execute MySQL queries from a web page except by using php.If you want to take time to look at this site:http://www.html.it/pag/32165/tutorial-di-esempio-database-prenotazioni-alberghiere-i/
you can probably see what I am trying to do. In the 1st part you create the DB and tables, populate the various tables etc.In the 2nd part you actually execute MySQL queries to insert client's data, reserve the room, calculate the price and supplements etc. and all is revolving around that @codCliente.I am sorry is in Italian but I am sure you can figure it out.As I said if I run everything from the command line or phpMyAdmin it works fine, but my problem is that I have to deliver a web site from which to execute the various tasks. Thank you

MySQL library doesn't support multiple queries, so if you set a MySQL variable as @codCliente it will not be considered by the subsequent query. In order to do so, you have to use MySQLi or PDO libraries.

In other words:

$q = mysql_query('SELECT LAST_INSERT_ID() INTO @codCliente');

will not work. Instead this should work:

<?php

$dbhost = 'hostname';
$dbname = 'database';
$dbuser = 'username';
$dbpass = 'password';

$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", "$dbuser", "$dbpass");

# insert query
$conn->query("INSERT INTO clienti SET nominativo = 'Rossi Mario', indirizzo = 'via Manzi, 2 - 00153 Roma', telefono = '06 86123920'");

# set variable @codCliente
$conn->query('SELECT LAST_INSERT_ID() INTO @codCliente');

# retrieve @codCliente
$cod = $conn->query('SELECT @codCliente as codiceCliente')->fetch();

echo $cod['codiceCliente'];

More information:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.