Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 06-23-2004, 09:52 AM   #1 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
Resetting a table from a backup copy...

I have a problem with a MySQL/PHP script. It's supposed to take a passed table name, and use it to drop the table and copy the default backup table (with data) to replace it (a sort of reset). The trouble, of course, is that it's not working. IS there something obvious I'm just missing?
PHP Code:
<?
$lnk 
mysql_connect('localhost''username''password') or die (mysql_error()); 
mysql_select_db('databasename',$lnk); 
$daydata=$_POST['passdate'];
$sqldrop "DROP TABLE $daydata"
$sqlreset "CREATE TABLE '$daydata' ('id' tinyint(4) NOT NULL auto_increment, 'start_time' int(11) NOT NULL default '0', 'end_time' int(11) NOT NULL default '0', 'time_label' text NOT NULL, 'name' text NOT NULL, 'image' text NOT NULL, 'info_link' text NOT NULL, PRIMARY KEY ('id')) TYPE=MyISAM; INSERT INTO '$daydata' SELECT * FROM '".$daydata."_Template'"
echo (
$sqldrop);
echo (
$sqlreset);
mysql_query($sqldrop) or die(mysql_error()); ; 
mysql_query($sqlreset) or die(mysql_error()); ;
echo(
"The Schedule for ".$daydata." has been updated.</span>");
?>
metazai is offline   Reply With Quote
Old 06-23-2004, 10:11 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
are there any errors when you run that sql query directly from mysql? or phpmyadmin?
__________________
Mike
sde is offline   Reply With Quote
Old 06-23-2004, 10:17 AM   #3 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
Yes: phpmyadmin ran the drop fine (and so has the script on the page), but the second part, when I replace every instance of '$daydata' with, for instance 'Friday' (one of the names of the tables), I get:

You have an error in your SQL syntax near ''Friday' ('id' tinyint(4) NOT NULL auto_increment, 'start_time' int(11) NOT NULL' at line 1
metazai is offline   Reply With Quote
Old 06-23-2004, 10:23 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
i don't know if this is gonna be correct, but i'd first try to isolate my variables:
PHP Code:
$sqlreset "CREATE TABLE '" $daydata "' ('id' tinyint(4) NOT NULL auto_increment, 'start_time' int(11) NOT NULL default '0', 'end_time' int(11) NOT NULL default '0', 'time_label' text NOT NULL, 'name' text NOT NULL, 'image' text NOT NULL, 'info_link' text NOT NULL, PRIMARY KEY ('id')) TYPE=MyISAM; INSERT INTO '" $daydata "' SELECT * FROM '".$daydata."_Template'"
then, i have never used 2 queries in 1, so i would try to seperate the last query into 2
PHP Code:
$sqlreset "CREATE TABLE '$daydata' ('id' tinyint(4) NOT NULL auto_increment, 'start_time' int(11) NOT NULL default '0', 'end_time' int(11) NOT NULL default '0', 'time_label' text NOT NULL, 'name' text NOT NULL, 'image' text NOT NULL, 'info_link' text NOT NULL, PRIMARY KEY ('id')) TYPE=MyISAM";
$sqlinsert "INSERT INTO '$daydata' SELECT * FROM '".$daydata."_Template'"
last, i didn't think that mysql supported sub-queries, but i guess i'm wrong if it works in phpmyadmin.
__________________
Mike
sde is offline   Reply With Quote
Old 06-23-2004, 10:36 AM   #5 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
What do you mean by "sub-queries"?
metazai is offline   Reply With Quote
Old 06-23-2004, 10:40 AM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
INSERT INTO '$daydata' SELECT * FROM '".$daydata."_Template'

that, .. i thought the 'select' inside an insert is a subquery.
__________________
Mike
sde is offline   Reply With Quote
Old 06-23-2004, 10:51 AM   #7 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
Yeah, that line came from directly from phpmyadmin, actually (I used their PHP code creator button, under "Operations" for a table, to copy structure and content from the table to another with a different name, then I stripped out the fixed name and put in a variable.

Breaking down the queries doesn't seem to help. Again, it runs the first, the DROP, with no problem, but this code:

PHP Code:
<?
$lnk 
mysql_connect('localhost''username''password') or die (mysql_error()); 
mysql_select_db('database',$lnk); 
$daydata=$_POST['passdate'];
$sqldrop "DROP TABLE $daydata"
$sqlreset "CREATE TABLE '$daydata' ('id' tinyint(4) NOT NULL auto_increment, 'start_time' int(11) NOT NULL default '0', 'end_time' int(11) NOT NULL default '0', 'time_label' text NOT NULL, 'name' text NOT NULL, 'image' text NOT NULL, 'info_link' text NOT NULL, PRIMARY KEY ('id')) TYPE=MyISAM"
$sqlinsert "INSERT INTO '$daydata' SELECT * FROM '".$daydata."_Template'";
mysql_query($sqldrop) or die(mysql_error());
mysql_query($sqlreset) or die(mysql_error());
mysql_query($sqlinsert) or die(mysql_error());
echo(
"The Schedule for ".$daydata." has been updated.</span>");
?>
Gives me this error message:

You have an error in your SQL syntax near ''Friday' ('id' tinyint(4) NOT NULL auto_increment, 'start_time' int(11) NOT NULL' at line 1
metazai is offline   Reply With Quote
Old 06-23-2004, 10:57 AM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
did you try isolating the variable? i mean breaking the string up with a "beggining of string" . $variable . "rest of the string";

" . $daydata . "
__________________
Mike
sde is offline   Reply With Quote
Old 06-23-2004, 11:02 AM   #9 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
Oh, sorry . . . yes, I'd tried it before as well, but I just tried it again:

PHP Code:
$sqldrop "DROP TABLE $daydata"
$sqlreset "CREATE TABLE '".$daydata."' ('id' tinyint(4) NOT NULL auto_increment, 'start_time' int(11) NOT NULL default '0', 'end_time' int(11) NOT NULL default '0', 'time_label' text NOT NULL, 'name' text NOT NULL, 'image' text NOT NULL, 'info_link' text NOT NULL, PRIMARY KEY ('id')) TYPE=MyISAM"
$sqlinsert "INSERT INTO '".$daydata."' SELECT * FROM '".$daydata."_Template'"
Resulting in this:
You have an error in your SQL syntax near ''Friday' ('id' tinyint(4) NOT NULL auto_increment, 'start_time' int(11) NOT NULL' at line 1
metazai is offline   Reply With Quote
Old 06-23-2004, 11:08 AM   #10 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
when you run the query in phpmyadmin, it should print out the query up top after it is executed.

can you paste it as phpmyadmin re-prints the query?

then, also paste an echo $sqlreset; to compare it to.
__________________
Mike
sde is offline   Reply With Quote
Old 06-23-2004, 12:14 PM   #11 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
Ok, here's from phpadmin:

Error

SQL-query :

CREATE TABLE 'Friday'(

'id'tinyint( 4 ) NOT NULL auto_increment,
'start_time'int( 11 ) NOT NULL default '0',
'end_time'int( 11 ) NOT NULL default '0',
'time_label'text NOT NULL ,
'name'text NOT NULL ,
'image'text NOT NULL ,
'info_link'text NOT NULL ,
PRIMARY KEY ( 'id' )
) TYPE = MyISAM

MySQL said:


You have an error in your SQL syntax near ''Friday' ('id' tinyint(4) NOT NULL auto_increment, 'start_time' int(11) NOT NULL' at line 1





And here's the echo:

CREATE TABLE 'Friday' ('id' tinyint(4) NOT NULL auto_increment, 'start_time' int(11) NOT NULL default '0', 'end_time' int(11) NOT NULL default '0', 'time_label' text NOT NULL, 'name' text NOT NULL, 'image' text NOT NULL, 'info_link' text NOT NULL, PRIMARY KEY ('id')) TYPE=MyISAMYou have an error in your SQL syntax near ''Friday' ('id' tinyint(4) NOT NULL auto_increment, 'start_time' int(11) NOT NULL' at line 1
metazai is offline   Reply With Quote
Old 06-23-2004, 12:44 PM   #12 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
you are getting that error in phpmadmin then too .. ?? i thought it was working fine in phpadmin.

i think you need back-ticks around your table and column names
PHP Code:
CREATE TABLE `Friday` (
`
idTINYINTUNSIGNED NOT NULL AUTO_INCREMENT ,
`
start_timeINT11 UNSIGNED DEFAULT '0' NOT NULL ,
`
end_timeINT11 UNSIGNED DEFAULT '0' NOT NULL ,
`
time_labelTEXT NOT NULL ,
`
imageTEXT NOT NULL ,
`
info_linkTINYTEXT NOT NULL ,
PRIMARY KEY ( `id` )
); 
__________________
Mike
sde is offline   Reply With Quote
Old 06-23-2004, 12:46 PM   #13 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
yes, back ticks or nothing at all:
PHP Code:
CREATE TABLE Friday (
id TINYINTUNSIGNED NOT NULL AUTO_INCREMENT ,
start_time INT11 UNSIGNED DEFAULT '0' NOT NULL ,
end_time INT11 UNSIGNED DEFAULT '0' NOT NULL ,
time_label TEXT NOT NULL ,
image TEXT NOT NULL ,
info_link TINYTEXT NOT NULL ,
a TINYINT NOT NULL ,
PRIMARY KEY id )
); 
__________________
Mike
sde is offline   Reply With Quote
Old 06-23-2004, 12:48 PM   #14 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
Perhaps I should also post this . . . this is the readout created when the function is executed correctly through phpmyadmin. I've replaced the database name with DATABASENAME, as it would give away something about the site I'm not allowed to, but other than that it's verbatim.

I have tried to rephrase the query with these 'DATABASENAME'. extensions with still no luck. I originally trimmed them out because of this line in my own code:

mysql_select_db('databasename',$lnk);

CREATE TABLE `DATABASENAME`.`Friday` (
`id` tinyint( 4 ) NOT NULL auto_increment,
`start_time` int( 11 ) NOT NULL default '0',
`end_time` int( 11 ) NOT NULL default '0',
`time_label` text NOT NULL ,
`name` text NOT NULL ,
`image` text NOT NULL ,
`info_link` text NOT NULL ,
PRIMARY KEY ( `id` )
) TYPE = MyISAM ;

INSERT INTO `DATABASENAME`.`Friday`
SELECT *
FROM `DATABASENAME`.`Friday_Template`
metazai is offline   Reply With Quote
Old 06-23-2004, 12:49 PM   #15 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
Sorry, just saw your post. Back ticks? Really? Let me try it.
metazai is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Table problems prendo HTML, XML, Javascript, AJAX 12 04-06-2003 05:38 AM
broken mysql table? sde PHP 1 03-31-2003 06:30 PM
help me name a table sde Lounge 4 12-07-2002 12:50 PM


All times are GMT -8. The time now is 04:40 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting