I want to search with PHP in mysql Database, and search results to edit or delete, but until now i can do search and everything works or Edit/Delete and search dosen't work anymore..
I want to combine both in same php file, but dosen't work. Know someone how to combine ?
How can i add both in same php.. ?
Search PHP
<?php
include_once("db_W3.php");
$result = mysqli_query($mysqli, "SELECT * FROM centralhub_lagerbestand");
//load database connection
include_once("db_W3.php");
$host = "$servername";
$user = "$username";
$password = "$password";
$database_name = "$db";
$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 centralhub_lagerbestand where KundenNr LIKE '%$search%' LIMIT 0 , 50");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
// Display search result
if (!$query->rowCount() == 0) {
echo "<table style=\"font-family:arial;color:#333333;\">";
echo "<tr>
<td align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;background:#FFFFFF;\">Kunden Nr.:</td>
<td align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;background:#FFFFFF;\">Regal:</td>
<td align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;background:#FFFFFF;\">Regal Ebene:</td>
<td align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;background:#FFFFFF;\"> Edit </td>
</tr>";
while ($results = $query->fetch()) {
echo "<tr> <td width='500px' align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;\">";
echo $results['KundenNr'];
echo "</td><td width='230px' align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;\">";
echo $results['Regal'];
echo "</td><td width='230px' align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;\">";
echo "".$results['RegalEbene'];
echo "</td> </tr>";
}
echo "</table>";
} else {
echo 'Für die aktuellen Filter wurden keine Ergebnisse gefunden';
}
?>
Edit/Delete PHP
<?php
//including the database connection file
include_once("db_W3.php");
//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated
$result = mysqli_query($mysqli, "SELECT * FROM centralhub_lagerbestand ORDER BY id DESC"); // using mysqli_query instead
?>
<html>
<head>
<title>Homepage</title>
</head>
<body>
<table width='100%' border=0>
<tr bgcolor='#CCCCCC'>
<td>KundenNr</td>
<td>Regal</td>
<td>RegalEbene</td>
<td>Update</td>
</tr>
<?php
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['KundenNr']."</td>";
echo "<td>".$res['Regal']."</td>";
echo "<td>".$res['RegalEbene']."</td>";
}
?>
</table>
</body>
</html>
Related
I have this code snippet:
try
{
$conn = new PDO("mysql:host=$host;dbname=judges", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
and
$query = "SELECT * FROM feedback";
echo '<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td> <font face="Arial">Judge Name</font> </td>
<td> <font face="Arial">PC</font> </td>
<td> <font face="Arial">TD</font> </td>
<td> <font face="Arial">EX</font> </td>
<td> <font face="Arial">ID</font> </td>
<td> <font face="Arial">comment</font> </td>
</tr>';
if ($result = $conn->query($query)) {
echo "got result";
echo $result["PC"];
//while ($row = $result->fetch_assoc()) {
while ($row = $result) {
$field1name = $row["judgeName"];
$field2name = $row["PC"];
$field3name = $row["TD"];
$field4name = $row["EX"];
$field5name = $row["ID"];
$field6name = $row["comment"];
echo '<tr>
<td>'.$field1name.'</td>
<td>'.$field2name.'</td>
<td>'.$field3name.'</td>
<td>'.$field4name.'</td>
<td>'.$field5name.'</td>
<td>'.$field6name.'</td>
</tr>';
}
}
both inside submit.php, however, while the feedback table inside judges database is not empty, I don't see any of the rows shown in the HTML page only the column name shows up in the Response tab of Network tab in Firefox Inspect tool. What do you suggest to populate the existing database into the html page even before I enter the information for the new user?
Here's my feedback table from the judges database:
Trying the following from the comments also didn't work and nothing got printed:
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
There's no point in the if statement. You have PDO::ERRMODE_EXCEPTION enabled, so if there's an error in the query it will throw an exception, not return false.
The syntax to fetch a row in PDO is $result->fetch(PDO::FETCH_ASSOC).
$result = $conn->query($query);
echo "<td><tr>got result</tr></td>";
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$field1name = $row["judgeName"];
$field2name = $row["PC"];
$field3name = $row["TD"];
$field4name = $row["EX"];
$field5name = $row["ID"];
$field6name = $row["comment"];
echo '<tr>
<td>'.$field1name.'</td>
<td>'.$field2name.'</td>
<td>'.$field3name.'</td>
<td>'.$field4name.'</td>
<td>'.$field5name.'</td>
<td>'.$field6name.'</td>
</tr>';
}
You were mixing PDO and MYSQLI_
Also you had some debug code that would have damaged your table HTML.
Also there is no real point in moving data from a perfectly good array into scalar variables just to include those values in some HTML.
if ($result = $conn->query($query)) {
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>
<td>$row[judgeName]</td>
<td>$row[PC]</td>
<td>$row[TD]</td>
<td>$row[EX]</td>
<td>$row[ID]</td>
<td>$row[comment]</td>
</tr>";
}
}
I want to display data after in static table just like an "Add to Cart".
<table>
<th>Item</th> <th>Description</th>
<tr>
<td>Monitor<td>
<td>Monitor Blue<td>
</tr>
<tr>
<td>Mouse<td>
<td>Mouse Wireless<td>
</tr>
<tr>
<td>Keyboard<td>
<td>Keyboard<td>
</tr>
My search form : This is no problem is searching item. Only in the add button every row. So when you search for item you have choice if you want to add in static table.
Search:
Mysql when search
<?
$sql = "select * from item_tb where name='".$search."'";
$result=$conn->query($sql);
echo "
<table cellspacing='20' border='0' ><tr> <th>Item</th><th>Description</th>
</tr>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "
<tr> <td>
".$row["name"]."</td> <td>
".$row["description"]."</td>";
echo" <form name='add' method='POST'>";
echo" <td><a href='item.php?id=".$row['Iid']."'><input type='submit' value='Add'></td></a>";
echo" </form>";
}
}
?>
Here is my main problem. i don't how to make table where i'll get the data i have search and will display as many as item i add. I'm really down in this.
<?
if($id != "")
{
$sql = "SELECT * FROM item_tb WHERE Iid='".$id."'";
$result=$conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo" ".$row["name"]." ";
}
}
}
?>
<?php
if($id != "") {
$sql = "SELECT * FROM item_tb WHERE Iid='" . $id . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?= $row['name'] ?></td>
</tr>
<?php
}
}
}
?>
I have an issue with the below code, it is showing the html part but not the data from my database. I am just trying to have the data from the database in the drop down list and when I click find to display it on the table below. Can you please help?
<?php
include 'config.php';
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<select name="manufacturer">
<option value="">----ALL----</option>
<?php
$sql = "SELECT id, manufacturer FROM coop";
$result = $conn->query($sql);
while ($r = mysqli_fetch_assoc($result))
{
echo "<option value='$r[id]'> $r[manufacturer]</option>";
}
?>
</select>
<input type="submit" name="find" value="find"/>
<br><br>
<table border="1">
<tr align="center">
<th>ID</th> <th>Manufacturer</th>
</tr>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$des=$_POST["manufacturer"];
if($des=="")
{
$result= mysqli_query("SELECT id, manufacturer FROM coop");
}
else
{
$result= mysqli_query($sql);
}
echo "<tr><td colspan='5'></td></tr>";
while($r= mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td align='center'>$r[0]</td>";
echo "<td width='200'>$r[1]" . " $r[2]</td>";
echo "<td alig='center' width='40'> $r[3]</td>";
echo "<td align='center' width='200'>$r[4]</td>";
echo "<td width='100' align='center'>$r[5]</td>";
echo "</tr>";
}
}
?>
</table>
</body>
</html>
your queries are wrong here
<?php
$sql = "SELECT id, manufacturer FROM coop";
$result = $conn->query($sql);
while ($r = mysqli_fetch_assoc($sql))
{
echo "<option value='$r[0]'> $r[0]</option>";
}
?>
this while ($r = mysqli_fetch_assoc($sql)) should be while ($r = mysqli_fetch_assoc($result ))
you should fetch your assoc with $result not with$sql
still you have some error bellow part
I'm not really familiar with Ajax Response - I have edited the PHP Ajax Search code from W3schools.com as follows :
<?php
require_once('connect_db.php');
$query = "select item_no from items";
$result = mysql_query($query);
$a = array();
while ($row = mysql_fetch_assoc($result)){
$a[] = $row['item_no'];
}
//get the q parameter from URL
$q=$_GET["q"];
//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if ($hint=="")
{
$hint=$a[$i];
}
else
{
$hint=$hint." , ".$a[$i];
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
{
$response="No Suggestion";
}
else
{
$response=$hint;
}
//output the response
echo "<table border=1><tr><td>".$response."</td></tr></table>";
?>
The output of the above code works perfectly, but they are all listed like this (2L500BU , 2L500GO , 2L500NA , 2L500RD , 2L802CA , 2L802WH , 2L803GR , 2L804BE , 2L804BK , 2L804CO , 2L805BU , 2L806BE , 2L806GR ) - Those numbers are Item No in mysql table called items.
Now :
1) I want to output the response into table with <tr> for each like this
2l500BU
2L500GO
.
.
.
.
etc.
2) Do you think its possible to output all table records from Mysql based on the hint entered as follows :
$sql="SELECT * FROM items WHERE item_no = '".**$hint**."'";
$result = mysql_query($sql);
echo "<table align='center' cellpadding='3' cellspacing='3' width='800px' border='1' font style='font-family:arial;'>";
echo "
<tr align=center>
<th style=font-size:18px; bgcolor=#20c500>Item Number</th>
<th style=font-size:18px; bgcolor=#20c500>QTY</th>
<th style=font-size:18px; bgcolor=#20c500>Actual Price</th>
<th style=font-size:18px; bgcolor=#20c500>Selling Price</th>
<th style=font-size:18px; bgcolor=#20c500>Difference</th>
<th style=font-size:18px; bgcolor=#20c500>Date</th>
</tr>";
while($row = mysql_fetch_assoc($result)){
echo "<tr align=center bgcolor=#e3e3e3>";
echo "<td style='font-size:18px; font-weight:bold;'>" . strtoupper($row['item_no']) . "</td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['qty'] . "</td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['actual_price'] . " <font style=font-size:12px;>JD</font></td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['discount_price'] . " <font style=font-size:12px;>JD</font></td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['difference_price'] . " <font style=font-size:12px;>JD</font></td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . date("d-m-Y",strtotime($row['date'])) . "</td>";
echo "</tr>";
}
echo "<table>";
If you're wanting to fetch items from a database and display a row for each of them, this is how you'd do it with jQuery.
Your PHP script:
<?php
$mysqli = new mysqli('localhost', 'user', 'password', 'database');
$sql = "SELECT item_no FROM items";
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
$rows[] = $row['item_no'];
}
header('Content-Type: application/json');
echo json_encode($rows);
?>
And your HTML with the table:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table>
<thead>
<tr>
<th scope="col">Item No.</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
<script src="js/lib/jquery.js"></script>
<script>
$(document).ready(function() {
$.getJSON('yourscript.php', function(items) {
$.each(items, function(i, item) {
$('tbody').append('<tr><td>' + item + '</td></tr>);
});
});
});
</script>
I've also used MySQLi (MySQL improved) rather than the standard mysql_ functions, as the mysql_ library is deprecated and you should be using either MySQLi or PDO now.
I have a blog written in PHP which uses MySQL as its' back-end. There is a template for blog posts stored in the database and the script pulls this content when it is needed. How can I make it so I do not need to store the template in my database and in my PHP file instead?
My PHP code is:
<?php
$dbhost="localhost";
$dbname="database";
$dbuser="username";
$dbpass="password";
$mysql=#mysql_select_db($dbname, #mysql_connect($dbhost, $dbuser, $dbpass)) or die (mysql_error());
if (($_GET['act'] == "blog") && (is_numeric($_GET['id']))) {
$temp = mysql_fetch_row(mysql_query("SELECT template FROM templates WHERE name = 'blog'"));
$sql = mysql_query("SELECT * FROM posts WHERE id = '".$_GET['id']."'");
while($r = mysql_fetch_array($sql)) {
$pab[0] = "/{subject}/";
$pab[1] = "/{date}/";
$pab[2] = "/{blog}/";
$rab[0] = "<a href='blog-".$r[id].".html'>".stripslashes($r[subject])."</a>";
$rab[1] = $r[date];
$rab[2] = stripslashes($r[blog])."<br /><br />";
eval (" ?>" . preg_replace($pab, $rab, stripslashes($temp[0])) . " <?php ");
}
}
?>
Here is the code stored in the database:
<table cellpadding='0' cellspacing='0' border='0' align='center' width='394'>
<tr>
<td><font face='Tahoma' size='4' color='#3A3A3A'><b>{subject}</b></font></td>
</tr>
<tr>
<td>{blog}</td>
</tr>
</table>
My question is; How can I make it so I do not need to store the template in my database and in my PHP file instead?
You mean like this?
<?php
$dbhost="localhost";
$dbname="database";
$dbuser="username";
$dbpass="password";
$mysql=#mysql_select_db($dbname, #mysql_connect($dbhost, $dbuser, $dbpass)) or die (mysql_error());
if (($_GET['act'] == "blog") && (is_numeric($_GET['id']))) {
$temp = "<table cellpadding='0' cellspacing='0' border='0' align='center' width='394'>
<tr>
<td><font face='Tahoma' size='4' color='#3A3A3A'><b>{subject}</b></font></td>
</tr>
<tr>
<td>{blog}</td>
</tr>
</table>";
$sql = mysql_query("SELECT * FROM posts WHERE id = '".$_GET['id']."'");
while($r = mysql_fetch_array($sql)) {
$pab[0] = "/{subject}/";
$pab[1] = "/{date}/";
$pab[2] = "/{blog}/";
$rab[0] = "<a href='blog-".$r[id].".html'>".stripslashes($r[subject])."</a>";
$rab[1] = $r[date];
$rab[2] = stripslashes($r[blog])."<br /><br />";
eval (" ?>" . preg_replace($pab, $rab, stripslashes($temp)) . " <?php ");
}
}
?>