How to represent a link dynamically in php? - php

<?php
$stmt = $con->prepare("SELECT * FROM user_tbl WHERE name = ?");
$stmt->bind_param('s', $_REQUEST['name']);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo '<br /><br />Name: ' .$row['name'];
echo '<br /><br />Contact Number: ' .$row['cont'];
echo '<br /><br />Email ID: ' .$row['email'];
echo "<br /><br /><a href='viewdtl.php' target='_blank'>View details</a>";
echo "<br /><hr />";
}
}
else
{
echo "0 records found";
}
$stmt->close();
?>
This code runs just fine but with two problems.. The else part 0 records found always shows which i want to print after the search get placed and the link .. i can't click on it.. how can i print the link here ?

Related

how to perform search in php?

I want to perform a search from the database using php in which when i enter name arun it should search for name arun and display the result with name arun and when i enter name and email it should search for name and email and should display single result since the email is unique field i have written the following code but it is not working can someone help me?
Please add a comment if any one gives Downvote and Why?
<?php
if (isset($_SESSION['message11'])) {
echo $_SESSION['message11'];
unset($_SESSION['message11']);
}
?>
<?php
include('connection.php');
$name = $_POST['name'];
$email = $_POST['email'];
$qualification = $_POST['qualify'];
$sql = "SELECT * FROM form WHERE Name ='$name' OR EmailAddress = '$email' OR Qualification = '$qualification' ";
$result = $conn->query($sql);
if (!empty($_POST)) {
if ($result->num_rows === 0) {
echo '<p style="margin-left:340px">no records</p>';
}
}
while ($row = $result->fetch_assoc()) {
$_SESSION["snum"] = $row['sno'];
$_SESSION["nam"] = $row['Name'];
$_SESSION["quali"] = $row['Qualification'];
$_SESSION["emai"] = $row['EmailAddress'];
echo '<br>';
echo '<form name="friend" action="received.php" method="post">';
echo '<input style="margin-left:340px;padding-bottom:10px" type="checkbox" value="' . $row['sno'] . '" name="friend[]"> user Details</input>';
echo '<br>';
echo '<br>';
echo '<div class="container" style="border-style:solid; border-width:medium;width: 550px;">';
echo '<br>';
echo 'Name: ' . $row['Name'];
echo '<br /> EmailAddress: ' . $row['EmailAddress'];
echo '<br /> Qualification: ' . $row['Qualification'];
echo '<br /> DOB: ' . $row['DOB'];
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '</div>';
echo '<br/>';
}
if (!empty($_POST)) {
echo '<button style="margin-left:340px" type=""submit">invite</button>';
}
echo '</form>';
$conn->close();
?>
You are adding OR condition to three fields even if they are not entered by the user.
Use conditions so that sql should search only entered fields.
Use array()
$sql = "SELECT * FROM form WHERE ";
$conditions = array();
if (! empty($name)) {
$conditions[] = "Name ='$name'";
}
if (! empty($email)) {
$conditions[] = "EmailAddress ='$email'";
}
if (! empty($qualification)) {
$conditions[] = "Qualification ='$qualification'";
}
$sql .= ! empty($conditions) ? implode(' OR ', $conditions) : '1';
$result=$conn->query($sql);
Try this LIKE will be effective for searching stuffs.
Simple and best practice.
SELECT * FROM form WHERE Name LIKE '%".$name."%' OR EmailAddress LIKE '%".$email."%' OR Qualification LIKE '%".$qualification."%'

Display data from database depending on the how many I have entered

In my database and more specific in the table named portfolio a user with a speficic username can store more than one data. The columns of the database are id,username,portfolio and portfolio_description
and I want to echo in my web page all the data for this username for example username=nbourlai. Until now I can echo only the first row for each username what is the loop that i can use in order to echo as many time is the id number.
Below is the current result and i want to create Portfolio and Portfolio description to be displayed with the appropriate values as many time as the highest id number is for the specific user.
Here is my code...
$portfolio = queryMysql("SELECT portfolio,portfolio_description FROM portfolio WHERE username='nbourlai'");
$row = mysql_fetch_row($portfolio);
echo "<h2>Portfolio</h2>";
echo "Portfolio: ";
echo stripslashes($row[0]) . "<br/>Portfolio Description: ";
echo stripslashes($row[1]) . "<br clear=left /><br />";
Can you help me please?
Are you looking for while loop like this?
$portfolio = queryMysql("SELECT portfolio,portfolio_description FROM portfolio WHERE username='nbourlai'");
echo "<h2>Portfolio</h2>";
while($row = mysql_fetch_row($portfolio)){
echo "Portfolio: ";
echo stripslashes($row[0]) . "<br/>Portfolio Description: ";
echo stripslashes($row[1]) . "<br clear=left /><br />";
}
or, like this?
$portfolio = queryMysql("SELECT username,portfolio,portfolio_description FROM portfolio ORDER BY username");
$username = '';
while($row = mysql_fetch_row($portfolio)){
if($username != $row[0]){
echo "<h1>".$row[0]."</h1>";
echo "<h2>Portfolio</h2>";
echo "Portfolio: ";
echo stripslashes($row[1]) . "<br/>Portfolio Description: ";
echo stripslashes($row[2]) . "<br clear=left /><br />";
}else{
echo "Portfolio: ";
echo stripslashes($row[1]) . "<br/>Portfolio Description: ";
echo stripslashes($row[2]) . "<br clear=left /><br />";
}
$username = $row[0];
}

how to delete rows from mysql database not found in xml file

I have an xml file and now it's been inserted to MySQL database, but there was a problem in the xml file which meant some data may be deleted from the site, so I updated the xml file and I wrote code for deleting data from database table which deleted from xml but the records are still in the database.
from another way I want to compare between xml file and database and delete data not found.
<?php
ini_set('display_errors','On');
//connecting to database.
echo "connected to DB<br /><br />";
$url = "test.xml";
$xmlfgc = file_get_contents($url);
$xmlitem = new SimpleXMLElement($xmlfgc);
echo "xml loaded<br /><br />";
foreach ($xmlitem->property as $xml) {
//$id = mysql_real_escape_string($xml->id);
$mls_id = mysql_real_escape_string($xml->ref);
//echo "$mls_id";
echo "xml parsed<br /><br />";
$result = mysql_query("SELECT mls_id FROM ezrealty");
//$select = "SELECT mls_id FROM ezrealty";
//$storeArray = Array();
//while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// $storeArray[] = $row['mls_id'];
//}
if ($result != $mls_id){
$query = "DELETE FROM ezrealty WHERE mls_id = '$mls_id'";
mysql_query($query) or die(mysql_error());
echo "DataBase deleted<br /><br />";
}else{
echo "There is no deleted properties from xml file.";
}
}
//show updated records
echo "<br /><br />";
printf ("Records updated: %d\n", mysql_affected_rows());
//close connection
mysql_close($con2);
?>
thanks for you, now i have the solution for my problem.
<?php
ini_set('display_errors','On');
//connecting to database.
echo "connected to DB<br /><br />";
$url = "test.xml";
$xmlfgc = file_get_contents($url);
$xmlitem = new SimpleXMLElement($xmlfgc);
echo "xml loaded<br /><br />";
$xmldata = array();
foreach ($xmlitem->property as $xml) {
$mls_id = mysql_real_escape_string($xml->ref);
// $xmldata[] = "$mls_id";
$xmldata[] = '("' . $mls_id. '")';
}
//Get all reference numbers from database in array
$result = mysql_query("SELECT mls_id FROM gitfd_ezrealty");
$storeArray = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$storeArray[] = $row['mls_id'];
}
//get difference between two arrays.
$result1 = array_diff($xmldata, $storeArray);
foreach ($result1 as $value) {
//echo "<strong>$value</strong> <br />";
//$value1 = '("' . $value. '")';
//$query = 'INSERT INTO gitfd_ezrealty (mls_id)
//VALUES' . implode(',', $value1);
//mysql_query($query) or die(mysql_error());
//echo "inserted into mysql<br /><br />";
}
echo "These properties are founded in target site and Not founded in our site.<br />";
echo "________________________________________________________________"."<br />";
print_r($result1);
echo "<br />"."_____________________________________________________________________"."<br />";
$result2 = array_diff($storeArray, $xmldata);
foreach ($result2 as $value) {
//echo "<strong>$value</strong> <br />";
$query = "DELETE FROM ezrealty WHERE mls_id = '$value'";
mysql_query($query) or die(mysql_error());
echo "DataBase deleted<br /><br />";
}
echo "_________________________________________________________________"."<br />";
print_r($result2);
echo "<br />"."______________________________________________________________________"."<br />";
echo "These properties were deleted .<br />";
//show updated records
echo "<br /><br />";
printf ("Records updated: %d\n", mysql_affected_rows());
//close connection
mysql_close($con2);
?>

mysqli not displaying image from database

Im changing everything to mysqli and because im just learning its pretty hard. What am i doing wrong? my images appear broken and prints the file name, I know this is a simple question but ive tried searching the internet with no result. sorry im learning
<?php
include_once("db_conex.php");
$query = "SELECT * FROM employees ORDER BY price ASC";
$query = mysqli_real_escape_string($db,$query);
if($result = mysqli_query($db,$query)){
while($row = mysqli_fetch_object($result)){
echo "<br /><br />";
echo '<img src="/upload/ " border=0>', $row->photo;
echo "<br /><br />";
echo '<b> City: </b>', $row->city;
echo '<b> Price: </b>', $row->price;
echo '<b> Bath: </b>', $row->bath;
echo '<b> Bath: </b>', $row->bed;
echo '<b> Description: </b>', $row->description;
echo '<b> Link: </b>', $row->link;
}
mysqli_free_result($result);
}
//Close Connection
mysqli_close($db);
?>
Your problem is here:
echo '<img src="/upload/ " border=0>', $row->photo;
You are literally just printing an empty (probably non-existant) image, followed by the filename. You probably meant:
echo '<img src="/upload/' . $row->photo . '" border=0>';

MySQL return result if 0 rows returned

I have this PHP code:
$query = "SELECT name, COUNT(message) FROM guestbook_message WHERE name='".$req_user_info['username']."' GROUP BY name";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo "Messages posted: ". $row['COUNT(message)'] ."";
echo "<br />";
}
Which will show the amount of comments a user has posted.
How do I make it return a value if there is no messages posted from that user? Currently is displays nothing at all. But I want it to show "Messages posted: 0"
Any ideas?
Check the number of results returned in result using mysql_num_rows.
$query = "SELECT name, COUNT(message) FROM guestbook_message WHERE name='".$req_user_info['username']."' GROUP BY name";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result) > 0)
while($row = mysql_fetch_array($result))
{
echo "Messages posted: ". $row['COUNT(message)'] ."";
echo "<br />";
}
else
echo "NO Messages posted. <br />";
if ($row = mysql_fetch_array($result)) {
echo "Messages posted: ". $row['COUNT(message)'] . "";
echo "<br />";
}
else {
echo "Messages posted: 0";
echo "<br />";
}
Replace $row['COUNT(message)'] with the tertiary operator:
( $row['COUNT(message)'] > 0 ? $row['COUNT(message)'] : 0 )
This is basically a compacted if... else... statement.

Categories