Insert into two tables (image / text)

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

Insert into two tables (image / text)

Post by psurrena »

I'm trying to submit an image and text to two different tables in mySQL. I have both potions working seperatly but need help on combining them so when I hit submit, it shares a_id and inserts to two seperate tables.

Here is the code for each:

Article

Code: Select all

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

	if(isset($_POST['add'])) {
		$title	  		= $_POST['title'];
		$abstract		= $_POST['abstract'];
		$full			= $_POST['full'];
		$source	    	= $_POST['source'];
		$date			= $_POST['date'];
		
		$result = move_uploaded_file($tmpName, $filePath);		
	
		if(!get_magic_quotes_gpc()) {
			$title  = addslashes($title);
			$abstract  = addslashes($abstract);
			$full  = addslashes($full);			
		}  
	
		$query = "INSERT INTO article (title, abstract, full, source, date)" . "VALUES ('$title', '$abstract','$full','$source', current_date)";
	
		mysql_query($query) or die('Error, query failed : ' . mysql_error());                    
	
		mysql_close();
	}        
?>
Image

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();
	}        
?>
Beginning form tag

Code: Select all

<form action="" method="post" enctype="multipart/form-data" name="upload">
Any help would be greatly appreciated.
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

After you do one of the inserts, assign a variable the value of mysql_insert_id(). (ex $id = mysql_insert_id();).

Mysql_insert_id() returns ID of the last insert query performed.

Then pass that var in with your query for the id
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

I've been playing with this but if I'm going to do it this way, I would want to pass the variable via the header.

This is how it should go:

add-article.php - header takes you - add-image.php - via the header - upload image - image refers to article id

Code: Select all

<?php
...previous code	

$a_id = mysql_insert_id();			
mysql_close();	
header ('location:add-image.php?a_id=' . $a_id);
?>
When it gets to add-image.php, the a_id=0 when in actuallity it's something like 15

How can I catch the new id?
Post Reply