is there any way to get the id you are going to insert the data in before you post the data, like get the last id and do a +1 to it or something pleaseee?

select max(id) from table will give you the max id used.

Be careful, you can't rely on this being the id that will actually be used.

First of all, why?

I would just insert the data, then use mysql_insert_id() to get the id.

But if you really need to get it before, here is a function:

function getNextID() {
  $query = mysql_query("SELECT `id` FROM `table_name` ORDER BY `id` DESC LIMIT 1") or die(mysql_error());
  if ( mysql_num_rows( $query ) == 1 ) {
    list( $id ) = mysql_fetch_row( $query );
    return (int) $id + 1;
  }
  return false;
}

or use pritaeas solution.

First of all, why?

I would just insert the data, then use mysql_insert_id() to get the id.

But if you really need to get it before, here is a function:

function getNextID() {
  $query = mysql_query("SELECT `id` FROM `table_name` ORDER BY `id` DESC LIMIT 1") or die(mysql_error());
  if ( mysql_num_rows( $query ) == 1 ) {
    list( $id ) = mysql_fetch_row( $query );
    return (int) $id + 1;
  }
  return false;
}

or use pritaeas solution.

hi mate first of all thanks for your reply.

i am going to explain my self better so maybe you can understand the issue more as i got you confused i think. so

i have the following code

<?php 

include_once 'core/modules/FCKeditor/fckeditor.php';

include 'includes/php/authentication.php';
include 'includes/php/timeout.php';
include 'templates/default.php';

if(isset($_POST['save']))
{
	$name = $_POST['name'];
	$title = $_POST['title'];
	$description = $_POST['description'];
	$keywords = $_POST['keywords'];
	$content = $_POST['content'];
    $updated = date("D, jS F, Y");
	
	if(!get_magic_quotes_gpc())
	{
		$name = addslashes($name);
		$title = addslashes($title);
		$description = addslashes($description);
		$keywords = addslashes($keywords);
		$content = addslashes($content);
	}
	
	include 'core/config.php';
	include 'core/opendb.php';

	mysql_query("INSERT INTO pages 
	(name, title, description, keywords, content, updated) VALUES('$name', '$title', '$description', '$keywords', '$content', '$updated' ) ") 
	or die(mysql_error());
	

	
	// Start Create php page file
	
	$filename = "$name.php";
	$filehandle = fopen($filename, 'w') or die("can't open file");

	fwrite($filehandle, $template);

	fclose($filehandle);
	
	// End Create php page file
	
	include 'core/closedb.php';

	header("location:default.php?s=listpages");
	}
?>

this code posts data to the db and at the end creats a .php page with provided data from the form.

$template is the content that is going to be on the actual .php page created.

here is $template

<?
$template = <<<TEMPLATE
<?php
include '../core/config.php';
include '../core/opendb.php';

$queryo = "SELECT id, name, title, description, keywords, content ". "WHERE id = 'i want the id for here'";
$resulto = mysql_query($queryo) or die('Error : ' . mysql_error());
list($id, $name, $title, $description, $keywords, $content) = mysql_fetch_array($resulto, MYSQL_NUM);

include '../core/closedb.php';
?>
TEMPLATE;
?>

as you can see i want the id before i post so i can echo/get it or something in the template where i have "WHERE ID ="". like this the .php page will get me the data :) hope i made my self more understandable this time hehe

This article has been dead for over six months. Start a new discussion instead.