Setup Script.. Keep getting unexpected T_CONSTANT_ENCAPSED..

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
doffer
Forum Newbie
Posts: 6
Joined: Mon Jul 18, 2005 10:56 pm

Setup Script.. Keep getting unexpected T_CONSTANT_ENCAPSED..

Post by doffer »

I've written a php setup script for my system, and it's supposed to set up a database table, make .htaccess & .htpasswd files, and create a config.php file. I started writing PHP a few days ago, so I'm quite new in the "game" :) It works like this: The one running the setup.php fills in information and click the submit button, which sends the information entered to setup2.php, which is the script making the files and creating the db table... But when the setup2-script is running, it gets an error at line 72. Line 72 is in the middle of my database query. I can't see why I'm getting this annoying error;
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /usr/home/web/wno14743/hap/setup2.php on line 72
... If someone could tell me what's wrong I would be very gratefull... The codes is posted below... Thanks in advance :)

setup.php

Code: Select all

CSS AND STUFF CROPPED

<body>
<form name=&quote;form1&quote; method=&quote;post&quote; action=&quote;setup2.php&quote;>
  <table width=&quote;610&quote; border=&quote;0&quote; align=&quote;center&quote; cellpadding=&quote;0&quote; cellspacing=&quote;0&quote;>
    <tr>
      <td colspan=&quote;4&quote; class=&quote;part&quote;><div align=&quote;right&quote;>setup: <span class=&quote;style3&quote;>del 1/3</span> </div></td>
    </tr>
    <tr>
      <td colspan=&quote;4&quote; class=&quote;part&quote;>.system</td>
    </tr>
    <tr>
      <td class=&quote;fyll&quote;> </td>
      <td class=&quote;fyll&quote;> </td>
      <td valign=&quote;top&quote; class=&quote;fyll&quote;> </td>
      <td class=&quote;fyll&quote;> </td>
    </tr>
    <tr>
      <td class=&quote;fyll&quote;><p align=&quote;right&quote;>Brukernavn</p>
      </td>
      <td class=&quote;fyll&quote;> </td>
      <td valign=&quote;top&quote; class=&quote;fyll&quote;><input type=&quote;text&quote; name=&quote;user&quote;></td>
      <td class=&quote;fyll&quote;>Brukernavn for "Samler"-systemet</td>
    </tr>
    <tr>
      <td class=&quote;fyll&quote;><div align=&quote;right&quote;>Passord</div></td>
      <td class=&quote;fyll&quote;> </td>
      <td class=&quote;fyll&quote;><input type=&quote;text&quote; name=&quote;pass&quote;></td>
      <td class=&quote;fyll&quote;>Passord for "Samler"-systemet </td>
    </tr>
    <tr>
      <td class=&quote;fyll&quote; valign=&quote;top&quote;><div align=&quote;right&quote;>URL-Adresse til rot </div></td>
      <td class=&quote;fyll&quote;> </td>
      <td class=&quote;fyll&quote; valign=&quote;top&quote;><input type=&quote;text&quote; name=&quote;url&quote;></td>
      <td class=&quote;fyll&quote;>Adressen til mappen du skal ha "Samler". I denne formen: http://doffer.net/samler/</td>
    </tr>
    <tr>
      <td valign=&quote;top&quote; class=&quote;fyll&quote;><div align=&quote;right&quote;>System-Adresse til rot </div></td>
      <td class=&quote;fyll&quote;> </td>
      <td valign=&quote;top&quote; class=&quote;fyll&quote;><input type=&quote;text&quote; name=&quote;path&quote;></td>
      <td class=&quote;fyll&quote;>Serveradressen til mappen du skal ha "Samler". I denne formen: /home/web/wno14743/samler/</td>
    </tr>
    <tr>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td colspan=&quote;4&quote; class=&quote;part&quote;>.database</td>
    </tr>
    <tr>
      <td class=&quote;fyll&quote;> </td>
      <td class=&quote;fyll&quote;> </td>
      <td class=&quote;fyll&quote;> </td>
      <td class=&quote;fyll&quote;> </td>
    </tr>
    <tr>
      <td class=&quote;fyll&quote; width=&quote;200&quote;><div align=&quote;right&quote;>Server-URL</div></td>
      <td class=&quote;fyll&quote; width=&quote;10&quote;><div align=&quote;right&quote;></div></td>
      <td class=&quote;fyll&quote; width=&quote;151&quote;><input type=&quote;text&quote; name=&quote;db_host&quote;></td>
      <td class=&quote;fyll&quote; width=&quote;271&quote;>Adressen til databaseserveren <br>I denne formen: db.doffer.net</td>
    </tr>
    <tr>
      <td class=&quote;fyll&quote;><div align=&quote;right&quote;>DatabaseNavn</div></td>
      <td class=&quote;fyll&quote;><div align=&quote;right&quote;></div></td>
      <td class=&quote;fyll&quote;><p>
        <input type=&quote;text&quote; name=&quote;db_name&quote;>
      </p>
      </td>
      <td class=&quote;fyll&quote;>Navnet på databasen du vil bruke </td>
    </tr>
    <tr>
      <td class=&quote;fyll&quote;><div align=&quote;right&quote;>Brukernavn</div></td>
      <td class=&quote;fyll&quote;> </td>
      <td class=&quote;fyll&quote;><input type=&quote;text&quote; name=&quote;db_user&quote;></td>
      <td class=&quote;fyll&quote;>Brukernavn for databasen </td>
    </tr>
    <tr>
      <td class=&quote;fyll&quote;><div align=&quote;right&quote;>Passorde </div></td>
      <td class=&quote;fyll&quote;> </td>
      <td class=&quote;fyll&quote;><input type=&quote;text&quote; name=&quote;db_pass&quote;></td>
      <td class=&quote;fyll&quote;>Passord til databasen </td>
    </tr>
    <tr>
      <td class=&quote;fyll&quote;><div align=&quote;right&quote;>Tabell</div></td>
      <td class=&quote;fyll&quote;> </td>
      <td class=&quote;fyll&quote; valign=&quote;top&quote;><input type=&quote;text&quote; name=&quote;db_table&quote;></td>
      <td class=&quote;fyll&quote;>Navnet du ønsker på tabellen for Samler </td>
    </tr>
    <tr>
      <td class=&quote;fyll&quote;><div align=&quote;right&quote;></div></td>
      <td class=&quote;fyll&quote; rowspan=&quote;2&quote;> </td>
      <td class=&quote;fyll&quote;> </td>
      <td class=&quote;fyll&quote;> </td>
    </tr>
    <tr>
      <td> </td>
      <td> </td>
      <td><input type=&quote;submit&quote; name=&quote;Submit&quote; value=&quote;Submit&quote;></td>
      
SOME MORE CROPPED
and the error generating file, setup2.php

Code: Select all

<?
//Systemvariabler
$user = $_POST['user'];
$password = $_POST['pass'];
$pass = crypt($password);
$url = $_POST['url'];
$path = $_POST['path'];

//Databasevariabler
$db_host = $_POST['db_host'];
$db_name = $_POST['db_name'];
$db_table = $_POST['db_table'];
$db_user = $_POST['db_user'];
$db_pass = $_POST['db_pass'];

//Opprett .htaccess (ta bort kommentartegnet når setup-filen er ferdig)
 
$data = "AuthName \"Samler - Admin\"
AuthType Basic
AuthUserFile ".$path."
require valid-user";  
$file = "admin/.htaccess";   
if (!$file_handle = fopen($file,"a")) { echo "Cannot open file"; }  
if (!fwrite($file_handle, $data)) { echo "Cannot write to file"; }  
echo ".htaccess-fil er opprettet";   
fclose($file_handle);  

//Opprett .htpasswd (ta bort kommentartegnet når setup-filen er ferdig)

$data = "$user:$pass";  
$file = ".htpasswd";   
if (!$file_handle = fopen($file,"a")) { echo "Cannot open file"; }  
if (!fwrite($file_handle, $data)) { echo "Cannot write to file"; }  
echo ".htaccess-fil er opprettet";   
fclose($file_handle);  


//Opprett config.php (ta bort kommentartegnet når setup-filen er ferdig)

$data = "<?php
//Database-variabler
\$db_host = \"".$db_host."\";
\$db_user = \"".$db_user."\";
\$db_pass = \"".$db_pass."\";
\$db_name = \"".$db_name."\";
\$db_table = \"".$db_table."\";

//Andre variabler
$root = \"".$url."\";
?>";  

$file = "config.php";   
if (!$file_handle = fopen($file,"a")) { echo "Cannot open file"; }  
if (!fwrite($file_handle, $data)) { echo "Cannot write to file"; }  
echo "config-fil er opprettet";   
fclose($file_handle);  


//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("$db_host","$db_user","$db_pass"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("$db_name") or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$query = 'CREATE TABLE `test` ('
        . ' `utgitt` INT(11) NOT NULL, '
        . ' `album` VARCHAR(100), '
        . ' `artist` VARCHAR(100), '
        . ' `bilde` VARCHAR(150), '
        . ' `om` LONGTEXT NOT NULL, '
        . ' `vurdering` TINYINT(4) DEFAULT ''0'' NOT NULL\,'
        . ' `forbokstav` TINYTEXT NOT NULL, '
        . ' `type` ENUM(''1'',''2'',''3'') DEFAULT ''1'' NOT NULL, '
        . ' `isbn` MEDIUMTEXT NOT NULL, '
        . ' `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,'
        . ' PRIMARY KEY (`id`)'
        . ' )';
mysql_query($query) or die("Couldn't execute query");
?>
Hope somebody can see my mistake... Have a nice day...
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Code: Select all

$query = 'CREATE TABLE `test` ('
        . ' `utgitt` INT(11) NOT NULL, '
        . ' `album` VARCHAR(100), '
        . ' `artist` VARCHAR(100), '
        . ' `bilde` VARCHAR(150), '
        . ' `om` LONGTEXT NOT NULL, '
        . ' `vurdering` TINYINT(4) DEFAULT 0 NOT NULL\,'
        . ' `forbokstav` TINYTEXT NOT NULL, '
        . ' `type` ENUM(1,2,3) DEFAULT 1 NOT NULL, '
        . ' `isbn` MEDIUMTEXT NOT NULL, '
        . ' `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,'
        . ' PRIMARY KEY (`id`)'
        . ' )';

THe problem was with the ''1'' ... Try this see if it works.
doffer
Forum Newbie
Posts: 6
Joined: Mon Jul 18, 2005 10:56 pm

worked

Post by doffer »

Thanks neophyte! No I have to figure out why the query ain't beeng run with the db... But that's a bit easier :)

Have a nice day!
doffer
Forum Newbie
Posts: 6
Joined: Mon Jul 18, 2005 10:56 pm

Another problem with the setup file...

Post by doffer »

The problem with the php processing of the query was solved by neophyte... Now another error gets in my way...

My query gets properly parsed in the script. But when running it in the database, it outputs an error... The error message is:
Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' . ' `utgitt` INT(11) NOT NULL, ' . ' `album` VARCHAR(100), '' at line 1
I'm posting the mysql one more time, since I've taken away a few backslashes..

Code: Select all

$query = "CREATE TABLE `$db_table` ('
        . ' `utgitt` INT(11) NOT NULL, '
        . ' `album` VARCHAR(100), '
        . ' `artist` VARCHAR(100), '
        . ' `bilde` VARCHAR(150), '
        . ' `om` LONGTEXT NOT NULL, '
        . ' `vurdering` TINYINT(4) DEFAULT 0 NOT NULL,'
        . ' `forbokstav` TINYTEXT NOT NULL, '
        . ' `type` ENUM(1,2,3) DEFAULT 1 NOT NULL, '
        . ' `isbn` MEDIUMTEXT NOT NULL, '
        . ' `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,'
        . ' PRIMARY KEY (`id`)'
        . ' )";
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Lose all those single quotes... they shouldn't be there ;)

Code: Select all

$query = "CREATE TABLE `$db_table` (
        `utgitt` INT(11) NOT NULL,
        `album` VARCHAR(100),
        `artist` VARCHAR(100),
        `bilde` VARCHAR(150),
        `om` LONGTEXT NOT NULL,
        `vurdering` TINYINT(4) DEFAULT 0 NOT NULL,
        `forbokstav` TINYTEXT NOT NULL,
        `type` ENUM(1,2,3) DEFAULT 1 NOT NULL,
        `isbn` MEDIUMTEXT NOT NULL, 
        `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
        PRIMARY KEY (`id`)
        )";
doffer
Forum Newbie
Posts: 6
Joined: Mon Jul 18, 2005 10:56 pm

Post by doffer »

Thanks! I now know that single quotes shouldn't be in the queries... I used the get php code in phpmyadmin, but I could have used the standard query I think... Think the standard query was pretty much what I ended up with... Changed the ENUM(1,2,3) til INT, so that I might add more content types later :)

The code I ended up with was:

Code: Select all

$query = "CREATE TABLE `$db_table` (
            `utgitt` INT(11) NOT NULL, 
	    `album` VARCHAR(100), 
	    `artist` VARCHAR(100), 
	    `bilde` VARCHAR(150), 
	    `om` LONGTEXT NOT NULL, 
	    `vurdering` TINYINT(4) DEFAULT 0 NOT NULL, 
	    `forbokstav` TINYTEXT NOT NULL, 
	    `type` INT DEFAULT 1 NOT NULL, 
	    `isbn` MEDIUMTEXT NOT NULL, 
	    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) 
	  )";
Thanks for all the help folks! ...and BTW; are you a MUFC fan d11wtq? ..since you live in Manchester :)

My regards
Post Reply