Jump to content
xisto Community
Sign in to follow this  
nightfox1405241487

Problems With A Database Table & PHP Syntax

Recommended Posts

Ok, because I am feeling dangerous and ready to move to the next level, I wrote a simple "page generator" script for my website for several reasons:
1. I hate updating 20+ pages by hand just for ONE little template change
2. I'm expanding my PHP & MySQL knowledge...

Here is an error I get on the page that is supposed to submit contents to the database:
Parse error: parse error, unexpected T_VARIABLE in /home/coco563/public_html/beta/make_page/make_page.php on line 11

Now, on make_page.php, line 11 & 12 (where I feel the problem) is the following:

$sql = "INSERT INTO `".$tableprefix."regpage` (`id`, `title`, `content`) VALUES ('"$_POST['id']"', '"$_POST['title']"', '"$_POST['content']"');";mysql_query($sql);

Also, can someone show me the proper way to make a database? I feel the way I did it must have caused the error.

Thanks!

[N]F
Edited by miCRoSCoPiC^eaRthLinG (see edit history)

Share this post


Link to post
Share on other sites

What is your table prefix and is is defined somewhere like in a configuration file that accesses your database. I would look something like the below

$dbhost = "localhost";$dbuname = "root";$dbpass = "";$dbname = "78";$tableprefix = "somePrefix";$user_prefix = "somePrefix";$dbtype = "MySQL";
The $prefix could be the variable that it doesn't find because it is not referenced or defined elsewhere. The SQL or PHP you have is valid.

Share this post


Link to post
Share on other sites

Here is an error I get on the page that is supposed to submit contents to the database:

Parse error: parse error, unexpected T_VARIABLE in /home/coco563/public_html/beta/make_page/make_page.php on line 11

 

Now, on make_page.php, line 11 & 12 (where I feel the problem) is the following:

$sql = "INSERT INTO `".$tableprefix."regpage` (`id`, `title`, `content`) VALUES ('"$_POST['id']"', '"$_POST['title']"', '"$_POST['content']"');";mysql_query($sql);
[N]F

1064332832[/snapback]


The problem is most definitely here. In PHP, whenever that parse error, unexpected T_VARIABLE kind of message - be sure that it's a problem with either quotes - single or double - not ending/ending prematurely or a similar case with paranthesis (, { or [.

 

Look at your code carefully:

$sql = "INSERT INTO `".$tableprefix."regpage` (`id`, `title`, `content`) VALUES ('"$_POST['id']"', '"$_POST['title']"', '"$_POST['content']"');";

Notice that your second " (double quotes) start from "regpage ..... '" - upto right before $_POST. See how it terminates the string right there leving $_POST attached like a tail to the string before that - but the intrepreter is unable to parse $_POST, as to it, it appears as, "........"$_POST - which doesn't make any sense.

 

The confusion between ' (single) and " (double) quotes is always a BIG issue for any newcomer and even I've faced the same situation as you're facing today. That's why when it comes to quote symbols I learnt to be extra careful and split out my string as clearly as possible - even if it takes more typing to do.

 

Here's what your string should look like:

$sql = "INSERT INTO `" . $tableprefix . "regpage` ( `id`, 'title`, `content` ) VALUES ( '" . $_POST['id'] . "', '" . $_POST ['title'] . "', '" . $_POST ['content'] . "' );";

See how I've extracted the variables that were to be evaluated and put into the string - and plugged them in separately.

 

Since PHP parses any variable-name that begins with a $ and enclosed within double quotes, an alternative approach would be:

$sql = "INSERT INTO `$tableprefix`.`regpage` ( `id`, `title`, `content` ) VALUES ( '$_POST ['id']', '$_POST['title']', '$_POST['content']' )";
Hope this helps.

 

Regards,

m^e

 

P.S. Keep in mind that if your string begins with a " (double) quote - it HAS to end with a double too - and so for the single. Always match up your quotes and see which is getting messed up of ending the string prematurely.

Share this post


Link to post
Share on other sites

Thanks m^e! I seem to miss things like that... once, I had a post-it note with a big semicolon drawn on it with a sharpie so I'd remember to add one at the end of the lines because that was mainly the biggest problem for me.Thanks again! Time to check it out and see how well it works! :huh:[N]F

Share this post


Link to post
Share on other sites

Ok! I finally got it working, except my database table is bad (I don't know how to properly make one) as I guessed on how to do it. I set-up a test page here:
http://www.hugedomains.com/domain_profile.cfm?d=coconutproject&e=com
Yes, images are broken, I know. I saved it into one too many directories on my server (I try to keep my local machine looking like my server). The only problem I have is that NOTHING show up.

I need a TABLE made for me that only includes the following:
-id
-title
-content

ID is NOT a number... when you make a page in the admin area, you specify what you want, like "somepage" and this is what goes in the URL to call the proper content from the database.

Thanks!

[N]F

Share this post


Link to post
Share on other sites

Try this:

 

CREATE TABLE tablename (  `id` VARCHAR ( max_number_of_chars ) NOT NULL,  `title` VARCHAR ( max_number_of_chars ) NOT NULL,  `content` TEXT NOT NULL,  PRIMARY KEY (`id`) );

That should get the proper table up for you. Don't forget to change the max_number_of_chars to whatever maximum characters you want for each field.

Share this post


Link to post
Share on other sites

boy do I feel stupid... ok, my page.html (I have my server set to parse .html as .php) won't display... how do I get it to properly dislplay content from the database?

 

For example, a sample URL with sample data in the database is http://www.hugedomains.com/domain_profile.cfm?d=coconutproject&e=com

 

The tutorial I followed isn't very helpful as I have modded the system into page management since I couldn't find anything like it. The original tutorial is here if it provides any help to you: http://ww2.pixelfull.com/?folio=9POR7JU99

 

Anyway, I need to have it where it checks the "ID" string and it looks up in the database for that ID. Once it gets that I need it to grab the title of the page and the page content. This is where I keep having my problems. I can get the page to not error out, but NOTHING is displayed out of the database.

 

Thanks!

 

[N]F

Share this post


Link to post
Share on other sites

Dude you need two books - these two have been my indispensible companions in respect of MySQL and PHP.

PHP CookBook by David Sklar and Adam Trachtenberg (O'Reilly Pub.)

MySQL CookBook by Paul DuBois (O'Reilly Pub.)

You get these two books - you nip your coding nightmares right in the bud :huh: They've got every little thing you'd ever want to know about coding your pages using these tools.

 

I'm going for a bath - will come back and post some code that might help you sort out your problem :)

Share this post


Link to post
Share on other sites

Here you are - when you pass two parameters like that (tutorial and id) along with the URL, you can check for them in PHP using $_GET. But first you need to check whether the variables contain any value or not - or in other words, whether they've been SET.

 

First we check for tutorial - whether it has been set and the value contained is view - so we know which action to take.

 

if ( isset ( $_GET [ 'tutorial' ] ) && $_GET [ 'tutorial' ] == 'view' ) {.................// Rest of code}

The above code checks whether tutorial contains a value and whether that value is equal to 'view' - if so, next step will be to see whether the id variable has been set, and if so, we use the value to find the right article from the database.

 

if ( isset ( $_GET [ 'tutorial' ] ) && $_GET [ 'tutorial' ] == 'view' ) {    if ( isset ( $_GET [ 'id' ] ) ) {        // Fetch the id of the tutorial and store in a var        $tutorial_id = $_GET [ 'id' ];                // Setup the mysql query string        $query = 'SELECT title, content FROM tablename WHERE `id` = ' . " '$tutorial_id' ";        // Connect to mysql db and store connection ID in a variable        $resid = mysql_connect ( 'localhost', 'username', 'password' );        // Open the required database using the connection ID fetched above        mysql_select_db ( 'database_name', $resid )                // Now query the database using the $tutorialid variable to fetch the correct content        // Use the same connection resource id that was set above        $result = mysql_query ( $query, $resid );                // Now check first whether a tutorial with that ID has been actually returned        // '1' as a result implies that the tutorial exists and has been returned        if ( mysql_num_rows ( $result ) == 1 ) {                // Fetch title - which in the field 0 of returned result        $title = mysql_result ( $result, 0 );  	        // Fetch content - which in the field 1 of returned result        $content = mysql_result ( $result, 1 );  	        // ==============================================================================        //         //      Now add code here to print out the title        //    	and content on the page formatted as you wish        //        // ==============================================================================                } // end if            } // end if} // end if

Follow the comments I've put in along with the code - they should explain each step. If any part is unclear, post back and I'll try to explain further :huh:

Share this post


Link to post
Share on other sites

m^e, everytime I execute the script, it keeps outputting:

Parse error: parse error, unexpected T_VARIABLE in /home/coco563/public_html/test_page.html on line 20

And in Dreamweaver, line 20 is this:
      $result = mysql_query ( $query, $resid );
I'm not quite sure what is wrong with that one line.

[N]F

Share this post


Link to post
Share on other sites

Those books will take care of all your problems - whatever you want to design are given in those books.. that's why I recommend them so strongly.Now this error is usually produced if there's some missing single or double quotes or some parathesis () {} or [] aren't matched up properly - causing strings to end prematurely. Can you paste a little more of the code - lines above this and below this - otherwise hard to tell what the problem is.

Share this post


Link to post
Share on other sites
      // Open the required database using the connection ID fetched above       mysql_select_db ( 'coco563_content', $resid )              // Now query the database using the $tutorialid variable to fetch the correct content       // Use the same connection resource id that was set above       $result = mysql_query ( $query, $resid );              // Now check first whether a tutorial with that ID has been actually returned       // '1' as a result implies that the tutorial exists and has been returned       if ( mysql_num_rows ( $result ) == 1 ) {

It's your code that you posted.... unless something in my template is causing the error?

[N]F

Share this post


Link to post
Share on other sites
// Open the required database using the connection ID fetched above      mysql_select_db ( 'coco563_content', $resid );            // Now query the database using the $tutorialid variable to fetch the correct content      // Use the same connection resource id that was set above      $result = mysql_query ( $query, $resid );            // Now check first whether a tutorial with that ID has been actually returned      // '1' as a result implies that the tutorial exists and has been returned      if ( mysql_num_rows ( $result ) == 1 ) {
I think it was the semicolon missing in the first code line..
The thing with the not ended strings etc. is that the line it outputs as the one causing the error, is not necessarily the one that is "misspelled". You have to search your code from that line upwards till you find an irregularity.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.