Problem creating-inserting to a table

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
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Problem creating-inserting to a table

Post by jabbaonthedais »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


This is probably something really simple I just can't see. This is just for an admin page of a script I am working on. This part is supposed to create a table (from a variable), grab the files names of a directory on my server, then insert the file names into the new table.

It is creating the table, but not inserting the data. Before I had it creating the table, it was inserting the data into existing tables fine. So its some conflict or something but I just don't see it.

Code: Select all

<?
$db = "******";
$user = "*****";
$pw = "******";

if($email) {
    $mysql_access = mysql_connect("localhost", $user, $pw);
    mysql_select_db($db, $mysql_access);
$rot = "../members/galleries/tmp_" . $email . "/";
$folder=dir($rot);

// create table $email and insert the $folderentries
    $quer = "CREATE TABLE `$email` (`id` INT( 3 ) NOT NULL AUTO_INCREMENT , `filename` VARCHAR( 10 ) NOT NULL , PRIMARY KEY ( `id` ) ,UNIQUE (`filename` )) ";
    mysql_query($quer, $mysql_access);

while($folderEntry=$folder->read()){
 if($folderEntry != "." && $folderEntry != ".." && $folderEntry != "index.htm" && $folderEntry != "large" && $folderEntry != "small")
           {
    $query = "INSERT INTO '$email' ";
    $query .= "VALUES('0', '$folderEntry')";
    mysql_query($query, $mysql_access);

   }
   else
   {
 }
}


// rename directory from tmp_$email to $email
rename("../members/galleries/tmp_" . $email . "/", "../members/galleries/" . $email . "/");

// insert it into gallerylist table (not done yet)


    print("done");

} else {
  print("<form method='POST' action='addgal.php'>");
  print("Please enter gallery number: ");
  print("<input type='text' name='email' size='10'>");
  print("<input type='submit' value='submit'></form>");
}
?>

Thanks guys!!


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

could be a problem with your query or the directory object.. do some error checking.. mysql_error(), and whatever services the directory object has..
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post by jabbaonthedais »

Thanks feyd! It came back with this:
1064: 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 ''110' VALUES('0', '001.jpg')' at line 1
Can you use a period with VARCHAR? I've never needed to put periods in text fields before.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Yes, you can use periods in VARCHAR.

You most likely need to run mysql_real_escape_string() on your variables to escape quotes before sending the SQL query. If that's not it, echo $query to see if the SQL is what you think it should be.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

insert query syntax is invalid..
  1. $email must be a table name
  2. table, database, and field references should be in backticks not single quotes. The latter tells MySQL it's a string, which is the error in your syntax.
Backtick: `
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post by jabbaonthedais »

feyd wrote:insert query syntax is invalid..
  1. $email must be a table name
  2. table, database, and field references should be in backticks not single quotes. The latter tells MySQL it's a string, which is the error in your syntax.
Thanks feyd! That fixed it. :)
Post Reply