Welcome Guest ( Log In | Register )



3 Pages V   1 2 3 >  
Reply to this topicStart new topic
> Problems With A Database Table & PHP Syntax
nightfox
post Dec 15 2005, 12:49 AM
Post #1


NiGHTFoX - Hiding in the dark
Group Icon

Group: Members
Posts: 680
Joined: 3-April 05
Member No.: 3,584



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:
CODE
$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

This post has been edited by miCRoSCoPiC^eaRthLinG: Dec 15 2005, 03:51 AM
Go to the top of the page
 
+Quote Post
Houdini
post Dec 15 2005, 01:54 AM
Post #2


Super Member
Group Icon

Group: Members
Posts: 572
Joined: 25-April 05
From: Nashville Tennessee
Member No.: 4,340



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
CODE
$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.
Go to the top of the page
 
+Quote Post
miCRoSCoPiC^eaRt...
post Dec 15 2005, 03:41 AM
Post #3


PsYcheDeLiC dR3aMeR
Group Icon

Group: Admin
Posts: 2,242
Joined: 29-January 05
From: Nakorn Chaisri, Thailand
Member No.: 2,411
myCENTs:84.36



QUOTE(nightfox @ Dec 15 2005, 07:49 AM)
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:
CODE
$sql = "INSERT INTO `".$tableprefix."regpage` (`id`, `title`, `content`) VALUES ('"$_POST['id']"', '"$_POST['title']"', '"$_POST['content']"');";
mysql_query($sql);

[N]F
*



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:
CODE
$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:
CODE
$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:
CODE
$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.
Go to the top of the page
 
+Quote Post
nightfox
post Dec 15 2005, 04:57 AM
Post #4


NiGHTFoX - Hiding in the dark
Group Icon

Group: Members
Posts: 680
Joined: 3-April 05
Member No.: 3,584



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! smile.gif

[N]F
Go to the top of the page
 
+Quote Post
nightfox
post Dec 15 2005, 05:28 AM
Post #5


NiGHTFoX - Hiding in the dark
Group Icon

Group: Members
Posts: 680
Joined: 3-April 05
Member No.: 3,584



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.coconutproject.com/beta/make_pa...ge.html?ID=test
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
Go to the top of the page
 
+Quote Post
miCRoSCoPiC^eaRt...
post Dec 15 2005, 07:39 AM
Post #6


PsYcheDeLiC dR3aMeR
Group Icon

Group: Admin
Posts: 2,242
Joined: 29-January 05
From: Nakorn Chaisri, Thailand
Member No.: 2,411
myCENTs:84.36



Try this:

CODE

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.
Go to the top of the page
 
+Quote Post
nightfox
post Dec 15 2005, 09:04 PM
Post #7


NiGHTFoX - Hiding in the dark
Group Icon

Group: Members
Posts: 680
Joined: 3-April 05
Member No.: 3,584



Thanks m^e! I don't know what I would do without your assistance with MySQL! Looks like I may need to sit down and actually read my MySQL & mSQL book even though it is a few years old...

[N]F
Go to the top of the page
 
+Quote Post
nightfox
post Dec 15 2005, 11:26 PM
Post #8


NiGHTFoX - Hiding in the dark
Group Icon

Group: Members
Posts: 680
Joined: 3-April 05
Member No.: 3,584



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.coconutproject.com/page.html?ID=sample

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://www.pixelfull.com/tutorials.php?tutorial=view&id=8

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
Go to the top of the page
 
+Quote Post
miCRoSCoPiC^eaRt...
post Dec 17 2005, 05:54 AM
Post #9


PsYcheDeLiC dR3aMeR
Group Icon

Group: Admin
Posts: 2,242
Joined: 29-January 05
From: Nakorn Chaisri, Thailand
Member No.: 2,411
myCENTs:84.36



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 smile.gif 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 wink.gif
Go to the top of the page
 
+Quote Post
nightfox
post Dec 17 2005, 07:02 AM
Post #10


NiGHTFoX - Hiding in the dark
Group Icon

Group: Members
Posts: 680
Joined: 3-April 05
Member No.: 3,584



Thanks for the book suggestions! I'll look into them. I have several books that don't help much (wrong subject matter, if I needed to create a login system and a links database, I'd be perfect! laugh.gif )

Thanks again m^e! You've been VERY helpful!

[N]F
Go to the top of the page
 
+Quote Post

3 Pages V   1 2 3 >
Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Nero Problems(32)
  2. Creating A Game In Rpg Maker 2000/2003(18)
  3. How To: Connect, Read, Write, Close A Database(5)
  4. Freebsd Burning & Installation Problems(4)
  5. Rakion Problems.(2)
  6. MySQL Output Database Question(18)
  7. Problems Installing Vista(14)
  8. Screensaver Problems(11)
  9. Office 2007 Document Problems(14)
  10. Ajax + Php + Sql = Simply Superb! ( With Visitor Tracking )(11)
  11. Internet Connection Sharing Problems(2)
  12. Updating Graphics Card Problems(11)
  13. Accessing Ms Access Database From A Centralized Location?(5)
  14. Gimp: Saving As... Issues(3)
  15. Image Problems With Windows 2000(10)
  1. Space Needed For Database(10)
  2. Selecting More Than One Table(6)
  3. Some Useful Database Links.(7)
  4. Wamp Server Problems(5)
  5. Database(7)
  6. Best Database(13)
  7. Problems With New Computer(10)
  8. Problems Using Usb Devices(7)
  9. How To Understand A Database Schema(4)
  10. Center In Table Cell With Span(8)
  11. Free Or Opensource Database/schema Browser?(6)
  12. Sql*plus On Your Pc(3)
  13. Email Server / Cpanel Problems(1)


 



- Lo-Fi Version Time is now: 22nd November 2008 - 11:40 PM