Jump to content

Insert a blank row


Germaris

Recommended Posts

Hi there!

I wish to insert a blank row in a MySQL table and get as result the id of this row.

Purpose of this operation is to make later an update of this row with collected data.

 

How should I write the query?

 

Many thanks in advance for your help!

 

Best regards,

 

Gerry

Link to comment
https://forums.phpfreaks.com/topic/93336-insert-a-blank-row/
Share on other sites

The function you're looking for is mysql_insert_id().

Orio.

 

Thank you very much!

While you were posting I found by myself a less elegant solution but... also working!  :-)

I'm proud!  :-)

 

$table = "mytable";
$query = "INSERT INTO $table VALUES()";
$res = mysql_query($query) or die ("&error=".mysql_error()."<br/>".query);
$querytwo="SELECT * FROM $table";
$querytwo_res=mysql_query($querytwo) or die("&error=".mysql_error()."<br/>".querytwo);
$count=mysql_num_rows($querytwo_res);
print "&count=".$count;

 

But I prefer yours, of course!

 

Best regards,

Gerry

Link to comment
https://forums.phpfreaks.com/topic/93336-insert-a-blank-row/#findComment-478094
Share on other sites

Finding the number of rows in a database is not an accurate method of finding any information about a row id. Deleting one or more rows make the results incorrect.

 

The method that Orio posted is the correct method of getting the id immediately after an INSERT query.

Link to comment
https://forums.phpfreaks.com/topic/93336-insert-a-blank-row/#findComment-478103
Share on other sites

Finding the number of rows in a database is not an accurate method of finding any information about a row id. Deleting one or more rows make the results incorrect.

 

The method that Orio posted is the correct method of getting the id immediately after an INSERT query.

 

You are right.

Even if my solution is working I recognize Orio's as more secure !  :-)

Link to comment
https://forums.phpfreaks.com/topic/93336-insert-a-blank-row/#findComment-478111
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.