Update query showing old data after submitting - php

I have the following code:
$sql = mysql_query("UPDATE users SET name='$name' WHERE email='$email'") or die(mysql_error());
echo "<h4>Your information has been updated</h4><br />";
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<label for='name'>Name</label><br /><br />
<input class="input-text required" name="name" type="text" value="<?php echo $row[name]; ?>" />
</p>
<p><input type="submit" name="update" value="Update" /></p>
</form>
After clicking Update, it updates the information in the database, but the <?php echo $row['name']; ?> still shows the old value. Only after refreshing the page does it show the updated information. I could have it refresh the page after updating by echoing meta refresh but I want it to still show the echo saying "Your info has been updated" which doesn't happen if I set it to refresh. Is there any solution?

You will need to requery the database to repopulate $row or explicitly set the $row variable to your values (I would recommend repopulating it, just to be safe).
Or echo $name, instead of $row[name]

A update of the database is not a new select of the data.
You have 2 options:
after the update, do a new select of the data
instead of writing the row back to the form, write the posted value back

Related

PHP foreach executing code multiple times

At the bottom of this code you'll see an 'Accept Offer' button, when I click on that another piece of code gets executed as you can see on the bottom of this post.
For example this project has 3 bidders, so 3 times bidder_id and writer_bid so I use 'foreach' and load it in divs, works fine, but now I need to store those variables in a database, which technically works but it doesn't store the bids from the row I pull them from, it just takes the data from the last row, that is if I place the code at the bottom of this thread in my header.
However when I put it inside the loop it executes three times, I saw that when I got an error message that I had to close 3 times cause there are 3 rows in the database table that I pull the data from.
How can I prevent this, and either have it load once when the code is inside the foreach loop, or have it pull the correct writer_bid and bidder_id to store.
<div class="WB-Bottom-block lefts">
<?php $getBidders=" AND project_id=$project_id"; $bidders=getBidder($getBidders); foreach($bidders as $bidder) {
$bidder_id=$bidder['writer_id'];
$writer_bid=$bidder['writer_bid'];
?>
<div class="findwriters_boxes">
<div class="findwriters_right">
<div style="float:right;margin-top:6px;width:170px;">
<input type="hidden" name="writer_bid" id="writer_bid" value="<?php echo $writer_bid; ?>" />
<input type="hidden" name="bidder_id" id="bidder_id" value="<?php echo $bidder_id; ?>" />
<input type="submit" class="homebtn11" name="submit" id="submit" value="Accept Offer"/>
</div>
</div>
</div><?php } ?>
Below the code that needs to be executed and that results in issues, whether I place it inside the foreach loop, or inside the header instead.
As you can see I tried to store it in input fields so that it stays there so the header can pull it on refresh of the page / click of the button.
<?php if(isset($_POST['todo']) && $_POST['todo']=='submit_project') {
$balance=get_client_balance_info($current_user->ID);
$writer_bid=$_POST['writer_bid'];
$bidder_id=$_POST['bidder_id'];
if($balance >= $_POST['writer_bid']) {
global $wpdb;
$sql3="UPDATE `wp_project` SET `writer_id` = '".$bidder_id."' WHERE `id` =". $project_id;
$wpdb->query($sql3);
$sql4="UPDATE `wp_project` SET `price` = '".$writer_bid."' WHERE `id` =". $project_id;
$wpdb->query($sql4);
$sql5="UPDATE `wp_project` SET `status` = '2' WHERE `id` =". $project_id;
$wpdb->query($sql5);
$success_msg="You accepted a bid, the money will be deducted from your account.";
}
else $fail_msg="Your balance is not sufficient.";
I think you should make a form for each div that you are adding right now you are putting the bidder_id in the different inputs but the same name.
So it will get the last inputs, maybe it's better to specify the inputs with the row id or to separate the forms or make the input names as array.
I hope this helps you.
I fixed it with the help of Diar Selimi like this:
<div style="float:right;margin-top:6px;width:170px;">
<form action="" name="frmeditor" method="post" id="frmeditor" >
<input type="hidden" name="todo" id="todo" value="submit_project" />
<input type="hidden" name="writer_bid" id="writer_bid" value="<?php echo $writer_bid; ?>" />
<input type="hidden" name="writer_id" id="writer_id" value="<?php echo $writer_id; ?>" />
<input type="submit" class="homebtn11" name="submit" id="submit" value="Accept Offer"/>
</form>
Before that my form and value="submit_project" tags were scattered all over the place!

Using a PHP variable as a form fields default value

I have a created an HTML form where users sign up and input there data into an SQL database. I have then retrieved that data in a webpage where they can view their profile. I have created a page where users can edit there profile by creating a form which updates the value in the SQL database for there user id.
I would like the form to use the current value of the SQL cell as the default for that user to make alterations easier. Example: currently user 7 has their city set as New York, when they visits the edit info page, the city field in the form already hase New York as the default value.
I have no problem getting the SQL info and assigning it to a variable, I just don't understand how to set it as the default value. I am aware of how you set default values for input fields though.
My code:
<?php
$id = $_SESSION["user_id"];
// Create a query for the database
$query = "SELECT full_name FROM users WHERE id = $id LIMIT 1";
// Get a response from the database by sending the connection
// and the query
$response = #mysqli_query($dbc, $query);
// If the query executed properly proceed
if($response){
while($row = mysqli_fetch_array($response)){
echo $row['full_name'];
echo mysqli_error();
}
}
?>
<input type="text" name="aboutme" defualt="<?php echo $row['aboutme'] ?>" >
There's no default value for html input.
Input can has value, using attribute value:
<input type="text" name="some_name" value="Some value" />
In your case it's
<input type="text" name="aboutme" value="<?php echo $row['aboutme']?> />
Input can also has placeholder - some value that is present in an input, but erased when user starts to edit input's content:
<input type="text" name="aboutme" value="<?php echo $row['aboutme']?> placeholder="some value" />
How about
<?php
$id = $_SESSION["user_id"];
// Create a query for the database
$query = "SELECT full_name FROM users WHERE id = $id LIMIT 1";
// Get a response from the database by sending the connection
// and the query
$response = #mysqli_query($dbc, $query);
// If the query executed properly proceed
if($response){
while($row = mysqli_fetch_array($response)){
echo $row['full_name'];
?>
<input type="text" name="aboutme" value="<?php echo $row['aboutme'] ?>" >
<?php
echo mysqli_error();
}
}
?>
And here is a good example http://www.w3schools.com/php/showphpfile.asp?filename=demo_db_select_pdo
Neither of the answers worked and upon further research and trial and error I created a solution.
I changed the value that was store in the array to just be a normal php variable:
$aboutme = $row['aboutme'];
I then called that variable using the following code:
<input type="text" name="aboutme" value="<?php echo htmlspecialchars($aboutme); ?>" >
Thanks for your help.
I hope you find my answer useful.
Why don't you try using it as a place holder? This will provide editable text.
<input type="text" name="aboutme" placeholder="<?php echo $row['aboutme'];" />

PHP MySQL - Undefined Index in Edit Data

I make file details.php that show list of data from MySQL, and I make edit.php for it.
When I click edit.php it's automatically go to edit.php?id=detailsid (it works) but when I click submit on edit data, it's nothing changed and error.
This is my Edit.php
<form method="post" action="editdata.php">
<?php
include 'config.php';
$id=$_GET['id'];
$sqlTampil="select * from data_korban Where kasus_id=$id";
$qryTampil=mysql_query($sqlTampil);
$dataTampil=mysql_fetch_array($qryTampil);
$data=mysql_fetch_array($qryTampil);
?>
This is some of my HTML form
<input type="DATE" name="tanggal" size="20" value="<?php echo $dataTampil['tanggal']; ?>">
<input type="text" name="namakorban" size="40" value="<?php echo $dataTampil['namakorban']; ?>">
<input type="text" name="umurkorban" size="5" value="<?php echo $dataTampil['umurkorban']; ?>">
<input type="submit" name="submit" value="SUBMIT">
When I click Submit it's automatically call editdata.php but there is no change on my MySQL row ..
This is my editdata.php
<?php
include "config.php";
$id=$_GET['id'];
$tanggal = $_POST['tanggal'];
$namakorban = $_POST['namakorban'];
$umurkorban = $_POST['umurkorban'];
$update = "UPDATE data_korban SET tanggal='$tanggal', namakorban='$namakorban', umurkorban='$umurkorban' WHERE kasus_id = '$id'";
$hasil = mysql_query($update);
if ($hasil)
echo "<center>Update Success<center>";
else
echo "<center><h3><a href=pencarian.php>Back Tampil Data</a></h3></center>";
?>
There is something in my Error_Log
PHP Notice: Undefined index: id in /home/lombokdi/public_html/dbanak/editdata.php on line 4 ------ it's $id=$_GET['id'];
I have change to $_POST['id']; it's still error.
Your form does not seem to include the id named field.
And you are trying to read the non existing field from the request.
Request is formed to send data over post method but you tried to read id parameter using get method. As the value for id is not found, a null is used in where clause and that affected no rows updated.
$id=$_GET['id'];
$tanggal = $_POST['tanggal'];
$namakorban = $_POST['namakorban'];
$umurkorban = $_POST['umurkorban'];
the value for ID is $id=$_GET['id']; ... id come from detail.php?id=24 (example) go to edit.php?id=24 .. If I'm wrong, how to set the ID ??
In edit.php add id named element to the form with value read from the request. And on submitting the form, id goes to editdata.php page.
<input type="DATE" name="tanggal" size="20" value="<?php echo $dataTampil['tanggal']; ?>">
<input type="hidden" name="id" value="<?php echo $id['id']; ?>">
<input type="text" name="namakorban" size="40" value="<?php echo $dataTampil['namakorban']; ?>">
<input type="text" name="umurkorban" size="5" value="<?php echo $dataTampil['umurkorban']; ?>">
And in editdata.php:
Change:
$id=$_GET['id'];
To:
$id=$_POST['id'];
It's because you don't have the filed "id" if you switch to editdata.php
add: <input type="hidden" name="id" value="<?php $_GET['id']; ?>">
to your form in Edit.php
and replace
$id=$_GET['id']; with $id= $_POST['id']; in editdata.php.
Try like this
$id=$_GET['id'];
$sql=mysqli_query($con,"Update client set nama='$nama',instansi='$instansi',telepon='$telepon',email='$email' where id = '".$_GET['id']."'");
if($sql)
{
$msg="Your Client updated Successfully";
header('location:manage-client.php');
}
for after body add this query
$sql=mysqli_query($con,"select * from client where id = '".$_GET['id']."'");
while($data=mysqli_fetch_array($sql))
You are using $_GET to get the id. It is best do var_dump($_REQUEST);

Sending PHP variables back and forth between pages

I've created a registration form that successfully passes its variables from the registration page (go-gold.php) to a summary/verfication page (go-gold-summary.php). The data appears correctly on the second page.
However, I want to able to use an image button to return back to the registration page, in case the user made an entry error. Going back, the original form should now be populated with the data that was first entered.
The problem is that I cannot re-send/return the data from the second page, back to the first. My text fields appear blank. I do NOT want to use Session variables.
The code is truncated from the entire page.
Registration Page (go-gold.php):
<?php
$customer_name = $_POST['customer_name'];
?>
<form action="go-gold-summary.php" method="post">
Name: <input type="text" name="customer_name" id="customer_name" value= "<?php echo $customer_name ?>" />
<input name="<?php echo $customer_name ?>" type="hidden" id="<?php echo $customer_name ?>">
</form>
Summary Page (go-gold-summary.php)
<?php
$customer_name = $_POST['customer_name'];
?>
<form action="go-gold.php" method="post">
Name: <?php echo $customer_name ?> <input type="hidden" id="<?php echo $customer_name ?>" name="<?php echo $customer_name ?>">
<INPUT TYPE="image" src="images/arrow_back.png" id="arrow" alt="Back to Registration"> (Button to go back to Registration Page)
</form>
Thanks!
go-gold-summary.php should be changed like this.
<?php
$customer_name = $_POST['customer_name'];
?>
<form action="go-gold.php" method="post">
Name: <?php echo $customer_name ?> <input type="hidden" value="<?php echo $customer_name ?>" name="customer_name">
<INPUT TYPE="submit" src="images/arrow_back.png" id="arrow" alt="Back to Registration"> (Button to go back to Registration Page)
</form>
notice how I've changed this line
<input type="hidden" id="<?php echo $customer_name ?>" name="<?php echo $customer_name ?>">
into this
<input type="hidden" value="<?php echo $customer_name ?>" name="customer_name">
$_POST is an associative array and as you submit the form it will be populated like this:
$_POST["index"] = value;
where "index" is the text field "name" and value is the text field value.
You've missed that one in your code. Just update it with my code and it will work
Why you would not want to use the php session? Please give any reason for not to use it. I am asking this way since my reputation does not allow me to comment questions or answers any other than my own. Plese do not -1 for this.
Another way could be using cookies to store the data temporarily, but that and posting the data back and forth in the post request is really insecure compared to session.
there are very few ways to maintain variables across pages. The alternative is to have separate form on the second page with hidden text fields containing the $_POST data, and the submit button calls the previous page. No way of getting around the "back button" on a browser though unfortunately.
I missed the bold text about the session variables - disregard if this does not apply:
one way to maintain variables across pages on the server side is to use $_SESSION
first include the following at the top of your PHP pages to keep a session active:
session_start();
once you submit the for and move to page 2, add the following:
$_SESSION['customer_name'] = $_POST['customer_name'];
As well, on the first page, you could change the form element as such:
<input type="text" name="customer_name" value="<?PHP if isset($_SESSION['customer_name'] || !empty($_SESSION['customer_name'])) { echo $_SESSION['customer_name']; } ?>">
this will keep the filled in data and display it when the user returns tot he page, and if they put in something different it will be updated when they hit page 2 again.

DELETE FROM table WHERE ID='$id' — Variable refuses to stick

Trying to perform a very simple task here.
I have an <ol> that contains 4 rows of data in some handy <li>s. I want to add a delete button to remove the row from the table. The script in delete.php appears to have finished, but the row is never removed when I go back and check dashboard.php and PHPMyAdmin for the listing.
Here's the code for the delete button (inside PHP):
Print "<form action=delete.php method=POST><input name=".$info['ID']." type=hidden><input type=submit name=submit value=Remove></form>";
Moving on to delete.php:
<?
//initilize PHP
if($_POST['submit']) //If submit is hit
{
//then connect as user
//change user and password to your mySQL name and password
mysql_connect("mysql.***.com","***","***") or die(mysql_error());
//select which database you want to edit
mysql_select_db("shpdb") or die(mysql_error());
//convert all the posts to variables:
$id = $_POST['ID'];
$result=mysql_query("DELETE FROM savannah WHERE ID='$id'") or die(mysql_error());
//confirm
echo "Patient removed. <a href=dashboard.php>Return to Dashboard</a>";
}
?>
Database is: shpdb
Table is: savannah
Ideas?
It's refusing to stick because you're calling it one thing and getting it with another. Change:
"<input name=".$info['ID']." type=hidden>"
to
"<input name=ID value=".$info['ID']." type=hidden>"
because in delete.php you're trying to access it with:
$id = $_POST['ID'];
You should really quote attribute values as well ie:
print <<<END
form action="delete.php" method="post">
<input type="hidden" name="ID" value="$info[ID]">
<input type="submit" name="submit" value="Remove">
</form>
END;
or even:
?>
form action="delete.php" method="post">
<input type="hidden" name="ID" value="<?php echo $info['ID'] ?>">
<input type="submit" name="submit" value="Remove">
</form>
<?
Please, for the love of the web, don't built an SQL query yourself. Use PDO.
Just another point I'd like to make. I'm 95% sure that you can't give an input a numeric name/id attribute. It has to be like "id_1" not "1".
Also with php you can do arrays.
So you could do this
<input name="delete[2]">
then in your php
if(isset($_POST['delete']))
foreach($_POST['delete'] as $key=>$val)
if($_POST['delete'][$key]) delete from table where id = $val

Categories