Hello. I've created 2 pages that i'm currently working on. One is called; Test.php -- which just includes a basic form in HTML.

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

Building Name: <INPUT TYPE = "TEXT" Name ="buildingName">
Room Number: <INPUT TYPE = "TEXT" Name ="roomId">
<INPUT TYPE = "Submit" Name = "Submit" VALUE = "Go">

</FORM>

The other being a page (called results.php), which grabs the information from the form above when the submit button is clicked and queries a database that it's linked to.

<?php

            $buildingName = $_POST['buildingName'];
            $roomId = $_POST['roomId'];

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


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


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


        ?>

However, this code does work and displays all the relevant information accuratly when I'm only working with ONE text-input field (Room Number: <INPUT TYPE = "TEXT" Name ="roomId">) on the page; test.php.

I've later tried to expand on this to take into account the buildingName too. I have set it up in the exact same way that I did with the roomId and it seems to just spit out an error.

I'm pretty new to PhP and MySQL and as far as I'm aware the SQL;

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

is correct.

If anyone could help me with this matter It would be a great help!

Thanks a bunch,

Lewis.

Recommended Answers

All 6 Replies

Hey Lewis, I don't unerstand what you mean exactly.. It's only working with one of your input fields? Do you mean that only one result from the database is showing?

Sorry that I wasn't specific.

The full code above breaks when it's run.

however, when only having 1 text field on the form, it works

(for example;

<Form name ="form1" Method ="POST" Action ="result.php">
Room Number: <INPUT TYPE = "TEXT" Name ="roomId">
<INPUT TYPE = "Submit" Name = "Submit" VALUE = "Go">
</FORM>

this works -- and with anything in reference to buildingName removed from 'results.php')

I cleaned results up:

<?php

            $buildingName = $_POST['buildingName'];
            $roomId = $_POST['roomId'];

            $sql = mysql_query("SELECT * FROM forestcourt WHERE buildingName=$buildingName AND id=$roomId") or die("There was an error" + mysql_error());           

            $num_rows = mysql_num_rows($sql);

            if(num_rows >= 1)
            {
                 $id2 = 'id';
                 $buildingName = 'buildingName';
                 $subBuildings = 'subBuildings';
                 $imagePath = 'imagePath';
                 $description = 'description';

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



            }

        ?>

Try inserting both text fields and this should give an error, if there is one.. If there isn't one, then I would check whether or not buildingName is actually a field name. Hope this helps

Hello, Phorce. Thanks for the reply.

It seems that what you gave me doesn't seem to give out any information. It doesn't even say that there was an error.

buildingName is a field name in test.php so I can't see it being that.

Thanks,

Lewis.

But.. is buildingName a field name inside your database/table? And contains the data that you are sending through the form? If it isn't, the query will never be true.

<?php

            $buildingName = $_POST['buildingName'];
            $roomId = $_POST['roomId'];

            $sql = mysql_query("SELECT * FROM forestcourt WHERE buildingName=$buildingName AND id=$roomId") or die("There was an error" + mysql_error());           

            $num_rows = mysql_num_rows($sql);

            if(num_rows >= 1)
            {
                 $id2 = 'id';
                 $buildingName = 'buildingName';
                 $subBuildings = 'subBuildings';
                 $imagePath = 'imagePath';
                 $description = 'description';

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



            }else{
              echo "Cannot get data from table";


        ?>

Sorry, I forgot the else statement.

Ah. Sorry I didn't realise you were referring to the table/db.

Yes, it is a field name within the table 'forestcourt'.

I'm going to speak to my lecturer tomorrow morning about this since I cannot seem to figure it out and it's 3:15am.

Thank you for the help though, Phorce.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.