tags:

views:

48

answers:

4

I've created a code to view the data. the data can't display at the form...

the code :

<?php //======================================================================================================================= PROCESS DATA ======================================================= START. 
include "connectioncomplaint.php";
?>

<?php
$subject = $_GET['type'];
$comment = $_GET['id'];

//echo 'test : ' . $name;
//Tarik data dari sini 
$queryDetail = " SELECT * FROM campuscomplaint " . " WHERE subject = '" . $subject . "' AND comment = '" . $comment . "' ";
//echo 'QUERY DETAIL :' . $queryDetail . '<br>' ;

$resultDetail = mysql_query($queryDetail);
//echo 'RESULT DETAIL :' . $resultDetail + 0 . '<br>' ;

$detail = mysql_fetch_array($resultDetail);

//echo $detail . '<br>';
//echo 'detail subject is : ' . $detail['subject'] . '<br>';
//echo 'detail comment is : ' . $detail['comment'] . '<br>';
//echo $detail[$x] . '<br>';

?>

code for form:

<tr>
                  <td bordercolor="#FFFFFF" bgcolor="#FFFFFF" class="register style5">From:</td>
                  <td bordercolor="#FFFFFF" bgcolor="#FFFFFF" class="register style5"><input type="text" name="to" 
size="40" maxlength="80" value="<?php echo $detail['userid']; ?>"/></td>
                </tr>
                <tr>
                  <td width="38%" bordercolor="#FFFFFF" bgcolor="#FFFFFF" class="register style5">Subject:</td>
                  <td width="62%" bordercolor="#FFFFFF" bgcolor="#FFFFFF" class="register style5"><input type="text" name="subject" size="40" maxlength="80" value="<?php echo $detail['subject']; ?>"/></td>
                </tr>
                <tr>
                  <td bordercolor="#FFFFFF" bgcolor="#FFFFFF" class="register style5">Comment:</td>
                  <td bordercolor="#FFFFFF" bgcolor="#FFFFFF" class="register style5"><textarea name="comment" rows="5" cols="40"><?php echo $detail['message']; ?></textarea></td>
                </tr>
                <tr>
                  <td bordercolor="#FFFFFF" bgcolor="#FFFFFF" class="register style5"><p>&nbsp;</p>
                      <p>&nbsp;</p></td>
                  <td bordercolor="#FFFFFF" bgcolor="#FFFFFF" class="register style5"><input type="submit" name="submit" value="Submit Comment" onClick="return OnButton1();"/></td>
                </tr>
A: 

Change this to do:

$resulDetail = mysql_query($queryDetail);

to

$resulDetail = mysql_query($queryDetail) or die(mysql_error());

and post the error you get.

Pentium10
the error still same.....Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\e-Complaint(FYP)\userView.php on line 101
shimaTun
your sql query is wrong, print it
Pentium10
A: 

Considering your code :

$resulDetail = mysql_query($queryDetail);
$detail = mysql_fetch_array($resultDetail);

This kind of error is likely to happen when mysql_query fails -- and return false ; which means $resultDetail is not a resource, and cannot be used as a parameter to mysql_fetch_array.


This means you should test if $resultDetail is not false before calling mysql_fetch_array :

$resulDetail = mysql_query($queryDetail);
if ($resulDetail === false) {
    // while developping, you can echo the error message
    // in production, you should log it to a file
    echo mysql_error();
} else {
    // query has been run OK
    // => You can use the result :
    $detail = mysql_fetch_array($resultDetail);
    // ...
}


Humph : looking closer at your code, you have this :

$resulDetail = mysql_query($queryDetail);
$detail = mysql_fetch_array($resultDetail);

In the first line, the result of mysql_query is stored to $resulDetail, with no t !
And, in the second line, you are using $resultDetail, with a t !

i.e. the variable used for mysql_fetch_array doesn't exist -- hence is considered as null -- not a valid resource.


As a sidenote, even if this is not the cause of the problem in this specific case, but you are building your query like this :

$subject = $_GET['type'];
$comment = $_GET['id'];
$queryDetail = " SELECT * FROM campuscomplaint " . " WHERE subject = '" . $subject . "' AND comment = '" . $comment . "' ";

Which means you are not escaping the data before injecting it into the SQL query -- which is bad and can lead to SQL injections (which can cause an error while executing the query)

You should use mysql_real_escape_string on those strings, before injecting them into the SQL query :

$subject = mysql_real_escape_string($_GET['type']);
$comment = mysql_real_escape_string($_GET['id']);
$queryDetail = " SELECT * FROM campuscomplaint " . " WHERE subject = '" . $subject . "' AND comment = '" . $comment . "' ";
Pascal MARTIN
i not understand....can u explain more detail....
shimaTun
Heu... which part did you not understand ?
Pascal MARTIN
the data still can't display....
shimaTun
A: 

Your $resultDetail is not a resource, and cannot be used as a parameter to mysql_fetch_array(). from my practise - test all your queries with mysql "sql"! u can find more detailed information about an error there!!!

Syom
A: 

shimaTun, as you commented in a previous answer, "supplied argument is not a valid MySQL result resource". This means that your query is invalid, and mysql_query() is returning a mysql ERROR resource, not a RESULT resource. Change your code to the following:

$resultDetail = mysql_query($queryDetail);
if (mysql_error()) {
   die("MySQL error: " . mysql_error());
}

and re-run the script. That will spit out the exact error that the query is causing.

Incidentally, what's with all the "bordercolor" and "bgcolor" attributes in your td tags? You're obviously using CSS, since there's "class" attributes as well. Why not save a few kilobytes of bandwidth by styling your table cells with the style sheet? It also makes the HTML far easier to read.

You'll also want to fix up how the HTML is generated. Consider the case where the data you're inserting into the form contains a double quote or a tag:

<textarea name="comment" rows="5" cols="40"><?php echo $detail['message']; ?></textarea>

What if $detail['message'] looked like "Hello, </textarea> I'm outside your tags, messing with your HTML". The generated html will look like:

<textarea name="comment" rows="5" cols="40">Hello, </textarea> I'm outside your tags, messing with your HTML</textarea>

and now part of comment is gone, reduced to just Hello,, because it's broken out from the containing <textarea>. Unless your form data is stored pre-escaped (generally a bad idea), you should ALWAYS output it into the form via the htmlspecialchars() function, like this:

<textarea><?php echo htmlspecialchars($detail['message']) ?></textarea>

which will nicely translate <, >, &, ' and " into their character-entity equivalents.

Marc B