EDIT
I have a mysql table with fields as follows:
Products - serial, name, description, price, picture.
the viewproducts.php page is as follows:
<?php
$result = mysql_query("SELECT * FROM products ")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Products';
} else {
echo "<table border='0'><table border='1' width=100%><tr><th>Product Name</th><th>Description</th><th>Price</th><th>Image</th><th>Edit</th><th>Delete</th>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['name']. "</td>";
echo "<td>" . $info['description']. "</td>";
echo "<td>£" . $info['price']." </td>";
echo "<td>" . "<img src='../getImage.php?id=" . $info['serial'] ."'/></td>";
echo '<td> Edit</td>';
}
}
echo "</tr>";
echo "</table>";
?>
my edit.php page looks like this:
<?php
$product_id = $_GET['serial'];
$result = mysql_query("SELECT * FROM products WHERE serial = '$product_id'")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Products';
} else {
echo "<table border='0'><table border='1' width=100%><tr><th>Product Name</th><th>Description</th><th>Price</th><th>Image</th><th>Edit</th><th>Delete</th>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['name']. "</td>";
echo "<td>" . $info['description']. "</td>";
echo "<td>£" . $info['price']." </td>";
echo "<td>" . "<img src='../getImage.php?id=" . $info['serial'] ."'/></td>";
}
}
echo "</tr>";
echo "</table>";
?>
when i click on edit from thr viewproducts.php page, it goes to edit.php page where nothing is showing up. the serial id on the address bar is coming up as follows:
http://www.********.com/****/admin/edit.php?product_id=
I want to be able to edit any product clicked on from the viewproduct.php page and transfered to edit.php page. I dont think my edit.php page is set up corretly.
Please help,
Thanks
You can pass via $_GET the id of the product and then, in the edit/delete page, retrieve that parameter. Obviously you have to sanitize the input properly before using it. For example, the link of the each product should look like this:
echo '<td>Edit</td>';
In the edit page you should have something like:
$id = $_GET['id'];
// For example, if the product id is an integer
// you can sanitize it doing this
$id = (int) $id
You could pass it as an argument to your php file in wich you want to edit/delete the product:
Edit Product
Then in your edit.php you will pick up the id of the product and load it's data from the database.
[edit.php]
$product_id = isset($_GET['product_id']) ? intval($_GET['product_id']) : null;
if(!$product_id) {
exit();
}
// query your database for the product
$row = mysqli_query("SELECT * FROM <table> WHERE product_id = $product_id");
// then you output your html with fields populated from the result from the database
Related
First of all I am kind of new to PHP.
I've been asked to change up a bit of code and add the option to see the status of a field and mark the rows which are completed. So I've added a status field to my MySQL database which has a standard value of 0. Then on button click I'd like this to change to the value 1. Based on this I can either project a cross glyph icon with a button to update this, or project a checkmark so that the status is "done".
Now I'm having issues with the update part. I just cannot seem to get it to work with clicking the button and updating only that row. Field "prdh_num" is my unique value if that is usefull.
Here's the code I work with:
function uren_error(){
global $mysql;
$sql = "
SELECT *
FROM uren_error, medw
WHERE uren_error.uren_datum between adddate(now(),-14) and now() AND medw.medw_num = uren_error.medw_num
ORDER BY uren_error.prdh_num";
$query = $mysql->query($sql);
echo "<h1>Niet in MKG verwerkte uren van de afgelopen 14 dagen</h1>";
echo "<div class='row' id='urenTabel'>";
echo "<div class='col-xs-0 col-md-0'></div>";
echo "<div class='col-xs-12 col-md-12'>";
echo "<table class='table'>";
echo "<thead>";
echo "<th>Verwerkingsdatum</th>";
echo "<th>Medewerker</th>";
echo "<th>Productieorder</th>";
echo "<th>Productieorder regel</th>";
echo "<th>Bewerking</th>";
echo "<th>Duur</th>";
echo "<th>Status</th>";
echo "<th>Actie</th>";
echo "</thead>";
while($row = mysqli_fetch_array($query)){
$hours = floor($row['uren_tijd_man_std'] / 3600);
$mins = floor($row['uren_tijd_man_std'] / 60 % 60);
echo "<tr>";
echo "<td>" .$row['uren_datum']. "</td>";
echo "<td>" .$row['medw_roepnaam'] . " " . $row['medw_tussenvoegsel'] . " " . $row['medw_naam'] . "</td>";
echo "<td>" .$row['prdh_num']. "</td>";
echo "<td>" .$row['prdr_num']. "</td>";
echo "<td>" .$row['bewerk_num']. "</td>";
echo "<td>" .$hours.":".$mins. "</td>";
if($row['uren_status'] == "0"){
echo "<td><div class='glyphicon glyphicon-remove' style='color:red'></div></td>";
echo "<td><form action'' method='POST'><input type='submit' name=".$row['prdh_num']."></input></form></td>";
} else if($row['uren_status'] == "1"){
echo "<td><div class='glyphicon glyphicon-ok' style='color:green'></div></td>";
}
echo "</tr>";
}
echo "</table>";
}
my update code so far:
$productieorder = $row['prdh_num'];
$updateStatus = "UPDATE uren_error SET uren_status = 1 WHERE prdh_num=".$productieorder."";
$query2 = $mysql->query($updateStatus);
In more simple way you can use <a> tag to update your record ,instead of <form> , just attached your parameter which you need to passed . i.e :
echo "<td><a href='currentphppagename.php?prdh_num=".$row['prdh_num']."'></a></td>";
And to get prdh_num use $_GET on same page i.e :
//check if have some value or not
if (isset($_GET['prdh_num'])) {
//getting value passed in url
$productieorder = $_GET['prdh_num'];
$updateStatus = "UPDATE uren_error SET uren_status = 1 WHERE prdh_num=".$productieorder."";
$query2 = $mysql->query($updateStatus);
}
And other way using form you can have one hidden field and assign row value to that :
echo "<td><form action='' method='POST'><input type='hidden' value=".$row['prdh_num']." name='prdh_num'/><input type='submit' name='submit'></input></form></td>";
Now on click of submit check below on same page :
//check if have some value or not
if (isset($_POST['submit'])) {
//getting value passed in url
$productieorder = $_POST['prdh_num'];
$updateStatus = "UPDATE uren_error SET uren_status = 1 WHERE prdh_num=".$productieorder."";
$query2 = $mysql->query($updateStatus);
}
You can do this with javascript. You should put "ondblclick" event on tr tag.
<tr ondblclick="Update(rowno)" />
And in javascript tag, there must be a function like
<script type="text/javascript">
function Uptdate(row) {
window.location.href ="update.php?row="+row;
}
</script>
I have created a table in PHPMyAdmin, and I have connected to it via a localhost. My PHP code displays the data in a table.
I want to be able to delete a certain row, so I created a html/php link to delete the row within a table.
My problem is that whenever I press delete, the page just refreshes without an error but the record is still there.
Is there something missing within my code?
<?php
// Connect to the database
$username="root";$password="test";$database="products";
// Connect to the MySQL server and select the required database
$connection = new mysqli("localhost",$username,$password, $database);
$sql = "";
$sql = "SELECT * FROM products";
$ID = isset($row['ID']) ? $row['ID'] : '';{
$query = mysqli_query($connection, "DELETE FROM products WHERE ID=$ID");
}
$result = $connection->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<tr>\n";
echo "<td>" . $row['Name'] . "</td>\n";
echo "<td>" . $row['Description'] . "</td>\n";
echo "<td>" . $row['Price'] . "</td>\n";
echo "<td>" . $row['Cost_Price'] . "</td>\n";
echo "<td>" . $row['Stock'] . "</td>\n";
echo "<td>" . $row['EAN'] . "</td>\n";
?>
<td><a href="?mode=delete&ID=<?php echo $row["ID"]; ?>"
title="Delete <?php echo $row["ID"]; ?>">Delete</a></td>
<?php
echo "</tr>\n";
}
}
$stmt = $connection->prepare('SELECT * FROM products WHERE name = ?');
$stmt->bind_param('s', $name);
$stmt->execute();
$result = $stmt->get_result();
$connection->close();
?>
Change
$ID = isset($row['ID']) ? $row['ID'] : '';{
$query = mysqli_query($connection, "DELETE FROM products WHERE ID=$ID");
}
To
if(isset($_GET['ID']) && ($_GET['mode'] == 'delete')) {
$ID = $_GET['ID'];
$query = mysqli_query($connection, "DELETE FROM products WHERE ID=$ID");
}
Explanations
When after clicking delete link, it comes to this line
$ID = isset($row['ID']) ? $row['ID'] : '';{.
Here, $row['ID'] is not set. So, $ID set to "" as you declared in your code. So, DELETE statement is not able to find that product which you wanted to delete.
Actually, in delete link. You are passing 2 variables. One is mode and other is ID of product. Catch those 2 variables through $_GET as I mentioned in my answer.
Quick Links
How to set $_GET variable
$_GET : PHP Manual
<?php
$con = new mysqli('localhost', 'root' ,'', 'world');
$query = 'SELECT * FROM city ORDER BY Name';
if ($result = mysqli_query($con, $query)) {
echo "<table>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>"
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['CountryCode'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
}
mysqli_close($con);
?>
This simple code will display every entries in this database. Now I would like to add a selective display option by choosing the CountryCode.
$query2 = 'SELECT DISTINCT CountryCode FROM city ORDER BY CountryCode';
How do I use the result I got from the query above and make it become radio buttons to choose what to display?
Similar to what you are doing already: Something like
while ($row = mysqli_fetch_assoc($result)) {
echo "<input type='radio' name='whatever' value='".$row['Name']."'>". $row['Name'];
}
Ofcourse the field names can be whatever you like them to be, as long as you select them in the query
I have selected a random table from a list/array and I want to be able to call this random table with the mysqli_query function i.e. select a table dynamically
Here is my code:
<?php
$mysqli=mysqli_connect(HOST,USERNAME,PASSWORD,DATABASE);
// Check connection
if (mysqli_connect_errno($mysqli))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Looks at all prodcuts and selects those that have special offers enabled
// Selects a random table from the array
$tables_special = ["ajbs_products_console" ,"ajbs_products_console_games", "ajbs_products_pc", "ajbs_products_pc_games", "ajbs_products_pc_parts"];
$rtable = $tables_special[floor (rand(0,count($tables_special)-1))];
echo "<p>".$rtable." was selected</p>";
$products = mysqli_query("SELECT * FROM '".$rtable."'");
echo "<p>".$products."</p>";
while($productRow = mysqli_fetch_array($products))
{
if ($productRow['product_specials'] == 1)
{
echo "<table>";
echo "<tr>";
echo "<td rowspan='2'><img src=" . $productRow['product_image'] . "/></td>";
echo "</tr>";
echo "<table border='1'>";
echo "<tr>";
echo "<td>Product Name</td>";
echo "<td>" . $productRow['product_name'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Description</td>";
echo "<td>" . $productRow['product_description'] . "</td>";
echo "</tr>";
echo "</table>";
}
}
mysqli_close($mysqli);
?>
Im at a loss of why this is not working
Thanks,
Bull
You should not quote table names with single quotes but with backticks, single quotes makes it a string instead of a table name;
Also, the function mysqli_query takes an extra parameter, the connection.
$products = mysqli_query("SELECT * FROM '".$rtable."'");
should be;
$products = mysqli_query($mysqli, "SELECT * FROM `".$rtable."`");
Try
$products = mysqli_query($mysqli, "SELECT * FROM ".$rtable);
Can someone please help me on the following code? Simply put, I'm trying to get two separate SQL tables' data, one on the horizontal side (brands) and the other (distributors) on the vertical side of a dynamically populated table.
my issue is, if you go through the code I cant get the text boxes populated under each respective brand name I get to display from the database. The text boxes are appearing only for the first brand name column.
My second issue if how do I assign a unique ID or a name to a dynamically populated text box here?
<?php
$q=$_GET["q"];
include ("../connection/index.php");
$sql="SELECT * FROM distributors WHERE rsm='".$q."'";
$sqlq="SELECT * FROM brands";
$result = mysqli_query($db,$sql) or die ("SQL Error_er1");
$resultq = mysqli_query($db,$sqlq) or die ("SQL Error_er2");
echo "<table border='1'>
<tr>
<th>Distributor</th>";
"<tr>";
while($rowq = mysqli_fetch_array($resultq))
{
echo "<td>" . $rowq['bname'] . "</td>";
}
"</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['dname'] . "</td>";
echo "<td><input type='text' name='txt1'></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($db);
?>
You're creating loop without relating to eachother, and the same goes for the execution of the querys...
If you want to solve it you will have to nestle the loops together, something like:
<?php
$q=$_GET["q"];
include ("../connection/index.php");
$sql="SELECT * FROM distributors WHERE rsm='".$q."'";
$result = mysqli_query($db,$sql) or die ("SQL Error_er1");
echo "<table border='1'>
<tr>
<th>Distributor</th>";
"<tr>";
//Go through all distributors
while($rowq = mysqli_fetch_array($result)) {
echo "<td>" . $rowq['bname'] . "</td>";
//Show all brands for current distributor
$sqlq="SELECT * FROM brands where distributor_id = " . $rowq['rsm'];
$resultBrands = mysqli_query($db,$sql) or die ("SQL Error Brands");
while($row = mysqli_fetch_array($resultBrands))
{
$id = $row['rsm'];
echo "<tr>";
echo "<td>" . $row['dname'] . "</td>";
echo "<td><input type='text' name='textBox[]'></td>";
echo "</tr>";
}
//End show all brands for current distributor
}
//End Go through all distributors
A better solution though, would be something like (of course $q has to validated before input inside of query and also binded with bind_param()).
<?php
$q=$_GET["q"];
include ("../connection/index.php");
$sql = " SELECT * FROM distributors d";
$sql .=" LEFT JOIN brands b ON (d.brand_id = b.brand_id)";
$sql .=" WHERE d.rsm=$q";
$result = mysqli_query($db,$sql) or die ("SQL Error");
echo "<table border='1'>";
while($rowq = mysqli_fetch_array($result))
{
$id = rowq['rsm'];
echo "<tr>";
echo "<td>" . $rowq['dname'] . "</td>";
echo "<td>" . $rowq['bname'] . "</td>";
echo "<td><input type='text' name='textBox[]'></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($db);
?>
Take notice of the name='textBox[]'. From PHP you can access the variable with $_POST['textBox'] (or $_GET['textBox'] and PHP will return an array).