Page 1 of 1

Problem creating-inserting to a table

Posted: Thu Nov 10, 2005 9:25 pm
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]

Posted: Thu Nov 10, 2005 9:55 pm
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..

Posted: Thu Nov 10, 2005 10:31 pm
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.

Posted: Fri Nov 11, 2005 12:12 am
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.

Posted: Fri Nov 11, 2005 12:36 am
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: `

Posted: Fri Nov 11, 2005 1:06 am
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. :)