PHP SQL search result as POST / GET link - php

I have problems to get the right GET-method for my results.
I have a script which shows me some results of my database. It works with Ajax:
$suchbegriff = $_POST["suchbegriff"];
if ($_POST["suchbegriff"]){
$sql = "SELECT * FROM user WHERE vorname OR nachname LIKE '%$suchbegriff%'";
$result = $conn->query($sql);
echo "<br>Sie suchten nach: ".$suchbegriff."<br>";
while($row = $result->fetch_assoc())
{ echo "<form action=\"profil.php\" method=\"get\">".$row['nachname'];
echo $row['vorname']."</a> <input type=\"submit\">";
echo "<br/>";}
}
Now I want to get the ID of the search-results to bring it with GET/POST to an other page called profil.php (unique user-profile):
<div class="col-xl-12">
<div class="col-xl-2">
<p>Vorname</p>
</div>
<div class="col-xl-5">
<p><?php
$sql = "SELECT vorname FROM user WHERE ID = $_GET[nachname]";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row['vorname'];
?></p>
</div>
</div>
Is there a way to couple nachname and vorname (name, surname) in the SQL-Statement?
And how can I get the unique profile-URL from the search-results?
Thank you.

Change your first script to print a list of anchors, and put the ID into the href URL.
while($row = $result->fetch_assoc())
{
echo "".$row['nachname']. " ".$row['vorname']."";
echo "<br/>";
}

Related

How to select an image from retrieved data

I'm writing an html code to retrieve the details of the item table in my database.In it, I have some columns and one such column name is Image.It stores images of the Items and it's type is blob.I have written the following code to retrieve and display the data in this table in my php file.
$sql = "SELECT * FROM item where item.Category = 'Tops' limit 3";
$result = $conn->query($sql);
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$id = $row["Item_ID"];
echo " <div class='ite'>
<div class='card'>
<img src=".$row["Image"]." alt=".$row["Item_Name"]." class='imge'>
<h3>".$row["Item_Name"]."</h3>
<p class='price'>Rs. ".$row["Price"]."</p>
<p><button type='submit' onclick='view_data($id)'>View Item</button></p>
</div>
</div>";
}
}
The code works perfectly for the other columns except the image column(<img src=".$row["Image"]." alt=".$row["Item_Name"]." class='imge'>)
The output is as follows.
It gives garbage values like this.How can I fix this error and display the image stored in the database?
<img src='data:image/jpeg;base64," . base64_encode($row['Image']) . "' class='imge'>
This works!Thanks for the support!

Weird SQL query behavior - Issue with SIC code lookup

I am creating an SIC code lookup and I am having a weird issue with my query. I have 6 table names, description, description-2, description-3. and two-digit-sic, four-digit-sic, and six-digit-sic and I am getting unexpected results.
If I search for an item in any of the description tables (example: agriculture) it will return the correct queries. and it will display as it should. As well as if I only search for a two-digit-sic. (example: 01)
However if I try and search via six-digit-sic or four-digit-sic It will not display the records.
A weird thing is when I search for the correct six-digit-sic it will say it found records but just not display them. if you put in an incorrect sic code it will just say no records found. example of working six-digit-sic (011198) non-working SIC (112112)
I am truly at a loss as to why this is happening.
this is live here: http://mainstaycomputing.com/client/prospects/list-brokers-resources/sic-code-search/
<?php
get_header();
?>
<div id="main-content" style = 'position: relative'>
<div class = 'wrapper'>
<h1 class="entry-title main_title"><?php the_title(); ?></h1>
<div class = 'sic-text'>
<p> SIC codes are assigned by the Government and are standard at the 4 digit level.
The 8 digit codes may be customized by each individual list owner. Because we represent all list
sources, there may be variance in what the 8 digit codes represent. For greatest accuracy,
when speaking with one of our List Brokers please supply the sic code # along with a description so
we can provide as exact a match as possible.To use this search, simply type the Industry you’re
looking for into the Search By Keyword field. For instance, entering “Dentists” will cause all
businesses related to dentists listed. <! — If you know the SIC code and want to know the industry
name, enter the 8 digit code into the Search By Code field. –> </p>
</div>
<form action="" method="GET" class = 'sic-search'>
<input class = 'sic-search-text' type="text" name="search" placeholder="Search for an industry, eg 'Agriculture'"/>
<input type="submit" value="Search" class = 'sic-search-button'/>
</form>
<?php
$search = $_POST['search'];
$host = "****";
$user = "****";
$password = "****";
$database_name = "****";
$min_length = 2;
// you can set minimum length of the sic if you want
if(strlen($search) >= $min_length && $search != ''){ // if sic length is more or equal minimum length then
echo "<p id='rowCount'> </p>";
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
// Search from MySQL database table
$search=$_POST['search'];
$query = $pdo->prepare("select * from siccodes where description LIKE ? OR description-2 LIKE ?");
$query->bindValue(2, "%$search%", PDO::PARAM_STR);
$query->execute();
// Display search result
if (!$query->rowCount() == 0) {
echo "Search found :<br/>";
echo "<table class='sic-code-table'";
echo "<tr><th>description</th><th>two-digit-sic</th><th>description</th><th>four-digit-sic</th><th>description</th><th>six-digit-sic</th></tr>";
while ($results = $query->fetch()) {
echo "<tr><td>";
echo $results['description'];
echo "</td><td>";
echo $results['two-digit-sic'];
echo "</td><td>";
echo $results['description-2'];
echo "</td><td>";
echo $results['four-digit-sic'];
echo "</td><td>";
echo $results['description-3'];
echo "</td><td>";
echo $results['six-digit-sic'];
echo "</tr>";
}
echo "</table>";
} else {
echo "<p style = 'text-align:center; margin-bottom:30px; color: red;'>No results match" . " '" . $search . "' " . "Please try another search term.</p>";
}
} else {
if (!empty($_POST['search'])) {
echo "<p style = 'text-align:center; margin-bottom:30px; color: red;'> Please search again, '$search' is to short.</p>";
}
}
$host = "****";
$user = "***";
$password = "***!";
$database_name = "*****";
mysql_connect("****", "****", "****") or die("Error connecting to database: ".mysql_error());
/*
localhost - it's location of the mysql server, usually localhost
root - your username
third is your password
if connection fails it will stop loading the page and display an error
*/
mysql_select_db("****") or die(mysql_error());
/* tutorial_search is the name of database we've created */
?>
<?php
$query = $_GET['search'];
// gets value sent over search form
// if the query is null which it would be when they first enter the page then show all results
if(empty($_GET['search'])) {
// $query = '01';
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT * FROM siccodes ") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`, `title`, `text `
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example if $query is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
echo "<table class='sic-code-table'";
echo "<tr><th>description</th><th>two-digit-sic</th><th>description</th><th>four-digit-sic</th><th>description</th><th>six-digit-sic</th></tr>";
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo "<tr><td>";
echo $results['description'];
echo "</td><td>";
echo $results['two-digit-sic'];
echo "</td><td>";
echo $results['description-2'];
echo "</td><td>";
echo $results['four-digit-sic'];
echo "</td><td>";
echo $results['description-3'];
echo "</td><td>";
echo $results['six-digit-sic'];
echo "</tr>";
// posts results gotten from database(title and text) you can also show id ($results['description'])
}
echo "</table>";
}
} // end of displaying all results
/* this is the actual search */
$min_length = 2;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT * FROM siccodes
WHERE (`description` LIKE '%".$query."%') OR (`description-2` LIKE '%".$query."%') OR (`description-3` LIKE '%".$query."%') OR (`two-digit-sic` = '$query') OR (`four-digit-sic` = '$query') OR (`six-digit-sic` = '$query')") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`, `title`, `text `
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example if $query is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
$raw_num_rows = mysql_num_rows($raw_results);
$num_rows = $raw_num_rows - 1;
echo "<p id='rowCount'> We have retrieved " . " " . $num_rows . " " . " records related to the keyword '$query'</p>";
echo "<table class='sic-code-table'";
echo "<tr><th>description</th><th>two-digit-sic</th><th>description</th><th>four-digit-sic</th><th>description</th><th>six-digit-sic</th></tr>";
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo "<tr><td>";
echo $results['description'];
echo "</td><td>";
echo $results['two-digit-sic'];
echo "</td><td>";
echo $results['description-2'];
echo "</td><td>";
echo $results['four-digit-sic'];
echo "</td><td>";
echo $results['description-3'];
echo "</td><td>";
echo $results['six-digit-sic'];
echo "</tr>";
// posts results gotten from database(title and text) you can also show id ($results['description'])
}
echo "</table>";
}
else{ // if there is no matching rows do following
echo "<p style='text-align:center;color:red;'>No results for '$query' | Please try again";
}
}
else{ // if query length is less than minimum
echo "<p style='color:orange; text-align:center; '>Minimum length is ".$min_length."</p>";
}
?>
<!-- creates a form at the bottom of the list if there are more than 100 records -->
<?php if(mysql_num_rows($raw_results) > 100) : ?>
<form action="" method="GET" class = 'sic-search'>
<input class = 'sic-search-text' type="text" name="sic" placeholder="Search for an industry, eg 'Agriculture'"/>
<input type="submit" value="Search" class = 'sic-search-button'/>
</form>
<?php endif; ?>
</div> <!-- end of wrapper -->
</script>
</div> <!-- #main-content -->
<?php get_footer(); ?>

I Want to show the alert of fetched row from database table on body load event

I want to compare current system date with my database table date as shown. If both matched then corresponding member Name will be shown in alert on body load.
My Code
<?php
$now=date("Y/m/d");
$sql = "select MemName from payment where $now ='$RemindDate' ";
$result = mysql_query($sql) or die(mysql_error());
while($rowval2 = mysql_fetch_array($result)) {
$MemName=$rowval2['MemName']);
}
alert("$rowval2['MemName']");
?>
First fetch the date from database table then compare with your system date using date()Ref.1 Ref.2
if the date match then use member id to fetch member name and display it using alert().
<?php
$now=date("Y/m/d");
$sql = "select MemName from payment where date_column_name = '".$now."' ";
$result = mysql_query($sql) or die(mysql_error());
while($rowval2 = mysql_fetch_array($result))
{
$MemName=$rowval2['MemName'];
}
echo '<script language="javascript">';
echo 'alert ('<?php echo $MemName; ?>')';
echo '</script>';
?>
Now the other option is, When you use JavaScript tag in echo function, It can be misleading because of "quotation" signs in php echo function. I suggest you to close tag if you are beginner. Also try to echo each query and its result so you will get idea that it actually working or not.
<?php
$now=date("Y/m/d");
$sql = "select MemName from payment where date_column_name = '".$now."' ";
$result = mysql_query($sql) or die(mysql_error());
while($rowval2 = mysql_fetch_array($result))
{
$MemName=$rowval2['MemName'];
}
?>
<script type="text/javascript">
alert("<?php echo $MemName; ?>");
</script>
<?php
?>

My fetch appears not to get any results

I want to use this code to fetch content out of the database, database connection is made with global $connect and is working correctly. But I do not understand why there is no content available on the web page. If someone can help me with this, that would be very appreciated.
<div class="content">
<article class="fullcontent">
<header>
<h2>Advertenties</h2>
</header>
<content>
<?php
global $connect;
$sql2 = "SELECT * FROM advertentie";
$result2 = $connect->query($sql);
if ($result->num_rows > 0){
while ($row = $result -> fetch_assoc()){
echo "<div class='fixed-groups'>";
echo "<a class='fixed-group-item' href='website'>";
echo "<div class='image'>";
echo "<img src='cms/pages/afbeeldingen/".$row['filename']."' width='173px' height='138px' />";
echo "</div></a>";
echo "<strong>".$row['advertentie_titel']."</strong>";
echo "<span>".$row['verkoopprijs']."</span>";
echo "</div>";
}
}
?>
</content>
If someone got some tips for me on how to improve this, many thanks! I don't know how to assign a href to a inserted form, so when I insert a form that form also has to get his own web page. how can I manage that?
You assigned the sql query to $sql2 and ran $sql in $connect->query($sql)
Correct would be:
$sql2 = "SELECT * FROM advertentie";
$result2 = $connect->query($sql2);

Create a News Feed that displays posts from people they are following

I have a code that should check the Database following to find all the people that the current logged in user has followed then it will search the second database posts to find any posts by the people they are following but it currently only shows one post. I know its not very efficient compared to JOIN but I don't fully understand how that works.
Here is a Screenshot of the Database Following it has the user who sent the following request and the user they are following what is it should do is use the session id for the logged in user and find all matching results for the user in the following column and it should take all the results from the column following and search the database post in the column code and echo all the results as the newsfeed.
<?php
$con=mysqli_connect("HOST","USERNAME","PASSWORD","DB");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$myid=$_SESSION['user']['id'];
$result = mysqli_query($con,"SELECT * FROM following WHERE follower='$myid'");
while($row = mysqli_fetch_array($result)) {
$following=$row['following'];
?>
<?php
$con=mysqli_connect("HOST","USERNAME","PASSWORD","DB");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM posts");
while($row = mysqli_fetch_array($result)) {
if ($following==$row['code']){
?>
<div class="content">
<section class="intro">
<h2><?php echo $row['name']; ?> <?php echo $row['lastname']; ?></h2>
<div class="text">
<?php echo $row['post']; ?>
</div>
<?php
}
else{
}
}
mysqli_close($con);
?>
<?php
}
mysqli_close($con);
?>
Because my previouse answer didnt help What do you want to output??
If your database has 3 followers does it have to output this in html?
But this Code will Output All users in your following list Like this..
$result = mysqli_query($con,"SELECT * FROM posts");
echo "<div class='content'> <section class='intro'>";
echo "<h2><a href='/profile/?id=".$row['code']."'>";
while($row = mysqli_fetch_array($result)) {
if ($following==$row['code']){
echo $row['name']." ".$row['lastname']."</a></h2>";
echo $row['post'];
}
echo "<div class='text'>";
echo "</div>";
}
This Code is Outputting this but the Bold text in this example is a link that goes to
/profile/?id=CODEHERE
Steve Jobs <-- is a link (goes to /profile/?id=CODEHERE
The Post text
Johny John
The Post text
Miracle Tru
The Post text

Categories