Passing the ID -SOLVED

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Passing the ID -SOLVED

Post 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');
	}        
?>
Last edited by psurrena on Tue Aug 29, 2006 6:20 pm, edited 1 time in total.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

mysql_insert_id() may be of interest.
User avatar
darodesign
Forum Newbie
Posts: 19
Joined: Mon Aug 28, 2006 8:58 am
Location: Berlin
Contact:

Post by darodesign »

Maybe an Temp Table Can help you ?
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post 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...
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

So the problem is solved? Then please edit the thread's topic to reflect that by adding "[SOLVED]" to the beginning.
Post Reply