PHP SQL search not returning any results - php

I'm trying to make a search bar to search my db but it always returns 0 results,
Here is the Connect and search code,
<?php
$serverName = "server";
$connectionInfo = array( "Database"=>"server");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$output ='';
if(isset($_POST['PartSelect'])){
$searchq = $_POST['PartSelect'];
$query = sqlsrv_query($conn, "SELECT * FROM Parts WHERE PartNumber LIKE '%$searchq%'") or die ("Could not search");
$count = sqlsrv_num_rows($query);
if($count == 0){
$output = 'There are no results';
----- This Section is new error Code -----
$rows = sqlsrv_has_rows( $query );
if ($rows === true)
echo "\nthere are rows\n";
else
echo "\nno rows\n";
---- End Section ------
This prints saying it has rows so it it picking up the data,
}
else {
while ($row = sqlsrv_fetch_array($query)){
$Partno = $row['PartNumber'];
$output .= '<div>'.$PartNo. '</div>';
}
}
?>
Here is the search bar and submit button (I am using materialize as a framework), and the output at the end.
<form action="Tests.php" method="post">
<div class="form-group">
<br/><input name="PartSelect" type="text" class="form-control" id="PartSelect" aria-describedby="nameHelp" placeholder="Enter Part">
<br/><button name="SelectPartbtn" id="Submitbtn" type="Submit" class="waves-effect waves-light btn">Find</button>
</div>
<?php print ("$output"); ?>
</form>

try with this code check connection is made or not
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = sqlsrv_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . sqlsrv_errors());
}

Related

Odd paragraphs added to html after PHP POST

When i click the submit button for my search from, for each result there is a paragraph with a ">" which is put at the top of the page and I have no idea where it comes from. Here's my code (using wordpress)
[insert_php]
$db_host = "localhost";
$username = "root";
$password = "";
$dbname = "Tickets";
// Create connection
$conn = new mysqli($db_host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if((isset($_POST['Search'])) && ($_POST['Search'] != '')){
$search = $_POST['Search'];
$sql = "SELECT * FROM `Problem_Solutions` WHERE (`Problem_Type` LIKE '%$search%') OR (`Solution_Title` LIKE '%$search%')";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
if ($count > 0){
echo "<table><th>Title</th><th>Solution</th><th>Count</th>";
while ($row = mysqli_fetch_array($query)){
echo "<tr><td>".$row['Solution_Title']."</td>";
echo "<td>".$row['Solution']."</td>";
echo "<td>".$row['Count']."</td>";
echo "<<td><form name='form' method='POST'><input style='background:#6B8E23;' type='submit' name='".$row['Solution_ID']. "' value='This Solution Solved My Problem' /></form></td></tr>";
} echo "</table>";
}else{
echo " 0 results found";
}
}
$conn->close();
[/insert_php]
Your problem is in this line:
echo "<<td><form name='f
Delete the first <

Dynamically generate buttons with loop php

What I wan't to do is create buttons that are automatically generated from the database. So when I add a new record in the database the button is created Is this possible with a loop? So yes how do I create the button.
This is what I have so far:
<?php
$servername = "localhost";
$username = "root";
$password = "Iamthebest1009";
$dbname = "dktp";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM theme";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "". $row["theme_name"]. "<br>";
}
} else {
echo "no results";
}
$conn->close();
?>
Yes it is possible. you need to echo html
<?php
$servername = "localhost";
$username = "root";
$password = "Iamthebest1009";
$dbname = "dktp";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM theme";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$your_url ="https://www.google.com";
echo "". $row["theme_name"]. "<br>";
echo '<input type="button" name="' . $row["theme_name"]. '" value="'. $row["theme_name"].'">';
}
} else {
echo "no results";
}
$conn->close();
?>

How to return the results of my query in rows

This is my query, and it is working fine, but how do I have the results returned in an HTML TABLE. for example I want the first 5 results on row 1 the next 5 on row 2 and so on. Any help would be great. Thank you!
<?php
$servername = "XXX";
$username = "XXX";
$password = "XXX";
$dbname = "XXX";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM manufacturer LIMIT 10 OFFSET 0";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<td><a href="index.php?route=product/manufacturer/info&
manufacturer_id='. $row["manufacturer_id"] .'"><img src="image/' .
$row["image"] .'"></a></td>';
}
} else {
echo "0 results";
}
$conn->close();
<?php
$servername = "XXX";
$username = "XXX";
$password = "XXX";
$dbname = "XXX";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn- >connect_error);
}
$sql = "SELECT * FROM manufacturer LIMIT 10 OFFSET 0";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$i=0;
while($row = $result->fetch_assoc()) {
If($i==5){
echo "</tr>";
echo "<tr>";
$i=0;
}
echo '<td><a href="index.php? route=product/manufacturer/info&
manufacturer_id='. $row["manufacturer_id"] .'">. <img src="image/' .
$row["image"] .'"></a></td>';
$i++;
}
} else {
echo "0 results";
}
$conn->close();

Wrong response ajax

I try to fill text boxes from my database. I am using a onChange event. I can see that the code is running but I get a wrong response.
I am using the following code:
index.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM cus";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select class='form-control select2' name='cus_id' onChange='getCus(this.value)' style='width: 100%;'>";
echo "<option selected disabled hidden value=''></option>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row["cus_id"]. "'>" . $row["cus_id"]. " | " . $row["cus_name"]. "</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$conn->close();
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function getCus() {
// getting the selected id in combo
var selectedItem = jQuery('#cus_id option:selected').val();
// Do an Ajax request to retrieve the product price
jQuery.ajax({
url: 'get5.php',
method: 'POST',
data: {'cus_id': selectedItem},
success: function(response){
// and put the price in text field
jQuery('#cus_id').val(response);
},
error: function (request, status, error) {
alert(request.responseText);
},
});
}
</script>
get5.php
<?php
// Turn off all error reporting
error_reporting(0);
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname) ;
// Check connection
if ($conn->connect_error)
{
die('Connection failed: ' . $conn->connect_error) ;
}
else
{
$cus_name = isset($_POST['cus_name'])?$_POST['cus_name']:'';
$cus_ad = isset($_POST['cus_ad'])?$_POST['cus_ad']:'';
$cus_pos = isset($_POST['cus_pos'])?$_POST['cus_pos']:'';
$cus_pla = isset($_POST['cus_pla'])?$_POST['cus_pla']:'';
$cus_cou = isset($_POST['cus_cou'])?$_POST['cus_cou']:'';
$query = 'SELECT * FROM cus WHERE cus_id=' . $cus_id . ' ';
$res = mysqli_query($conn, $query) ;
if (mysqli_num_rows($res) > 0)
{
$result = mysqli_fetch_assoc($res) ;
echo $result['cus_name'];
echo $result['cus_ad'];
echo $result['cus_pos'];
echo $result['cus_pla'];
echo $result['cus_cou'];
}else{
$result = mysqli_fetch_assoc($res) ;
echo "0 res";
}
}
?>
I cant see whats wrong with my code. When I run this I get the response "0 res". Can anyone see what is wrong?
You have to this line of code
<?php
// Turn off all error reporting
error_reporting(0);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname) ;
// Check connection
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error) ;
}else {
$cus_id = isset($_POST['cus_id'])?$_POST['cus_id']:'';
$query = 'SELECT * FROM cus WHERE cus_id="' . mysqli_real_escape_string($conn, $cus_id) . '"';
$res = mysqli_query($conn, $query) ;
if (mysqli_num_rows($res) > 0) {
$result = mysqli_fetch_assoc($res) ;
echo $result['cus_name'];
echo $result['cus_ad'];
echo $result['cus_pos'];
echo $result['cus_pla'];
echo $result['cus_cou'];
}else{
$result = mysqli_fetch_assoc($res) ;
echo "0 res";
}
} ?>
You should always escapes special characters in a string for use in an SQL statement.
Please check your cus_id data type, i guess you didn't set it in integer or another numeric type.
If i am wrong, you should check your row in database, it must be empty in that cus_id.

SQL query into option form

I have a PHP code to get info from my Microsoft SQL server 2014, but it isnt working, the page it self works fine since it pops up as it should when i comment out the PHP code, but as soon as the PHP code isnt commented out, its just all white, so im assuming problem with the PHP code. I have to get the results from the query out into a drop down menu.
i use this code:
$servername = "VCCSQL03";
$username = "forecast";
$password = "Telefon2";
$dbname = "Forecast";
$connectionInfo = array("Database"=>$dbname, "UID"=>$username, "PWD"=>$password);
$conn = sqlsrv_connect($serverName, $connectionInfo);
if(!$conn) {
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
// Check connection
$result = sqlsrv_query($conn,"SELECT * FROM dbo.vw_BrandProduct");
if ($result->num_rows > 0) {
// output data of each row
while($row = sqlsrv_fetch_array($result)) {
echo "<option value='".$row['Brand_ProductID']."' name='".$row['Brand_ProductName']."'</option>";
}
} else {
echo "";
}
sqlsrv_close();
First and foremost, you do not have an open and closed select tag, and your option tags was missing a > to close it properly. Try the below revision, assuming connection is established on the page properly then this should work.
$connectionInfo = array( "Database"=>$dbname, "UID"=>$username, "PWD"=>$password);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if(!$conn) {
//// Check connection
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$result = sqlsrv_query($conn,"SELECT * FROM dbo.vw_BrandProduct");
if ($result->num_rows > 0) {
// output data of each row
echo "<select name='products'>";
while($row = sqlsrv_fetch_array($result)) {
echo "<option value='".$row['Brand_ProductID']."'>$row['Brand_ProductName']</option>";
}
echo "</select>";
} else {
echo ""; } sqlsrv_close(); ?>

Categories