PHP PDO output in angular - php

I am trying to get output in Angular via PHP PDO but unable to understand the error.
When I write the same code using PHP procedural way (mysqli) then I can easily fetch the output. But I am struggling in fetching data with PHP PDO.
Here is my code:
Index.php:
<div ng-app="myapp" ng-controller="fieldcontroller" ng-init="displayData()">
<tr ng-repeat="field in fields">
<td>{{ field.fieldlabel }}</td>
</tr>
My JS File:
var app = angular.module("myapp",[]);
app.controller("fieldcontroller", function($scope, $http){
//Display Function
$scope.displayData = function(){
$http.get("model/select.php?type=getForm")
.then(function(response) {
$scope.fields = response.data;
console.log($scope.fields);
});
}
});
PHP File:
?php
//include('Database.php');
//select.php
//$connect = mysqli_connect("localhost", "kcmsuser", "KC+wSH&X#z9P", "kcms");
$server = 'localhost';
$user = 'kcmsuser';
$pass = 'KC+wSH&X#z9P';
$dbname = 'kcms';
$conn = new mysqli($server, $user, $pass, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo " Connected successfully ";
}
$sql = "SELECT * FROM form";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$output = array();
while($row = $result->fetch_array()) {
$output[] = $row;
}
} else {
echo "0 Results ";
}
echo json_encode($output);
Console.log shows:
[{"0":"110","id":"110","1":"First Name","fieldlabel":"First Name","2":"firstname","fieldname":"firstname","3":"text","fieldtype":"text","4":"","isprimary":"","5":"yes","required":"yes","6":"1","position":"1"},{"0":"23","id":"23","1":"Carrier","fieldlabel":"Carrier","2":"carrier","fieldname":"carrier","3":"text","fieldtype":"text","4":"no","isprimary":"no","5":"yes","required":"yes","6":"5","position":"5"},{"0":"26","id":"26","1":"Email","fieldlabel":"Email","2":"email","fieldname":"email","3":"email","fieldtype":"email","4":"","isprimary":"","5":"yes","required":"yes","6":"9","position":"9"},{"0":"27","id":"27","1":"Password","fieldlabel":"Password","2":"password","fieldname":"password","3":"password","fieldtype":"password","4":"","isprimary":"","5":"no","required":"no","6":"4","position":"4"},{"0":"102","id":"102","1":"Date of Birth","fieldlabel":"Date of Birth","2":"dob","fieldname":"dob","3":"date","fieldtype":"date","4":"","isprimary":"","5":"no","required":"no","6":"6","position":"6"},{"0":"101","id":"101","1":"Last Name","fieldlabel":"Last Name","2":"lastname","fieldname":"lastname","3":"text","fieldtype":"text","4":"","isprimary":"","5":"yes","required":"yes","6":"2","position":"2"}]
And [ngRepeat:dupes] warning.
I tried track by $index with ng-repeat but nothing happens.
I am new to Angular.

you can use pdo code for get data from mysql
<?php
$servername="localhost";
$username="";
$password="";
$dbname="";
$dsn="mysql:host=$servername;dbname=$dbname";
try{
$connect=new PDO ($dsn,$username,$password);
$connect->exec("SET NAMES 'utf8';");
}catch(PDOException $error){
echo "Error in connect".$error->getMessage();
exit();
}
$sql = "SELECT * from `table`";
$result = $connect->query($sql);
$num_row=$connect->query("SELECT count(id) from `table`")->fetchColumn();
if ($num_row > 0) {
$output = array();
while($row=$result->fetch(PDO::FETCH_ASSOC)) {
$output[] = $row;
}
} else {
echo "0 Results ";
}
?>

After spending few hours and hit and try message, I was able to sole the problem.
Problem was in the below PHP code:
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo " Connected successfully ";
}
Thanks....

sorry i was busy, i write code for you very fast if you have any proplem you can say me
<?PHP
if(#$_REQUEST['delete']){
/**************delete**************/
$delete=$_REQUEST['delete'];
if($connect->query("delete from table where id='$delete'")){
echo 'delete successful';
}}else if(#$_REQUEST['insert']=='record'){
/**************insert**************/
if(isset($_POST['submit'])){
$title=$_POST['title'];
if($connect->query("INSERT INTO `table`(`title`) VALUES ('$title')")){
echo 'success'; }else{ echo 'error'; }
}}else if(#$_REQUEST['edit']=='record'){
/**************insert**************/
if(isset($_POST['submit'])){
$title=$_POST['title'];
$mid=$_POST['mid'];
if($connect->query("UPDATE `table` SET `title`='$title'")){
echo 'success'; }else{ echo 'error'; }
}} ?>
<h1><?PHP if(#$_REQUEST['edit']=='new'){?>insert<?PHP }else if(#$_REQUEST['edit']=='new'){?>edit<?PHP }else{?>manage<?PHP }?><h1>
<br>insert
<?PHP
if(#$_REQUEST['edit']=='new'){
$id=$_REQUEST['id'];
$checks=$connect->query("select * from table where id='$id' order by id desc");
$check=$checks->fetch(PDO::FETCH_ASSOC);
?>
<form>
<input type="text" name="title" value="<?=$check['title']?>">
<input type="hidden" name="edit" value="record">
<input type="hidden" name="mid" value="<?=$check['id']?>">
<input type="submit" value="edit" name="submit"/>
</form>
<?PHP }else if(#$_REQUEST['insert']=='new'){?>
<form>
<input type="text" name="title">
<input type="hidden" name="insert" value="record">
<input type="submit" value="create" name="submit"/>
</form>
<?PHP
}else{
$radif=0;
$qacs=$connect->query("select * from table order by id desc");
while ($qc=$qacs->fetch(PDO::FETCH_ASSOC)){$radif++;
?>
<tr>
<td scope="row"><?=$radif?></td>
<td><?=$qc['title']?></td>
<td>delete</td>
<td>edit</td>
<?PHP }}?>

Related

SQL query using string concatenation

I am new to php and I have a table "abc" where I have columns: id, name and age.
I want to do the following:
Only if a name is entered in the input field, corresponding data (id and age) should be shown using string concatenation to build SQL query.
This is for search functionality
What should be the SQL query for this question?
// Create connection
$conn = new mysqli($SERVER_NAME, $USER_NAME, $PASSWORD, $DATABASE_NAME);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = " SELECT name, CONCAT(id,'.',age) FROM personene WHERE name = 'VALUE_FROM INPUT_NAME'";
$result = $conn->query($sql);
$error = mysqli_error($conn);
// Store results
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php if(!empty($error))
echo "<p style='color:red'>$error</p>";
?>
<p>Please enter the name:</p>
<form action="<?=$_SERVER['PHP_SELF']?>" method="GET">
<input type="input" name="name" value="" />
<br/>
<input type="submit" name="sendbtn" value="Send" />
</form>
<?php
if(!empty($data)) {
echo "<h1>Persons:</h1><table border='1'><tr><th>Id</th><th>Firstname</th><th>Age</th></tr>";
foreach($data as $row) {
echo "<tr><td>".$row["id"]."</td>";
echo "<td>".$row["name"]."</td>";
echo "<td>".$row["age"]."</td></tr>";
}
echo "</table>";
}
else
echo "No data available";
echo '(Query: '.$sql.')';
?>
</body>
</html>
It is not clear why you want to concatenate fields in the SQL query when clearly in the html these fields are displayed in their own columns. The code you have is wide open to SQL Injection so you need to consider using a prepared statement to handle the user supplied input safely.
<?php
$data=[];
error_reporting( E_ALL );
if( $_SERVER['REQUEST_METHOD']=='GET' && !empty( $_GET['name'] ) ){
$SERVER_NAME='';
$USER_NAME='';
$PASSWORD='';
$DATABASE_NAME='';
mysqli_report( MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
$conn=new mysqli( $SERVER_NAME, $USER_NAME, $PASSWORD, $DATABASE_NAME );
try{
$sql = 'select `name`, `id`, `age` from `personene` where `name` = ?';
$stmt=$conn->prepare( $sql );
$stmt->bind_param('s', $_GET['name'] );
$stmt->execute();
$stmt->bind_result( $name, $id, $age);
while( $stmt->fetch() )$data[]=[
'name' => $name,
'id' => $id,
'age' => $age
];
$stmt->free_result();
$stmt->close();
$conn->close();
}catch( mysqli_sql_exception $e ){
exit( $e->getMessage() );
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Fetch user details</title>
</head>
<body>
<p>Please enter the name:</p>
<form method='GET'>
<input type='input' name='name' />
<br/>
<input type='submit' name='sendbtn' value='Send' />
</form>
<?php
if( !empty( $data ) ) {
echo "
<h1>Persons:</h1>
<table border='1'>
<tr>
<th>Id</th>
<th>Firstname</th>
<th>Age</th>
</tr>";
foreach( $data as $row ) {
echo "
<tr>
<td>{$row["id"]}</td>
<td>{$row["name"]}</td>
<td>{$row["age"]}</td>
</tr>";
}
echo "</table>";
} else {
echo "No data available";
}
?>
</body>
</html>
For the SQL query to find records related with name, you have to use this query,
select concat('id', '-', 'age') as user_data from abc where name = $REQUEST['search_name'];
OR you can use LIKE(check here) condition to get the records from the table.
select concat('id', '-', 'age') as user_data from abc where name like
$REQUEST['search_name'];
Here is an update for your code,
<?php
// Create connection
$conn = new mysqli($SERVER_NAME, $USER_NAME, $PASSWORD, $DATABASE_NAME);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// echo "Connected successfully";
$data = [];
$sql = "Please submit the form.";
if(isset($_GET['sendbtn']) ) {
$sql = " SELECT id, name, age FROM personene WHERE name = '". $_GET['name'] ."'";
$result = $conn->query($sql);
$error = mysqli_error($conn);
// Store results
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
}
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
if(!empty($error))
echo "<p style='color:red'>$error</p>";
?>
<p>Please enter the name:</p>
<form action="<?=$_SERVER['PHP_SELF']?>" method="GET">
<input type="input" name="name" value="" />
<br/>
<input type="submit" name="sendbtn" value="Send" />
</form>
<?php
if(isset($data) && !empty($data)) {
echo "<h1>Persons:</h1><table border='1'><tr><th>Id</th><th>Firstname</th><th>Age</th></tr>";
foreach($data as $row) {
echo "<tr><td>".$row["id"]."</td>";
echo "<td>".$row["name"]."</td>";
echo "<td>".$row["age"]."</td></tr>";
}
echo "</table>";
} else {
echo "No data available";
}
echo '(Query: '.$sql.')';
?>
</body>
</html>

How to pass posted variables to an Anchor ID tag in PHP

I have been trying to insert some data into a table in DB, but the error came at where one of my fields is GET variable from the anchor tag.
To get a clear understanding please take a look at my code:
index page: with a post value of id = 1
<form name="user_form" method="post" action="input.php">
<?php
$sql2 = "SELECT * FROM singleq ";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()) {
$id = $row2['id'];
$cat = $row2['cat'];
?>
<tr >
<input type="hidden" name="q_id" id="q_id" value="<?php echo ($row2["id"]); ?>" />
<input type="hidden" name="tablum" id="tablum" value="singleq" />
<input type="hidden" name="test_id" id="test_id" value="<?php echo $_GET['id'] ?>" />
<td><a href='singleqbanksub.php?id=<?php echo $row2['id'];?>'><?php echo substr(($row2["question"]),0,100) ?>...</a></td>
<td><button class="button button1" type="submit" name="insert-data" id="insert-data" style="width:180px;">Add Question</button></td>
</tr>
<?php }
} else {
echo "";
}
?>
</form>
input.php :
<?php include('db.php'); ?>
<?php
$q_id = $_POST['q_id'];
$test_id = $_POST['test_id'];
$tablum = $_POST['tablum'];
$date = date_default_timezone_set('America/New_York');
$date = date('M-d,Y H:i:s');
$date2 = date('M-d,Y');
$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO english (cat, test_id, q_id, tablum)
VALUES ('$cat', '$test_id', '$q_id', '$tablum')";
if ($conn->query($sql) === TRUE) {
header('Location: index.php?id='echo "$test_id";'');
}
else {
echo "ERROR" . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Here as I tried to pass the id through the anchor tag redirection to the same page to loop the process, it shows an error about an unidentified echo.
You are using echo inside header function which is wrong , use like this:
header('Location: index.php?id='.$test_id);

Issue with webform used for adding and deleting customer names

I'm trying to create a web form which lists all of the customers and then gives you a text field with a button next to it where you can add customers. Then it should show the list of customers with delete buttons next to them where you can click to delete the customer from the database.
I'm having getting this to work. For starters it's echoing the contents of one of the PHP script. I'm not sure what I need to do.
Here's my index.php file:
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "manager";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT url from customers";
$result = $conn->query($sql);
$tempArray = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$tempArray[] = $row["url"];
}
} else {
echo "0 results";
}
$conn->close();
?>
<table>
<tr>
<td><u>URL</u></td>
<td><u>Action</u></td>
</tr>
<?php foreach ($tempArray as $row) : ?>
<tr>
<td><?php echo $row; ?></td>
<td><form action="disable_customer.php" method="get"><input type="submit" name="url" value="Disable Customer2"/></form></td>
</tr>
<?php endforeach; ?>
</table>
<form action="add_customer.php" method="get">
<input type="text" name="url"> <input type="submit" name="add" value="Add Customer"/>
</form>
</body>
</html>
Here's my add_customers.php file:
<html> <body>
Added <?php echo $_GET['url']; ?><br>
<?php
$servername = "localhost"; $username = "root"; $password = "test123"; $dbname = "manager";
// Create connection $conn = new mysqli($servername,$username,$password,$dbname); // Check connection if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); }
$sql = "INSERT INTO customers (url) VALUES ('$url')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully"; } else {
echo "Error: " . $sql . "<br>" . $conn->error; }
$conn->close(); ?>
</body> </html>
Here's my disable_customer.php file:
<html>
<body>
<$php
session_start();
$SESSION['username']="Test";
$SESSION['authuser']=1;
$url = $_GET['url'];
echo "<br>" . $url . "<br>";
$servername = "localhost";
$username = "root";
$password = "test123";
$dbname = "manager";
// Create connection
$conn = new mysqli($servername,$username,$password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_REQUEST["btn_submit"])) {
echo "yyyyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaayyyyyyyyyy";
}
$sql = "DELETE FROM customers WHERE customers.url = " . "'$url'";
echo "---------------------\n";
echo $sql . "\n";
echo "---------------------\n";
if ($conn->query($sql) === TRUE) {
echo "Record successfully deleted.";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
</body>
</html>
try changing index.php from this:
<form action="disable_customer.php" method="get"><input type="submit" name="url" value="Disable Customer2"/></form>
to this:
<form action="disable_customer.php" method="get">
put the url in here: <input type="text" name="url"/>
<input type="submit" value="submit"/>
</form>
If that works - but you don't want the user to be entering their own urls - then you need to read those urls out of the database first:
Going back to your original code in index.php, change the foreach to output the value of the url into 'value' attribute of the button:
<?php foreach ($tempArray as $row) : ?>
<tr>
<td><?php echo $row; ?></td>
<td><form action="disable_customer.php" method="get"><input type="submit" name="url" value="<? echo $row['url'] ?>"/></form></td>
</tr>
<?php endforeach; ?>

PHP web form, used to add/remove customer names, not working correctly [duplicate]

I'm trying to create a web form which lists all of the customers and then gives you a text field with a button next to it where you can add customers. Then it should show the list of customers with delete buttons next to them where you can click to delete the customer from the database.
I'm having getting this to work. For starters it's echoing the contents of one of the PHP script. I'm not sure what I need to do.
Here's my index.php file:
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "manager";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT url from customers";
$result = $conn->query($sql);
$tempArray = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$tempArray[] = $row["url"];
}
} else {
echo "0 results";
}
$conn->close();
?>
<table>
<tr>
<td><u>URL</u></td>
<td><u>Action</u></td>
</tr>
<?php foreach ($tempArray as $row) : ?>
<tr>
<td><?php echo $row; ?></td>
<td><form action="disable_customer.php" method="get"><input type="submit" name="url" value="Disable Customer2"/></form></td>
</tr>
<?php endforeach; ?>
</table>
<form action="add_customer.php" method="get">
<input type="text" name="url"> <input type="submit" name="add" value="Add Customer"/>
</form>
</body>
</html>
Here's my add_customers.php file:
<html> <body>
Added <?php echo $_GET['url']; ?><br>
<?php
$servername = "localhost"; $username = "root"; $password = "test123"; $dbname = "manager";
// Create connection $conn = new mysqli($servername,$username,$password,$dbname); // Check connection if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); }
$sql = "INSERT INTO customers (url) VALUES ('$url')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully"; } else {
echo "Error: " . $sql . "<br>" . $conn->error; }
$conn->close(); ?>
</body> </html>
Here's my disable_customer.php file:
<html>
<body>
<$php
session_start();
$SESSION['username']="Test";
$SESSION['authuser']=1;
$url = $_GET['url'];
echo "<br>" . $url . "<br>";
$servername = "localhost";
$username = "root";
$password = "test123";
$dbname = "manager";
// Create connection
$conn = new mysqli($servername,$username,$password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_REQUEST["btn_submit"])) {
echo "yyyyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaayyyyyyyyyy";
}
$sql = "DELETE FROM customers WHERE customers.url = " . "'$url'";
echo "---------------------\n";
echo $sql . "\n";
echo "---------------------\n";
if ($conn->query($sql) === TRUE) {
echo "Record successfully deleted.";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
</body>
</html>
try changing index.php from this:
<form action="disable_customer.php" method="get"><input type="submit" name="url" value="Disable Customer2"/></form>
to this:
<form action="disable_customer.php" method="get">
put the url in here: <input type="text" name="url"/>
<input type="submit" value="submit"/>
</form>
If that works - but you don't want the user to be entering their own urls - then you need to read those urls out of the database first:
Going back to your original code in index.php, change the foreach to output the value of the url into 'value' attribute of the button:
<?php foreach ($tempArray as $row) : ?>
<tr>
<td><?php echo $row; ?></td>
<td><form action="disable_customer.php" method="get"><input type="submit" name="url" value="<? echo $row['url'] ?>"/></form></td>
</tr>
<?php endforeach; ?>

Database search returns inconsistent results

I'm running a simple database search against one table in my database. The results are then displayed in a table. If no results are found, the search shows a message that says "0 results", but sometimes it will echo the table headings without displaying any results. This search is part of a class project and isn't going to be an active database, so I haven't included any protection for SQL injection. Any help would be greatly appreciated.
<h2>Customer Search</h2>
<br>
<p class="first">Search the Customer Database</p>
<form action="searchcustomers.php" method="post">
<input type="text" name="search" placeholder="Search...." />
<input type="submit" value=">>" />
</form>
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "oldga740_SeniorProject";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// If there is a search variable try to search database
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$sql = "SELECT * FROM Customers WHERE Client LIKE '%$searchq%'";
if ($result = mysqli_query($conn, $sql)) {
if (mysqli_num_rows($result) > 0) {
echo '<table class="hoverTable"><tr><th>Client</th><th>Address</th><th>City</th><th>State</th><th>Zip Code<br></th><th>Phone</th></tr>';
// We have results! Go fetch rows!
while ($row = mysqli_fetch_row($result)) {
// This loop runs until there are no more results left to echo
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["Client"]. "</td><td>" . $row["Address"]. "</td><td> " . $row["City"]. "</td><td> " . $row["State"]. "</td><td> " . $row["ZipCode"]. "</td><td> " . $row["Phone"]. "</td></tr>";
}
echo "</table>";
}
} else {
// No results from query
$message = "0 results";
}
/* free result set */
mysqli_free_result($result);
}
}
?>
</div>
</div>
<div class="center">
<?php
if(isset($message)){ echo $message; }
?>
</div>
</body>
</html>
I find, and you may disagree, but properly indenting your code as you go and opening and closing tags before filling in content help reduce the number of problems. Having said that now it'll probably not work!
<html>
<head>
<title>db search</title>
</head>
<body>
<div>
<div>
<h2>Customer Search</h2>
<p class="first">Search the Customer Database</p>
<form action="searchcustomers.php" method="post">
<input type="text" name="search" placeholder="Search...." />
<input type="submit" value=">>" />
</form>
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "oldga740_SeniorProject";
$conn = new mysqli( $servername, $username, $password, $dbname );
if ( $conn->connect_error ) die("Connection failed: " . $conn->connect_error );
if( isset( $_POST['search'] ) ) {
$searchq = $_POST['search'];
$searchq = preg_replace( "#[^0-9a-z]#i", "", $searchq );
$sql = "SELECT * FROM `Customers` WHERE `Client` LIKE '%$searchq%';";
if ( $result = mysqli_query( $conn, $sql ) ) {
if ( mysqli_num_rows( $result ) > 0 ) {
echo '
<table class="hoverTable">
<tr>
<th>Client</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Phone</th>
</tr>';
while( $row = $result->fetch_assoc() ) {
echo "
<tr>
<td>".$row["Client"]."</td>
<td>".$row["Address"]."</td>
<td>".$row["City"]."</td>
<td>".$row["State"]."</td>
<td>".$row["ZipCode"]."</td>
<td>".$row["Phone"]."</td>
</tr>";
}
echo '
</table>';
} else {
$message = "0 results";
}
}
mysqli_free_result( $result );
}
?>
</div>
</div>
<div class="center">
<?php if( isset( $message ) ){ echo $message; } ?>
</div>
</body>
</html>
Delete the redundant first while loop while ($row = mysqli_fetch_row($result)) {, if the query had just 1 result the first loop would have fetched it only while the 2nd while loop would have fetched nothing resulting in just a header and no body of the table.
<h2>Customer Search</h2>
<br>
<p class="first">Search the Customer Database</p>
<form action="searchcustomers.php" method="post">
<input type="text" name="search" placeholder="Search...." />
<input type="submit" value=">>" />
</form>
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "oldga740_SeniorProject";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// If there is a search variable try to search database
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$sql = "SELECT * FROM Customers WHERE Client LIKE '%$searchq%'";
if ($result = mysqli_query($conn, $sql)) {
if (mysqli_num_rows($result) > 0) {
echo '<table class="hoverTable"><tr><th>Client</th><th>Address</th><th>City</th><th>State</th><th>Zip Code<br></th><th>Phone</th></tr>';
// We have results! Go fetch rows!
// This loop runs until there are no more results left to echo
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["Client"]. "</td><td>" . $row["Address"]. "</td><td> " . $row["City"]. "</td><td> " . $row["State"]. "</td><td> " . $row["ZipCode"]. "</td><td> " . $row["Phone"]. "</td></tr>";
}
echo "</table>";
} else {
// No results from query
$message = "0 results";
}
/* free result set */
mysqli_free_result($result);
}
}
?>
</div>
</div>
<div class="center">
<?php
if(isset($message)){ echo $message; }
?>
</div>
</body>
</html>

Categories