1

I've created 2 pages. One called test.php that just has some html on it with a bog standard form.

<Form name ="form1" Method ="GET" Action ="result.php">

   room number: <INPUT TYPE = "TEXT" Name ="roomId">
   <INPUT TYPE = "Submit" Name = "Submit" VALUE = "Go">

</FORM>

and another page that will process it called results.php.

$roomId = $_POST['roomId'];

            $sql = mysql_query("SELECT * FROM forestcourt WHERE id=$roomId");

            $id2 = 'id';
            $buildingName = 'buildingName';
            $subBuildings = 'subBuildings';
            $imagePath = 'imagePath';
            $description = 'description';


            $rows2 = mysql_fetch_assoc($sql);
            echo 'Name: ' . $rows2[$buildingName] . '<br/>' . 'Sub Buildings: ' . $rows2[$subBuildings] . '<br/>' . 'Description: ' . $rows2[$description] . '<br/>' . 'Location: ' . '<img src="../' . $rows2[$imagePath] . '"/>' . '<br/><br/>';

What I want it to effectively do is grab the value from the input on test.php and store it on the results.php page as $roomId when the submit button is pressed. I've got it to work with other examples but I'm not sure why it's not here... Hopefully I've just made an easy mistake and someone can just point it out!

I know that the page is connected to the database because if I do some commenting on 'results.php' it grabs information from the database.

If I comment out:

$roomId = $_POST['roomId'];

and change:

$sql = mysql_query("SELECT * FROM forestcourt WHERE id=$roomId");

to;

$sql = mysql_query("SELECT * FROM forestcourt WHERE id=1");

the information on the results.php page is the same as the ones in the database.

An error that comes up when I go through the process is;

"Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /websites/123reg/LinuxPackage23/ed/ge/_g/xxx.co.uk/public_html/testing/result.php on line 49"

Line 49 would be:

 $rows2 = mysql_fetch_assoc($sql);

on results.php.

If anyone could shed some light on why it isn't working that would be great. I'm pretty new to MySQL and I've spent the best part of a day on this problem! If any more information is needed I'll quickly try and supply it.

Malachi
  • 3,205
  • 4
  • 29
  • 46
EHU-Lewis
  • 75
  • 1
  • 1
  • 10

3 Answers3

4

You should either change method="post" in test.php or change $roomId = $_GET['roomId']; or $roomId = $REQUEST['roomId']; in results.php

diffie
  • 147
  • 6
  • Oh wow. That's all it was. Thanks for the answer! Could you shed some light on why that happens so I know in the future? Thanks. – EHU-Lewis Sep 15 '13 at 22:35
  • 1
    When you use method="GET" in the form you catch the values in the result.php with the same method ($_GET) and the same goes for POST. That's just how PHP works. You can also use $_REQUEST in results.php to catch values sent with method="GET" as well as method="POST", but this has other issues (http://stackoverflow.com/questions/1924939/php-request-vs-get-and-post) – diffie Sep 16 '13 at 15:45
  • please @diffie add that to your answer – Malachi Oct 08 '13 at 19:58
0

Your action page is result.php not results.php,change your form to POST if you want yout code to work:

<Form name ="form1" Method ="post" Action ="result.php">

   room number: <inpyt type = "text" Name ="roomId">
   <input TYPE = "Submit" Name = "Submit" VALUE = "Go">

</FORM>

and in php , check first that you got value like this :

if(isset($_POST['roomId'])) $roomId = $_POST['roomId'];
Charaf JRA
  • 8,249
  • 1
  • 34
  • 44
0
$sql = mysql_query("SELECT * FROM forestcourt WHERE id=1");

if you want above code or something like $_POST['roomId'] = $roomid to work.

than do the following:

$sql = mysql_query("SELECT * FROM forestcourt WHERE id='1'"); 

or

$sql = mysql_query("SELECT * FROM forestcourt WHERE id='$roomid'");

You might have sorted out but for other people.

of course form method must be POST or GET Accordingly.

Malachi
  • 3,205
  • 4
  • 29
  • 46