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);
}
?>
Related
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.
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
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.
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
I am making a forum, and I can add a reaction to save in the database, the only problem now is that it will leave empty id in the database.
The database:
How it will post it now:
Now I need to find a way to add the ledenpagina_id, topic_id and klant_id automatic. The klant_id needs to be set based on the klant session, but I am not sure how to get that.
The topic_id is set as active_id like this:
$actieftopicid = $topic['id'];`
But I don't know how to add that in the post so it will save it the correct way in the database.
I have tried doing this:
$q1['topic_id'] = $app->check_string($_POST[$topic['id']]);
But that isn't working.
The code to post it in the database:
<?php
if(isset($_POST['react_btn'])){
unset($q1);
$q1['reactie'] = $app->check_string($_POST['reactie']);
$q1['topic_id'] = $app->check_string($_POST[$topic['id']]);
$app->insert_query('reacties', $q1, 'id');
}
?>
<form action="" method="post">
<div class="form-group">
<label for="comment">Reactie:</label>
<textarea class="form-control" name="reactie" rows="3" id="comment"></textarea>
<button type="submit" name="react_btn" class="btn btn-primary">Plaats reactie</button>
</div>
</form>
You can add the id of the topic into the form like this:
<form action="" method="post">
<div class="form-group">
<label for="comment">Reactie:</label>
<textarea class="form-control" name="reactie" rows="3" id="comment"></textarea>
<input type="hidden" name="topicid" value="<?php echo $topic['id']; ?>">
<button type="submit" name="react_btn" class="btn btn-primary">Plaats reactie</button>
</div>
</form>
and then you can use it like $_POST['topicid'], since that is the name of the hidden input. Also, instead of
unset($q1);
you need to initialize $q1 properly:
$q1 = array();