Jump to content

Update multiple tables with one form?


Lee

Recommended Posts

Hi, I am trying to use 1 form to update a page title and a page content. They are both in different tables in the db. I tried the code below, but it won't work. Is there some way I can do it in the same function?

 

function EditHomepage($param)
{
$connect = db_connect();
if(isset($param['submit'])){         

	$errors = array();	

	if(strlen($param['editbody']) < 1) {
		$errors[] = 'Homepage body must be at least 2 characters.';
	}
	 if(strlen($param['edit_title']) == 0) {
		$errors[] = 'You must enter a page title.';
	}
	if($errors) {
		return $errors;
	} 
	else {
		$homepageBody = mysql_real_escape_string($param['editbody']);
		$homepageTitle = mysql_real_escape_string($param['edit_title']);

		$query = sprintf ("UPDATE content, title 
		                              SET 
									      homepage = '$homepageBody',
										  tile,homepage = '$homepageTitle'
										 ");
		    $result = mysql_query($query); 
    
	    if (!$result) {
			return false;
    		} else {
			return true;
    		}                           
	}
}
}

Link to comment
https://forums.phpfreaks.com/topic/197863-update-multiple-tables-with-one-form/
Share on other sites

Ok, I'm quite a noob to SQL (the reason for this practice) so I'll explain my db as in caveman terms. Tables in bold, columns in brackets. So far, it doesn't work.

 

DB structure

content ( homepage,  about_page,  contact_page )

title ( homepage,  about_page,  contact_page )

 

I guess I could just re-structure the db like this:

homepage ( title, content )

about_page ( title, content )

contact_page ( title, content )

But then if I want to add pages, I will need to keep adding more tables. I don't suppose that is so bad..

 

Thanks for the reply :)

I recommend this.

 

content (id,homepage,  about_page,  contact_page )

title (id,homepage,  about_page,  contact_page )

 

set id to 1 then make your query this.

 

$query = sprintf("UPDATE content,title SET homepage = '".$homepageBody."' , title,homepage = '".$homepageTitle."' WHERE id = '1'");

 

again not tested.

 

 

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.