I knew this would happen...

etherealFire

New member
Joined
Nov 8, 2004
Messages
3
Location
Indianapolis, Indiana
I'm completely new to SQL and PHP, and I know I could probably find the answer to my own question given enough time, but I want to get a working version of my website up soon.

My problem is with a PHP page I've been writing while following a tutorial that's only vaguely parrallel to my own codes. I'm creating a page on my website so that I can update my database using a form instead of SQL codes, and I've figure out quite a bit, but the mysql_fetch_array is giving me problems. The exact error message is:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/********/domains/**********.*********.***/public_html/newanime.php on line 90

Here's the code I'm using, please let me know what some possible solutions might be. The only thing I can say without a doubt is that it connects to the database, and that the tablenames and column names in the code are exactly as they are on the tables themselves.
_________________________________
<?php
if ($submit):
if ($lid == "") {
echo("<P>You must select a letter " .
"For this anime. Click 'Back' " .
"and try again.</P>");
exit();
}
$dbcnx = @mysql_connect("*******", "********", "********");
mysql_select_db("animedb");
$sql = "INSERT INTO anime SET " .
"Name='$name', " .
"Episodes='$episodes', " .
"Studio='$studio', " .
"Distro='$distro', " .
"Description='$description', " .
"Review='$review', ";

if (mysql_query($sql)) {
echo("<P>New anime series added</P>");
} else {
echo("<P>Error adding new anime: " .
mysql_error() . "</P>");
}
$aid = mysql_insert_id();

$genres = mysql_query("SELECT ID, Name FROM animegenres");
while ($genre = mysql_fetch_array($genres)) {
$gid = $genre["ID"];
$gname = $genre["Name"];
$var = "genre$gid";
if ($$var) {
$sql = "INSERT IGNORE INTO genrelookup " .
"SET AID=$aid, GID=$gid";
$ok = mysql_query($sql);
if ($ok) {
echo("<P>Anime added to genre: $gname</P>");
} else {
echo("<P>Error inserting anime into genre $gname:" .
mysql_error() . "</P>");
}
}
}
?>
<P><A HREF="<?php echo($PHP_SELF); ?>">
Add another anime</A></P>
<P><A HREF="anime.php">Return to Anime Search</A></P>

<?php
else:

$dbcnx = @mysql_connect("*******", "*********", "*******");
mysql_select_db("animedb");

$genres = mysql_query(
"SELECT ID, Name FROM animegenres");
$letters = mysql_query(
"SELECT ID, Name FROM animeletters");
?>

<Form Action="<?php echo($PHP_SELF); ?>" METHOD=POST>

<P>Enter the new anime name:<BR>
<TEXTAREA NAME="Name" ROWS=1 COLS=25 WRAP>
</TEXTAREA>
<BR>

<P>Enter the starting letter:
<SELECT NAME="lid" SIZE=1>
<OPTION SELECTED VALUE="">SELECT ONE
<OPTION VALUE="">--------------
<?php
while ($letter = mysql_fetch_array($letters)) {
$lid = $letter["ID"];
$lname = $letter["Name"];
echo("<OPTION VALUE='$lid'>$lname\n");
}
?>
</SELECT></P>
<BR>

<P>Enter the number of episodes:<BR>
<TEXTAREA NAME="Episodes" ROWS=1 COLS=1 WRAP>
</TEXTAREA>
<BR>

<P>Place the genres this anime applies to:<BR>
<?php
while ($genre = mysql_fetch_array($genres)) {
$gid = $genre["ID"];
$gname = $genre["Name"];
echo("<INPUT TYPE=CHECKBOX NAME='genre$gid'>" .
"$gname<BR>\n");
}
?>
</P>

<BR>
<P>Enter the Production Studio:<BR>
<TEXTAREA NAME="Studio" ROWS=1 COLS=25 WRAP>
</TEXTAREA>
<BR>

<P>Enter the US Distributor:<BR>
<TEXTAREA NAME="Distro" ROWS=1 COLS=25 WRAP>
</TEXTAREA>
<BR>

<Center>
<P>Enter the anime description:<BR>
<TEXTAREA NAME="Description" ROWS=10 COLS=75 WRAP>
</TEXTAREA>
<BR>

<P>Enter the anime review:<BR>
<TEXTAREA NAME="Review" ROWS=10 COLS=75 WRAP>
</TEXTAREA></Center>

<P><INPUT TYPE=SUBMIT NAME="submit" VALUE="SUBMIT"></P>
</Form>
<?php endif; ?>
 
oh yeah

the tables setup is as follows, I realised that it might be hard to figure out even what I was going for with that code... that and the fact that the PHP book I was reading was trying to kill me >.< (I resigned to try in here when I finally passed out in front of the computer after trying to figure out what was wrong for 3 hours)


Table: anime
Columns: id, name, episodes, studio, distro, description, review

id = int not null auto_increment primary_key
name = text not null
episodes = int null
studio = text null
distro = text null
description = mediumtext null
review = mediumtext null


Table: letterlookup
Columns: aid, lid (short for animeid, letterid, this table is "supposed" to link the two tables anime and animeletters)

aid = int not null auto_increment primary_key
lid = int not null


Table: animeletters
Columns: id, name (this table is actually already completely filled out, I just need to use it as a reference to fill in the "lid" section of the letterlookup table)

id = int not null auto_increment primary_key
name = text not null


Table: genrelookup
Columns: aid, gid (animeid, genreid, this table is "supposed" to link the anime and animegenres tables, anim both columns are a joined Primary Key since one anime can have more than one genre)

aid = int not null primary_key
gid = int not null primary_key


Table: animegenres
Columns: id, name (again, this one is already filled out, I just need to it in the code to reference and create an entry for "gid" in the genrelookup table)

id = int not null auto_increment primary_jey
name = text not null
 
The php room on the irc server freenode.net would be better and faster to help you.

But that error indicates the the variable which contains your query (in this case $genres) is not set correctly or is empty altogether which is a more likely case. The way I work out such errors is back track all events that determine what that variable is going to be with echoes to report output for every step, then its really easy to pinpoint the mistake.
 
Simply add:

echo mysql_error($genres);

right before:

while ($genre = mysql_fetch_array($genres)) {

This error means $genres isn't pointing to a result sets it can use
 
thanks

Thanks guys, I figured it out and it's working beautifully now ^.^ I wasn't quite aware of what this forum was for I suppose, I merely found the link to it on my control panel and thought it might be a good place to ask for help. Thanks for helping even though you could've told me to shove off, I'll try that irc channel you mentioned next time I need help with coding something ^.^ arigatou gosaimasta minna. Ja ne
 
Back
Top