Page 1 of 1

Passing the ID -SOLVED

Posted: Tue Aug 29, 2006 10:27 am
by psurrena
Situation: The end product is an article with an image. Both are submitted via form, two seperate forms or two seperate pages. On page 1, you enter the article, submit button posts it and the header takes you to page 2. Page two needs the id from the newly inserted article so the image can reference it.

On the page 2 code below, I left the "$a_id = $_GET['a_id'];" code even though I know it doesn't work for this situation. Any idea?

Page 1 PHP:

Code: Select all

<?php
	include '../includes/connect.php';

	if(isset($_POST['add'])) {
		$title	  	= $_POST['title'];
		$abstract	= $_POST['abstract'];
		$full		= $_POST['full'];
		$category 	= $_POST['category'];
		$source	    = $_POST['source'];
		$date		= $_POST['date'];
	
		if(!get_magic_quotes_gpc()) {
			$title     = addslashes($title);
			$abstract  = addslashes($abstract);
			$full      = addslashes($full);	
			$source	   = addslashes($source);
		}  
	
		$query = "INSERT INTO article (title, abstract, full, category, source, date)" . "VALUES ('$title', '$abstract','$full', '$category', '$source', current_date)";
					
		mysql_query($query) or die (mysql_error());			
					
		mysql_close();	
	
	header ('location:add-image.php');
	}  
?>
Page 2 PHP:

Code: Select all

<?php
	include '../includes/connect.php';

	$a_id = $_GET['a_id'];
	
	$uploadDir = 'images/article/';			
	
	if(isset($_POST['upload'])) {
		$fileName         = $_FILES['userfile']['name'];
		$tmpName        = $_FILES['userfile']['tmp_name'];
		$fileSize            = $_FILES['userfile']['size'];
		$fileType           = $_FILES['userfile']['type'];
	
		$filePath 	    = $uploadDir . $fileName;
		$result 		= move_uploaded_file($tmpName, $filePath);
			
		if (!$result) {
			echo "Error uploading file";
			exit;
		}
	
		if(!get_magic_quotes_gpc()) {
			$fileName  = addslashes($fileName);
			$filePath  = addslashes($filePath);
		}  
	
		$query = "INSERT INTO image (name, size, type, path, a_id)" . "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$a_id')";
	
		mysql_query($query) or die('Error, query failed : ' . mysql_error());  
		
		mysql_close();
		
		header ('location:index.php');
	}        
?>

Posted: Tue Aug 29, 2006 10:46 am
by Oren
mysql_insert_id() may be of interest.

Posted: Tue Aug 29, 2006 10:49 am
by darodesign
Maybe an Temp Table Can help you ?

Posted: Tue Aug 29, 2006 12:41 pm
by psurrena
THANK! I used mysql_insert_id() like this:

Code: Select all

<?php

...code...

		$query = "INSERT INTO article (title, abstract, full, source, date)" . "VALUES ('$title', '$abstract', '$full', '$source', current_date)";
					
		mysql_query($query) or die (mysql_error());			
		
		$a_id = mysql_insert_id();
							
		mysql_close();	
	
	header ('location:add-image.php?a_id=' . $a_id);
	}	
?>

Code: Select all

<?php
	include '../includes/connect.php';

	$a_id = $_GET['a_id'];

....rest of code...
?>

Posted: Tue Aug 29, 2006 5:43 pm
by feyd
So the problem is solved? Then please edit the thread's topic to reflect that by adding "[SOLVED]" to the beginning.