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);

?>
9
  • 1
    echo '<img src="">'? Commented Mar 22, 2017 at 20:27
  • if (mysqli_num_rows($result) > 0) change this first to see if u get any rows back Commented Mar 22, 2017 at 20:29
  • I have multiple images that need to be displayed depending on the user's input from a search form Commented Mar 22, 2017 at 20:31
  • Then after this if (mysqli_num_rows($result) > 0) make a variable $data = mysqli_fetch_array($result); and use foreach or while loop to display results from $data Commented Mar 22, 2017 at 20:35
  • @Mario I am having problems implementing the changes you have told me. Can you kindly help me hardcode on how it would look like? Commented Mar 22, 2017 at 21:03

2 Answers 2

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:

Sign up to request clarification or add additional context in comments.

Comments

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>

5 Comments

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.
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 <img src="image_folder/user_image.jpg"> that is a best solution. If you have already uploaded image you can't know from which user it is.
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 '<img src="images/bookstore.jpg">'
Just receiving a blank page, like it's stuck or something.
add this on top of script under <?php tags error_reporting(1); ini_set('error_reporting', E_ALL);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.