View Full Version : php/mySQL, I am stumped
imported_Gryphon
08-16-2002, 01:29 PM
Ok I have a table and I am adding data, well the data stopped at entry 9 an will not add more. Can anyone see the problem?
I hate posting my code btw
#
# Table structure for table `files`
#
CREATE TABLE files (
file_id int(255) NOT NULL auto_increment,
file_name varchar(255) NOT NULL default '',
file_description text NOT NULL,
file_location text NOT NULL,
file_catagory varchar(255) NOT NULL default '',
PRIMARY KEY (file_id)
) TYPE=MyISAM;
# --------------------------------------------------------
$file_sql = "INSERT INTO files VALUES('', '$filename', '$filedescription', '$filelocation', '$filecatagory')";
Grizzly
08-16-2002, 01:44 PM
I don't think you should be inserting into the auto_num field. Try this statement on for size:
$file_sql = "
INSERT INTO files(
file_name,
file_description,
file_location,
file_category)
VALUES(
'$filename',
'$filedescription',
'$filelocation',
'$filecatagory')
";
Specifically defining which columns data is to be inserted into, instead of relying on the ordering of the table's column definition is always a good idea when exec'ing an INSERT query. It also allows you to specifically omit the file_id autonumber field. I'm really not sure how you were able to add 9 records using your original approach in the first place - that kind of confuses me. But either way, try my approach and see what happens, it's worth a shot at least :p
If you still have problems, try posting what ever error message(s) you can - that will help us figure out the root of your problem.
imported_Gryphon
08-16-2002, 01:57 PM
Hmm no luck :/
http://www.thzclan.com/gryphav.gif/sigs.php
http://www.thzclan.com/tcdb
These use this approach I am using here for the filedb
I have this bad habbit of using code that works and pasting it into new files weather it is right or not :/
I am not getting any errors, but I really have no real error checking.
Here is the full thing :/ horrendous I know
<?php
include($DOCUMENT_ROOT."/includes/inc_header.php");
echo"THZ files database - <a href=\"index.php\">files</a><hr>";
// ###### create the initial form ######
function dofileform($addfileaction) {
if ($addfileaction == 'error') {
global $filename;
global $filedescription;
global $filelocation;
global $filecatagory;
$filename = stripslashes($filename);
$filedescription = stripslashes($filedescription);
} else if ($addfileaction == 'clear') {
$filelocation = "http://";
}
echo"<form action=\"admin.php\" method=\"post\">
<input type=\"hidden\" name=\"action\" value=\"addnew\">
<input type=\"text\" name=\"filename\" value=\"$filename\"> Name
<input type=\"text\" name=\"filedescription\" value=\"$filedescription\"> Description
<input type=\"text\" name=\"filelocation\" value=\"$filelocation\"> Location
<input type=\"text\" name=\"filecatagory\" value=\"$filecatagory\"> Catagory
<input type=\"password\" name=\"adminpass\" value=\"\"> Admin Pass
<input type=\"submit\" name=\"submit\" value=\"submit\"> <input type=\"reset\" name=\"reset\" value=\"reset\"></form>";
}
// ###### form for new files ######
if (!$action) {
echo"Status: enter information
";
dofileform(clear);
}
// ###### insert new files ######
if ($action == 'addnew' && $adminpass == $comadminpass) {
$filename = stripslashes($filename);
$filedescription = stripslashes($filedescription);
echo"Status: successfully added $filename to the database
";
dofileform(clear);
do_baseconnect(thzfiles);
$file_sql = "INSERT INTO files(file_name, file_description, file_location, file_category) VALUES('$filename', '$filedescription', '$filelocation', '$filecatagory')";
// $file_sql = "INSERT INTO files VALUES('', '$filename', '$filedescription', '$filelocation', '$filecatagory')";
mysql_query($file_sql);
mysql_close();
}
// ###### lame pass scare tactic ######
else if ($action == 'addnew' && $adminpass != $comadminpass) {
echo"Status: wrong password, ip $REMOTE_ADDR logged
";
dofileform(error);
}
include($DOCUMENT_ROOT."/includes/inc_footer.php");
?>
imported_Gryphon
08-16-2002, 02:04 PM
And here is my file.php, this has gone past 9 entrys :/
Here is where I am implementing this code
http://www.thzclan.com/files
#
# Table structure for table `filestats`
#
CREATE TABLE filestats (
file_stat_id int(255) NOT NULL auto_increment,
file_id varchar(255) NOT NULL default '',
file_referer text NOT NULL,
file_date varchar(255) NOT NULL default '',
file_ip varchar(255) NOT NULL default '',
PRIMARY KEY (file_stat_id)
) TYPE=MyISAM;
<?php
include($DOCUMENT_ROOT."/includes/inc_config.php");
if (!$id) {
header ("location: index.php");
} else {
do_baseconnect(thzfiles);
$result = mysql_query("SELECT file_location FROM files WHERE file_id='$id'");
$filelocation = mysql_result($result,0);
if ($filelocation) {
$filetime = time();
$filesql = "INSERT INTO filestats VALUES('', '$id', '$HTTP_REFERER', '$filetime', '$REMOTE_ADDR')";
mysql_query($filesql);
}
mysql_close();
header ("location: $filelocation");
}
?>
roninblade
08-16-2002, 10:52 PM
can you try this?
(i've added an error echo where you're executing the query)
<?php
include ($DOCUMENT_ROOT."/includes/inc_header.php");
echo 'THZ files database - files (index.php) <hr>';
function dofileform($addfileaction) {
if ($addfileaction == 'error') {
global $filename;
global $filedescription;
global $filelocation;
global $filecatagory;
$filename = stripslashes($filename);
$filedescription = stripslashes($filedescription);
} elseif ($addfileaction == 'clear') {
$filelocation = "http://";
}
echo '<form action="admin.php" method="post">
<input type="hidden" name="action" value="addnew">
<input type="text" name="filename" value="'.$filename.'"> Name
<input type="text" name="filedescription" value="'.$filedescription.'"> Description
<input type="text" name="filelocation" value="'.$filelocation.'"> Location
<input type="text" name="filecatagory" value="'.$filecatagory.'"> Catagory
<input type="password" name="adminpass" value=""> Admin Pass
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" value="reset">
</form>';
}
if (!$action) {
echo 'Status: enter information
';
dofileform('clear');
}
if ($action == 'addnew' && $adminpass == $comadminpass) {
$filename = stripslashes($filename);
$filedescription = stripslashes($filedescription);
dofileform('clear');
do_baseconnect('thzfiles');
$file_sql = "INSERT INTO files set
file_name='$filename', file_description='$filedescription',
file_location='$filelocation', file_category='$filecatagory'";
if (mysql_query($file_sql)) echo 'File '.$filename.' Successfully Added to the Database
';
else echo 'Error Executing Database Query.
Error '.mysql_errno().' : '.mysql_error().'
';
mysql_close();
} elseif ($action == 'addnew' && $adminpass != $comadminpass) {
echo"Status: wrong password, ip $REMOTE_ADDR logged
";
dofileform(error);
}
include($DOCUMENT_ROOT."/includes/inc_footer.php");
?>
imported_Gryphon
08-17-2002, 09:31 PM
Thanks, it appears to be the stripslashes I think. The data I was entering had the title DeLukas's Hover v1.0 and the description When enabled, this script allows you to almost "hover" with your flying craft.
Here is the error
Error Executing Database Query.
Error 1064 : You have an error in your SQL syntax near 's Hover v1.0', file_description='When enabled, this script allows you to almost ' at line 2
The wierd part is that I uploaded yours as admin2.php and submitted, but it posted to admin.php 'cause I forgot to change the form action.
It posted ok, but it only posted Delukas for the file name and posted the full description.
When I changed the form action to admin2.php it returned the error.
roninblade
08-18-2002, 10:52 PM
ok, change the file sql to:
<?php
$file_sql = "INSERT INTO files set
file_name='".mysql_escape_string($filename)."', file_description='".mysql_escape_string($filedescription)."',
file_location='".mysql_escape_string($filelocation)."', file_category='".mysql_escape_string($filecatagory)."'";
?>
this way all the variables can be safely queried into the database.
imported_Gryphon
08-19-2002, 01:59 PM
Yup that works, probably would have been easier had I spelled "catagory" correctly.
Now I have got to learn why it works. It also looks like I have to learn how to use the quotes and apostrophes correctly.
What do the periods do in value="'.$filecatagory.'">, in english?
stuka
08-19-2002, 04:33 PM
The . acts as a string concatenation operator in PHP. (I don't know how to make that any more plain English...) Oh, and the reason that will work (without combing over this in fine detail as I'm sure has been done already) is that you were having your string broken up on the single quote in your description. mysql_escape_string() appropriately escapes all characters that mysql needs escaped before sending to the database engine. Hence, your ' is replaced with \', passing it appropriately.
Strike
08-19-2002, 05:09 PM
Originally posted by Gryphon
Yup that works, probably would have been easier had I spelled "catagory" correctly.
Now I have got to learn why it works. It also looks like I have to learn how to use the quotes and apostrophes correctly.
What do the periods do in value="'.$filecatagory.'">, in english?
uhh ... it is spelled "category" ... just making sure you know that (though I think you do (now), but I want to be sure)
imported_Gryphon
08-20-2002, 02:19 AM
Yes I noticed it from the post prior to that.
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.