1

I have this form page

<!DOCTYPE html>
<html>
    <head>
        <title>Navigation</title>
    </head>

<body>

<form name="form" method="POST" action="capstone.php">
    <fieldset>
        <p>Where are you trying to go? </p>
        <input type="roominput" name="mapinput" id="mapinput">
<br />
<br />
<input type="submit" name="submit" value="Submit">
</fieldset>
</form>
</body>
</html>

I have this php code that matches user input from the form page with the sql database and echos if the room exists or not.

How do I change this code in order to match user input from the form page with the database and post different images depending on the user input from the form?

 <?php
$conn=mysqli_connect("localhost","xxx","xxx","xxx");
if (mysqli_connect_errno())
{
echo "Connect failed: " . mysqli_connect_error();
exit();
}

if (isset($_POST['mapinput']))
{
 $map = $_POST['mapinput'];
 $query = "SELECT * FROM `Rooms` WHERE `Room Number` ='$map'";
 $result = mysqli_query($conn,$query); 

     if (mysqli_num_rows($result))
        {
           echo 'Room already exists';
        } 

else 
{
echo 'Room does not exist, please try again.';
}

}

mysqli_close($conn);

?>
Raf Robles
  • 11
  • 1
  • 3

2 Answers2

1

type="roominput" that isn't a valid input type.

It needs to be either "text" or "number" if you plan on using integers. The latter being an HTML5 type.

Reference:

Your code is also open to an SQL injection, use a prepared statement:

Note: If the input is to be an integer, then using $map = (int)$_POST['mapinput']; is good to use.

However, you should first check if it is an integer first. There are many ways to check for this, and to name a few:

"How do I change this code in order to match user input from the form page with the database and post different images depending on the user input from the form?"

You fetch results over a successful query with either a while or foreach loop.

Then, echo the image you wish to show. The source of the image will depend on its location, be it from a folder or a BLOB in your database; that part is unknown and are two different methods entirely.

If from a saved file on the server:

<img src="/path/to/images/image_x.jpg">

or using $image = $row['image_in_column']; from a while loop:

<img src="/path/to/images/$image">

as a BLOB:

More examples:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

Here, i can give u this answer like this because i don't know names of database table columns.

Mysqli: capstone.php

<?php

$conn = mysqli_connect("localhost","xxx","xxx","xxx");

if (isset($_POST['submit']))
{
    if (isset($_POST['mapinput']))
    {
        $map = mysqli_real_escape_string($conn, $_POST['mapinput']);

        $query = "SELECT * FROM Rooms WHERE Room Number = '$map'";
        $result = mysqli_query($conn,$query); 

        // check if query returns any result
        if (mysqli_num_rows($result) > 0)
        {
            $data = mysqli_fetch_array($result);

            // loop through yours result displaying data u want
            // and lets say u have image path stored in database
            foreach ($data as $dat)
            {
                echo $dat['column_name'] . ' <img src="'.$dat['image_url'].'"><br />';
            }
        }
        else
        {
            echo 'No results found.';
        } 
    }
    else 
    {
        echo 'Please enter room name.';
    }
}

mysqli_close($conn);

?>

Form :

<form method="POST" action="capstone.php">
<fieldset>
    <p>Where are you trying to go? </p>
    <input type="text" name="mapinput" id="mapinput">
    <input type="submit" name="submit" value="Submit">
</fieldset>
</form>
Mario
  • 518
  • 2
  • 19
  • the images aren't stored in the database. They're just in /var/www/html folder. Are they supposed to be in the database? Also, lets say the user inputs "bookstore" into the form and matches it with the database, how do you bring up its own personal image? Ex: bookstore has its own image, foodcourt has its own image, etc.. but should only be displayed when the user inputs that room and matches with the database. – Raf Robles Mar 22 '17 at 21:55
  • For that its best when user upload a image to store image path to database like ( image_folder/user_image.jpg ) so you can use it later like i give u example in this code and it will be `` that is a best solution. If you have already uploaded image you can't know from which user it is. – Mario Mar 22 '17 at 21:59
  • If its image for a bookstore and you have only 1 image for that, you can use it in `foreach` loop just add if statement `if ($dat['column_name'] == 'bookstore') echo ''` – Mario Mar 22 '17 at 22:06
  • Just receiving a blank page, like it's stuck or something. – Raf Robles Mar 22 '17 at 22:22
  • add this on top of script under ` – Mario Mar 22 '17 at 22:24