how to Edit a particular id in php - php

I have a page which is showing around 3000 entries. what i am trying to do is when i click on filter button the data of only that entry will change. but currently, when i click on filter button the data of all the entries are changing.
here is my code:-
<?php
include("config.php");
$sql="SELECT * FROM inventory_details where status ='0' limit 0,100";
$query=mysqli_query($conn, $sql);
while($row=mysqli_fetch_assoc($query)){
$id = $row['id'];
$inventorybackground = $row['inventorybackground'];
$inventorycolor = $row['inventorycolor'];
$firm_name = $row['firm_name'];
$position = $row['position'];
$city = $row['city'];
$catagory_name = $row['catagory_name'];
$supplier = $row['supplier'];
if (isset($_POST['update'])){
$position=$_POST['position'];
$update_query="UPDATE inventory_details SET position ='$position' WHERE id='$id'";
$run_update=mysqli_query($conn, $update_query);
}
?>
<div class="container">
<div class="row" style="background-color: <?php echo $inventorybackground; ?> !important; color: <?php echo $inventorycolor; ?>; padding-top: 10px;">
<form class="form-horizontal" method="post" enctype="multipart/form-data">
<div class="col-md-1 inventory-data">
<input type="text" class="form-control" id="id" name="id" value="<?php echo $id;?>" disabled>
</div>
<div class="col-md-3 inventory-data">
<input type="text" class="form-control" id="firm_name" name="firm_name" value="<?php echo $firm_name;?>" disabled>
</div>
<div class="col-md-1 inventory-data">
<input type="text" class="form-control" id="position" name="position" value="<?php echo $position;?>">
</div>
<div class="col-md-1 inventory-data">
<input type="text" class="form-control" id="city" name="city" value="<?php echo $city;?>" disabled>
</div>
<div class="col-md-2 inventory-data">
<input type="text" class="form-control" id="catagory_name" name="catagory_name" value="<?php echo $catagory_name;?>" disabled>
</div>
<div class="col-md-2 inventory-data">
<input type="text" class="form-control" id="supplier" name="supplier" value="<?php echo $supplier;?>" disabled>
</div>
<div class="col-md-2 inventory-data">
<button type="submit" name="update" value="update" class="btn btn-primary">Filter</button>
</div>
</form>
</div>
</div>
<?php } ?>
Currently, i am only trying to change the position and I think, this problem is because of loop.
Please help me out, Thanks in advance.

When you click the button, the entire list is updated on your loop to list the rows.
Add this code at your page to detect the form POST to do the update:
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// perform update here
$id = $_POST['id'];
$position = $_POST['position'];
$update_query="UPDATE inventory_details SET position ='$position' WHERE id='$id'";
$run_update=mysqli_query($conn, $update_query);
}
Edit:
Your input can't be disabled, try to use input hidden to pass id to POST:
<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
If you need to show the id on input text, change the name to different name istead of id, because it will be used from input hidden

while($row=mysqli_fetch_assoc($query)){
This has ending bracket on the end. So you are basically looping through all the data and updating it all. Do the update query outside of loop.
$update_query="UPDATE inventory_details SET position ='$position' WHERE id='$id'";
You have SQL injection vulnerability here, please use prepared statements.
https://www.w3schools.com/php/php_mysql_prepared_statements.asp
If id is integer, you dont have to put quotation marks around it

Related

Cannot display edit form when clicked on edit button and the form doesn't parse error and displays blank page

EDIT: My edit button refuse to work.
I'm trying to edit this page, but it seems to be breaking when I click the edit button. it is retrieving data from the database and displaying on faculty.php successfully while if I try to edit it shows a blank page. what is wrong with my code, please.
faculty.php: a separate page where i have my edit button
<td>
<form action="facultyedit.php" method="POST">
<input type="hidden" name="edit_id" value="<?php echo $row['id']; ?>" >
<button type="submit" name="edit_data_btn" class="btn btn-success">EDIT</button>
</form>
</td>
facultyedit page: where i receive the edit button
<?php
if (isset($_POST['edit_data_btn']))
{
$id = $_POST['edit_id'];
$query = " SELECT * FROM register WHERE id='$id' ";
$query_run = mysqli_query($connection, $query);
foreach ($query_run as $row ) {
?>
<form action="" method="POST" >
<input type="text" name="edit_id" value="<?php echo $row['id'] ?>" >
<div class="form-group">
<label>Name:</label>
<input type="text" name="edit_name" value="<?php echo $row['name']; ?>" class="form-control" >
</div>
<div class="form-group">
<label>Designation:</label>
<input type="text" name="edit_designation" value="<?php echo $row['design']; ?>" class="form-control" >
</div>
<div>
<label>Description:</label>
<input type="text" name="edit_description" value="<?php echo $row['descrip']; ?>" class="form-control" >
</div>
<div class="form-group">
<label>Upload Image:</label>
<input type="file" name="edit_faculty_image" id="faculty_image" value="<?php echo $row['image']; ?>" class="form-control" >
</div>
<div class="">
Cancel
<button type="update" name="update_aboutusbtn" class="btn btn-primary">Update</button>
</div>
</form>
<?php
}
}
?
I have fixed the issue:
The problem was that I was selecting a register table from the database instead of the faculty table.

PHP - Displaying Specific data on a text boxes

I need to display only the id row of the database on a textbox, my problem was the data is displaying all id's rows and columns.
I was fetching all data in the database, but what I need is only the id on the database.
Here is my code in forms, input text fields and submit.
<form action="" method="POST">
<div class="row col-md-4">
<label>Amount</label>
<input type="text" name="id" class="form-control validate">
<br>
<input type="submit" class="form-control btn-warning" name="search" value="Search Data"></input><br>
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection, 'qrproject');
if(isset($_POST['search']))
{
$id = $_POST['id'];
$query = "SELECT * FROM scratch_cards WHERE amount='$id' ";
$query_run = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($query_run))
{
?>
<form action="" method="POST">
<input type="text" name="code" value="<?php echo $row['code'] ?>" class="form-control validate" id="mapo">
<input type="text" name="pin" value="<?php echo $row['pin'] ?>" class="form-control validate" id="mact">
<input type="text" name="status" value="<?php echo $row['status'] ?>" class="validate form-control" id="soluong">
<input type="date" name="card_expiration" value="<?php echo $row['card_expiration'] ?>" class="validate form-control" id="cardex">
<input type="number" name="card_validity" value="<?php echo $row['card_validity'] ?>" class="validate form-control" id="cardval">
</form>
<?php
}
}
?>
</form>
From your comments, it seems what you want to do is output a single, random row which matches the $_POST['id'] value against the amount column in your table. You can do this by changing your query to this:
SELECT * FROM scratch_cards WHERE amount='$id' ORDER BY RAND() LIMIT 1
You can also change
while($row = mysqli_fetch_array($query_run))
to
if($row = mysqli_fetch_array($query_run))
although with the change to the query to limit the output to one row this is no longer absolutely necessary.
As has been pointed out, you are vulnerable to SQL injection, you should read this question to see how to resolve that.

PHP - Randomized data displayed in text box when Submit is clicked

Hello I have a database and its data is this please see this image
I need a solution to this problem, the solution is i need the data to be change and not repeating, so when i input a value that is written on the database the displayed value on the textbox will not REPEAT and Change everytime i input the same value.
<form action="" method="POST">
<div class="row col-md-4">
<label>Amount</label>
<input type="text" name="id" class="form-control validate">
<br>
<input type="submit" class="form-control btn-warning" name="search" value="Search Data"></input><br>
//HERE IS WHERE I SUBMIT THE DATA RIGHT NOW IT IS NOT RANDOMIZED WHEN I SUBMIT AGAIN THE SAME VALUE APPEARS
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection, 'qrproject');
if(isset($_POST['search'])){
$id = $_POST['id'];
$query = "SELECT * FROM scratch_cards WHERE amount='$id' ";
$query_run = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($query_run)) {
?>
<form action="" method="POST">
<input type="text" name="code" value="<?php echo $row['code'] ?>" class="form-control validate" id="mapo">
<input type="text" name="pin" value="<?php echo $row['pin'] ?>" class="form-control validate" id="mact">
<input type="text" name="status" value="<?php echo $row['status'] ?>" class="validate form-control" id="soluong">
<input type="date" name="card_expiration" value="<?php echo $row['card_expiration'] ?>" class="validate form-control" id="cardex">
<input type="number" name="card_validity" value="<?php echo $row['card_validity'] ?>" class="validate form-control" id="cardval">
</form>
<?php
}
}
?>
</form>
Maybe as per your comment, you need unique records, so you can use DISTINCT in your query so by giving DISTINCT to any column, you won't get repeated records.
Here in your case:
SELECT DISTINCT id, amount FROM scratch_cards WHERE amount='$id'
Maybe this can help you to get unique records

Error updating record: You have an error in your SQL syntax how can i solve it?

i'm new to php and mysql.
i try to update record in my database from a html form.
i have a search page that work and a edit page whit a $_GET in the link where is the id of the element i have to edit in the database.
all work the form show me the correct data, i can edit them in the form. but when i click the edit button i get this error msg:
Error updating record: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
my php code of the edit page is this:
<?php
require('includes\dbconn.php');
require('includes\dbfunction.php');
session_start();
if(isset($_SESSION["username"])){
include('includes\theme\head.php');
include('includes\theme\topbar.php');
include('includes\theme\sidebar.php');
?>
<!-- !PAGE CONTENT! -->
<div class="w3-main" style="margin-left:300px;margin-top:43px;">
<div class="w3-container">
<div class="w3-row">
<div class="w3-col s12 l3">
<br>
<?php
//RICEVI DATI ARTICOLO DAL DATABASE
if(isset($_GET['art_id'])){
$sql = "SELECT * FROM articoli WHERE id =". $_GET['art_id'];
$result = $conn->query($sql);
$row = $result->fetch_assoc();
}
//AGGIORNA DATI ARTICOLO NEL DATABASE
if(isset($_POST['btn-aggiorna'])){
$codiceArt = $_POST['codicearticolo'];
$descrizioneArt = $_POST['nomearticolo'];
$barcodeArt = $_POST['barcode'];
$prezzoAcquistoArt = $_POST['prezzoacquisto'];
$prezzoVenditaArt = $_POST['prezzovendita'];
$quantitaArt = $_POST['quantita'];
$scontoArt = $_POST['sconto'];
$update = "
UPDATE articoli
SET codiceArticolo='$codiceArt'
, nomeArticolo='$descrizioneArt'
, barcode='$barcodeArt'
, prezzoAcquisto='$prezzoAcquistoArt'
, prezzoVendita='$prezzoVenditaArt'
, quantita='$quantitaArt'
, scontoPercentuale='$scontoArt'
WHERE id =". $_GET['art_id'];
if ($conn->query($update) === TRUE) {
?>
<script type="text/javascript">
window.location.href = 'articolomodificato.php';
</script>
<?php
} else {
echo "Error updating record: " . $conn->error;
}
}
?>
</div>
<div class="w3-col s12 l5">
<form class="w3-container" action="modificaarticolo.php" method="post">
<label class="w3-text-blue"><b>Codice articolo:</b></label>
<input class="w3-input w3-border" name="codicearticolo" type="text" value="<?php echo $row['codiceArticolo'];?>"><br>
<label class="w3-text-blue"><b>Descrizione articolo:</b></label>
<input class="w3-input w3-border" name="nomearticolo" type="text" value="<?php echo $row['nomeArticolo']; ?>"><br>
<label class="w3-text-blue"><b>Barcode:</b></label>
<input class="w3-input w3-border" name="barcode" type="text" value="<?php echo $row['barcode']; ?>"><br>
<label class="w3-text-blue"><b>Prezzo Acquisto:</b></label>
<input class="w3-input w3-border" name="prezzoacquisto" type="text" value="<?php echo $row['prezzoAcquisto']; ?>"><br>
<label class="w3-text-blue"><b>Prezzo Vendita:</b></label>
<input class="w3-input w3-border" name="prezzovendita" type="text" value="<?php echo $row['prezzoVendita']; ?>"><br>
<label class="w3-text-blue"><b>Quantità:</b></label>
<input class="w3-input w3-border" name="quantita" type="text" value="<?php echo $row['quantita']; ?>"><br>
<label class="w3-text-blue"><b>Sconto %:</b></label>
<input class="w3-input w3-border" name="sconto" type="text" value="<?php echo $row['scontoPercentuale']; ?>"><br>
<button class="w3-btn w3-blue" name="btn-aggiorna" type="submit">Modifica</button>
<a class="w3-btn w3-blue" href="/bk2/magazzino.php">Indietro<a>
</form>
</div>
<div class="w3-col s12 l3">
<br>
</div>
</div>
</div>
</div>
<?php
include('includes\theme\script.php');
?>
</body>
</html>
<?php
}
else{
header("location: accessonegato.php");
}
?>
i know from the error message the error is in my query code but i don't understand where. i had try multiple time to change the &_GET['art_id'] position in and out of the double quote of the query but noting work.
You need to add the ID to the form when calling the page, otherwise it's not being passed in
<form class="w3-container" action="modificaarticolo.php?art_id=<?php echo $row['id'];?>" method="post">
You may need to alter $row['id'] to have the correct column name.
Although this will cause a slight problem, as the data is fetched in
//RICEVI DATI ARTICOLO DAL DATABASE
if(isset($_GET['art_id'])){
which is before the update, so you may need to move this code below the update to ensure it retrieves the latest data.

Get value from $_GET and use it as a ID

I have problem with reciving $_GET value, I need it as a ID to update my table.
Im using <a href='includes/edit_section.php?id=$id'>Change</a>. Variable $id is properly fetched and it is working, when I click on this link (because it is in table) it shows right value.
But problem is in next part, when I recive it with $_GET. I tryed to make variable ($id = $_GET['id']) but there is no any change.
When I execute this, nothing updates, when I replace $_GET with number it works perfectly.
My table looks like this Table(ID,Title,Paragraph,Image)
This is my code:
<?php
if(isset($_POST['submit'])) {
$title = $_POST['title'];
$paragraph = $_POST['paragraph'];
$query = "UPDATE table SET title = '{$title}', paragraph = '{$paragraph}' WHERE id = '{$_GET['id']}' ";
$c= mysqli_query($connection, $query);
}
This is my form
<form action="edit_section.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title">
<label for="paragraph">Paragraph</label>
<input type="text" class="form-control" name="paragraph">
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="cHANGE">
</div>
</form>
The problem is that you need to send the id when you submit the form, a traditional way of doing it is using a hidden type input, for example <input type="hidden" name="id" value="<?= $id ?>"> And of course it is necessary to pass the id to the view where the form is rendered
So you could try this
<form action="edit_section.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?= $id ?>">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title">
</div>
<div class="form-group">
<label for="paragraph">Paragraph</label>
<input type="text" class="form-control" name="paragraph">
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="cHANGE">
</div>
</form>
And when processing the form
<?php
if(isset($_POST['submit'])) {
$id = $_POST['id'];
$title = $_POST['title'];
$paragraph = $_POST['paragraph'];
$query = "UPDATE table SET title = '{$title}', paragraph = '{$paragraph}' WHERE id = '{$id}' ";
$c= mysqli_query($connection, $query);
}
I separated in its respective form-group the inputs of title and paragraph
At top add
$id = $_GET['id'];
then
basically problem is in
<form action="edit_section.php"
it should be
<form action="edit_section.php?id=<?php echo $id;?>"
plus in your update query id should be ID (capital).. also check for other column names for case-sensitivity
change the action for form at HMTL like tihs
<form action="edit_section.php?<?=$_GET['id']?>" method="post" enctype="multipart/form-data">
Add "id" as a hidden variable in your form.
Edited - Missed out an enclosing double quote for the hidden 'id' value in the form.
<form action="edit_section.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title">
<label for="paragraph">Paragraph</label>
<input type="text" class="form-control" name="paragraph">
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="cHANGE">
</div>
</form>
Make the following changes to your PHP script
<?php
if(isset($_POST['submit'])) {
$title = $_POST['title'];
$id = $_POST['id'];
$paragraph = $_POST['paragraph'];
$query = "UPDATE table SET title = '{$title}', paragraph = '{$paragraph}' WHERE id = '{$id}' ";
$c= mysqli_query($connection, $query);
}
?>

Categories