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
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 :)

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.