Jump to content

Recommended Posts

Hiya guys, I'm really quite new to PHP so don't come down too heavy on the simplicity of my question please!

 

I've created a php search page for a website I'm building, with an SQL back-end. The search page needs to pull an image URL from the database and then display it with each search result, similar to any E-commerce site. This is the code im using to try and display the image but I don't know where I'm going wrong! Any help would be appreciated!

 

$search = $_GET['search'];

 

$sql = "SELECT * FROM Prints WHERE name ='$search' ";

 

$rs=mysql_query($sql,$conn);

 

while ($row=mysql_fetch_array($rs))

{

 

echo($row["name"]);

echo("..");

echo($row["artist"]);

echo("..");

echo($row["price"]);

echo("..");

echo<img src='URL'>);

}

 

?>

Link to comment
https://forums.phpfreaks.com/topic/156382-images/
Share on other sites

1) use the proper code tags when posting code

2) Read up on SQL Injection as you are vulnerable :)http://en.wikipedia.org/wiki/SQL_injection

 

<?php
$search = htmlentities($_GET['search']);

$query = sprintf("SELECT * FROM prints WHERE id = '%s' OR name LIKE '%%%s%%' OR manufacturer LIKE '%%%s%%'", $search, $search, $search);

$queryResult = mysql_query($query);
while ($row = mysql_fetch_assoc($queryResult)) {
    echo searchResult($row);
}

function searchResult($row) {
    ..html markup code for a search result..
}
?>

Link to comment
https://forums.phpfreaks.com/topic/156382-images/#findComment-823357
Share on other sites

Hey, thankyou for the advice, I'll give that a read :)

 

I've given the code you suggested a try, but I get nothing but a blank page :S

I'm a bit lost with what you've written so I haven't been able to modify it. All the search needs to do is accept a search term from 'search' and find the name in table 'Prints' and display the relevant data with an image stored in that table using it's URL.

Thankyou for the help!

Link to comment
https://forums.phpfreaks.com/topic/156382-images/#findComment-823367
Share on other sites

Then only use the

 

name LIKE '%%%s%%'

 

part

 

htmlentities is a security measure against sql injection mysql_real_escape_string() is even better but i don't use mysql as a database, the next line in the code is a formatted string or if you use mysqli a prepared statement the double %% represent a % and %s represents a string which means that whatever is passed is converted to a string read up on sprintf() on php.net http://be.php.net/sprintf

 

The function searchResult() is only to make your code more clearer and more easy to read it does this by encapsulating your html code inside a function which gets the required information passed along. This way you can more easily use it again somewhere else in your application.

Link to comment
https://forums.phpfreaks.com/topic/156382-images/#findComment-823368
Share on other sites

This is the code I'm using, but I'm still getting the same result?

 

$search = htmlentities($_GET['search']);

$query = sprintf("SELECT * FROM prints WHERE name LIKE '%%%s%%'", $search, $search, $search);

$queryResult = mysql_query($query);
while ($row = mysql_fetch_assoc($queryResult)) {
    echo searchResult($row);
}

function searchResult($row) {
    echo($row["name"]);
echo("..");
echo($row["artist"]);
echo("..");
echo($row["price"]);
echo("..");
<img src='URL'>;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/156382-images/#findComment-823372
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.