Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,466 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,427 people online right now. Registration is fast and FREE... Join Now!




Can Anyone Help?

 
Reply to this topicStart new topic

Can Anyone Help?, PHP

Teessider_2000
post 3 Mar, 2005 - 06:50 AM
Post #1


New D.I.C Head

*
Joined: 3 Mar, 2005
Posts: 3

Can anyone work out why the following short php script should return the error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /export/homepages.cs/w.j.carson/addquestion.php on line 78

<?php

$Q_id=$_POST[qid];

$question=$_POST[question];

$answer1=$_POST[ans1];

$answer2=$_POST[ans2];

$answer3=$_POST[ans3];

$answer4=$_POST[ans4];

$image=$_POST[image];

$answer=$_POST[answer];


require_once('db_login.php');

$check = "SELECT Q_id FROM `question` where Q_id = '$_POST[qid]'";

$result2 = $db->query($check);

$num_rows = mysql_num_rows($result2);

if ($num_rows == 0)
{

$sql = "INSERT INTO `question` (`Q_id`, `Question`, `1`, `2`, `3`, `4`, `Image`, `Answer`) VALUES ('$Q_id', '$question', '$answer1', '$answer2', '$answer3', '$answer4', '$image', '$answer')";


$result = $db->query($sql);
}
else
{
echo "<p>That question id already exists, please select another id.</p>";
}
?>



WHY?????

Cheers in advance.
User is offlineProfile CardPM

Go to the top of the page

snoj
post 3 Mar, 2005 - 07:05 AM
Post #2


$Null

Group Icon
Joined: 31 Mar, 2003
Posts: 3,304



Thanked 5 times

Dream Kudos: 700
My Contributions


That is not all the code.

Besides something is wrong with some MySQL query. Could be the connection.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 3 Mar, 2005 - 07:10 AM
Post #3


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,173



Thanked 33 times

Dream Kudos: 25
My Contributions


The problem is here:
CODE

$check = "SELECT Q_id FROM `question` where Q_id = '$_POST[qid]'";

what format is the variable qid? Is it a number? If so, you should probably use
CODE

$check = "SELECT Q_id FROM `question` where Q_id = ".$_POST[qid];



Of course, I'd be remiss if I didn't mention that you should be validating any variable that comes from a user inputted form.

you may also wish to change the query run line to
CODE

$result2 = mysql_query($check);

and check the results before processing them...something like
[CODE]
$result2 = mysql_query($check);
if (!$result2)
{
die('this query has failed for the following reason: ' . mysql_error());
}
User is offlineProfile CardPM

Go to the top of the page

Teessider_2000
post 3 Mar, 2005 - 07:15 AM
Post #4


New D.I.C Head

*
Joined: 3 Mar, 2005
Posts: 3

Cheers for the reply

Yea it is a number, i tried what you suggested but it still hates the line

$num_rows = mysql_num_rows($result2);

Im really confused by PHP!!!!
crazy.gif
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 3 Mar, 2005 - 07:17 AM
Post #5


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,173



Thanked 33 times

Dream Kudos: 25
My Contributions


Check my edited reply...then you can post the reason it gives. Also, print out the exact query to the screen, then post that.
User is offlineProfile CardPM

Go to the top of the page

Teessider_2000
post 3 Mar, 2005 - 07:26 AM
Post #6


New D.I.C Head

*
Joined: 3 Mar, 2005
Posts: 3

Thanks very much for your help, much aprreciated!
For whatever reason it works now but the sql error still appears at the bottom of the html page!!! Not sure why like!
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 3 Mar, 2005 - 07:40 AM
Post #7


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,173



Thanked 33 times

Dream Kudos: 25
My Contributions


Is it the same error, or one relating to the insert statement...I notive that you're bracleting all your numbers with quotes...generally speaking, numbers do not take quotes for database entry or query, unless the number is being stored in a text based field.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 01:37PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month