I took into consideration what people said and I am trying to write a script that reads a directory, then puts the info into a mysql database:
<?php
$farray = glob('*.*');
natcasesort($farray);
foreach ($farray as $fname) {
include("dbconnect.php");
//Don't worry about this part
$result = mysql_query('INSERT INTO `roms_info` ( `rom_id` , `name` , `size` , `ss_1` , `ss_2` , `downloads` , `country` , `dl_url` , `system` )
//Insert the values
VALUES (
//Insert the rom ID. Remember to keep the numbers from overlapping
\'+1\',
//Insert the roms name
\'$fname\',
//Insert the file size in KB
\'filesize($fname)\',
//Keep this as the default
\'upload.png\',
//Ignore this
\'\',
//This is the download number, leave this at 0
\'0\',
//This is the country logo
//If the rom is european -which most of the roms arent- put europe.png
//If the rom is japanese -which most of the roms arent-put japan.png
\'usa.png\',
//This is the file name that is shown in the ftp. Copy as you see it there
\'$fname\',
//The system for the rom
\'SNES\'
)')
or die(mysql_error());
echo 'It worked';
}
?>
And I'm getting this error:
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 '//Insert the values VALUES ( //Insert the rom ID. Remember to
I know I did somethign wrong and I probably screwed the script. Any help.
Thanks :)
Oh and I don't know howto for each file it sees, it adds +1 as the file ID. I'm know I'm really wrong right here :(
You can't comment inside a query :)
So delete all the comments inside your query
[edit]oh, and about that ID+1: you have to set your column ID to 'auto_increment' (you can do this in phpmyadmin)[/edit]
Ooo didn't know that. I will try that ;)
*watches my jaw drop*
errr
www.dohgames.com/roms/snes/showfile.php
:'(
Please helpĀ :(
It has to do with the variables...I believe so....
There are many things you could do to improve that code but here's two important ones
Firstly, put the incldue statement OUTSIDE the foreach statement.
Secondly, the problem is in the query $fname is INSIDE a ' ' string - it needs to be changed to look like:
\'' . $fname . '\'
instead of:
\'$fname\'
Personally I'd change the query to be inside " quotes instead then you don't need to escape the ' symbols. In your example would mean:
$result = mysql_query("INSERT INTO roms_info ( rom_id , name , size , ss_1 , ss_2 , downloads , country , dl_url , system )
VALUES ( '+1', '$fname', filesize($fname), 'upload.png', '', '', 'usa.png', '$fname', 'SNES')") or die... (etc)
That should work :)
Note your code is still inefficient. Your best bet is to do the insert in one go (in the foreach loop build up a string of all the inserts and then stick them in at once - but the code above should work fine me thinks
Nope parse error on line seven
<?php
include("dbconnect.php");
$farray = glob('*.*');
natcasesort($farray);
foreach ($farray as $fname) {
$result = mysql_query("INSERT INTO roms_info ( rom_id , name , size , ss_1 , ss_2 , downloads , country , dl_url , system )
VALUES ( '+1', '$fname', filesize($fname), 'upload.png', '', '', 'usa.png', '$fname', 'SNES')")')
or die(mysql_error());
}
?>
Worked
Hold on I got rid of an extra ')
and heres what i get :
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 '(3 Ninjas Strike Back.zip), 'upload.png', '', '0', 'usa.png', '
Change
filesize($fname)
to
'filesize($fname)'
Well its not increasing the ID +1 at all. Its staying on one.
That isn't a valid command as far as I was aware.
Set the extra for ID to "auto_increment" and set the type to bigint or something.
^ What he said. +1 will not work. Set rom_id to auto_incremement using phpmyadmin and then just remove that column from that insert command. (It will auto inc it for you automatically)
www.dohgames.com/roms/n64/showfile.php
It works so far but now with teh file size... :-\
filesize needs an absolute position of the file and $fname looks like it's just the filename, so you need to add the directory where the files are to the query
e.g
filesize('/usr/ftp/snes/' . $fname)
Thanks for the help so far but yet another error:
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 ''. 1080 Snowboarding.zip)', 'upload.png', '', '0', 'usa.png', '1
<?php
include("dbconnect.php");
$farray = glob('*.*');
natcasesort($farray);
foreach ($farray as $fname) {
$result = mysql_query("INSERT INTO roms_n64 ( name , size , ss_1 , ss_2 , downloads , country , dl_url , system , rate_up , rate_down )
VALUES ( '$fname', 'filesize('/home/dohgames/public_html/roms/n64'. $fname)', 'upload.png', '', '0', 'usa.png', '$fname', 'N64' , '0' , '0')")
or die(mysql_error());
}
?>
Worked
Try this...
<?php
include("dbconnect.php");
$farray = glob('*.*');
natcasesort($farray);
foreach ($farray as $fname) {
$filesize = filesize('/home/dohgames/public_html/roms/n64'. $fname);
$result = mysql_query("INSERT INTO roms_n64 ( name , size , ss_1 , ss_2 , downloads , country , dl_url , system , rate_up , rate_down )
VALUES ( '$fname', '$filesize', 'upload.png', '', '0', 'usa.png', '$fname', 'N64' , '0' , '0')")
or die(mysql_error());
}
?>
The single quotes just needed to be escaped, but I don't like putting PHP commands in queries... just my thing though
Ah thanks a bunch :D
Gots another question...
I entered from information for one and I want to update the system colum for each row... Heres' what i have:
<?php include("dbconnect.php");
$result2=mysql_query("UPDATE roms_snes SET system=Super Nintendo WHERE system=Sega Genesis");
echo '<a href="javascript:history.back(1)">Thank You. Click here to return.</a>';
?>
But it wont work. Thanks
system=Super Nintendo
How does it know you want it to be "Super Nintendo"? It can't read your mind... maybe you mean this:
UPDATE roms_snes
SET system='Super', Nintendo=''
WHERE system='Sega', Genesis = '';
You need to use quotation marks.
UPDATE roms_snes
SET system='Super Nintendo'
WHERE system='Sega Genesis';
-[Unknown]
That's teh only thing I hate about PHP... stupid quotations and apostrophes...
Thanks again :)
That's the thing I love about PHP, no misunderstandings :)