How to update record got from session? - php

I dont know what to do with this. I want to update a record from database. I have header were there, the fullname of the user is displayed once login, and has also a dropdown. One of this dropdown is Account Setting were a user can update his Information. But I dont know what to do since Its my first time working with php. below is my code in displaying the record.
<?php
$session_useraccount_id=$_SESSION['useraccount_id'];
$qry= "SELECT * FROM tblusersaccount where useraccount_id = '$session_useraccount_id'";
$result=mysql_query($qry) or die(mysql_error());
{
$useraccount_id=mysql_result($result,$i,'useraccount_id');
$fname=mysql_result($result,$i,'fname');
$lname=mysql_result($result,$i,'lname');
$email=mysql_result($result,$i,'email');
$password=mysql_result($result,$i,'password');
$useraccountname=mysql_result($result,$i,'useraccountname');
?>
<?php echo "<a style='color: white' href='updateusers.php?update = $useraccount_id'>$fname $lname &nbsp&nbsp";?>
It gives me the id, and I can display it to other page.
$qry= "SELECT useraccount_id, fname, lname, useraccountname, email, password FROM tblusersaccount";
$result=mysql_query($qry) or die(mysql_error());
{
$useraccount_id=mysql_result($result,$i,'useraccount_id');
$fname=mysql_result($result,$i,'fname');
$lname=mysql_result($result,$i,'lname');
$useraccountname=mysql_result($result,$i,'useraccountname');
$password=mysql_result($result,$i,'password');
}
and below I put this
First Name" required>
and so on.
How can I update my record??

Question not clear, by the way you can provide a button that onCLick, once you collect the account data, executes an update query on server side.

Related

How to submit the values of form before update and after update

I have a form that retrieves data from a database and displays it in respective fields. The user then has the ability to make changes to the form and update those values. I'm able to correctly display the data from the database(SELECT) by clicking a submit button and update it as well(UPDATE) by clicking another submit button. What I want to do is store all the data before update in a separate table, sort of as an audit log that will tell me what the previous fields were and what the new update is like. I want to have both the Update and Insert query on the same submit button as well. How do I go about inserting the 'former' values in my table.
Here's my working update statement:-
if(isset($_POST['submit'])){
// Updating the dealer info using sfid
$sql = "UPDATE tbl_dealer_info ";
$sql .= "SET phone = '".$phone."', email = '".$email."', SFID = '".$sfid."', account_name = '".$account_name."', parent_account = '".$parent_account."', awi_code = '".$awi_code."', sales_portal_id = '".$sales_portal_id."', iae = '".$iae."', rsm_val = '".$rsm_val."', door_type_val = '".$door_type_val."', payment_method_val = '".$payment_method_val."', region_val = '".$region_val."', street_address = '".$street_address."', city = '".$city."', state = '".$state."', zip = '".$zip."', area = '".$area."', market = '".$market."', boost_app = '".$boost_app."', virgin_app = '".$virgin_app."', virgin_mob_app = '".$virgin_mob_app."', start_date = '".$start_date."', bank_name = '".$bank_name."', bank_acc_number = '".$bank_acc_number."', routing_number = '".$routing_number."' WHERE SFID = '".$sfid."' ";
$sql .= "LIMIT 1";
$result = mysqli_query($conn, $sql);
One easy way to do this is using hidden values example
<form>
<input type="text" name="phoneNumberNew">
<input type="hidden" name="phoneNumberOld">
</form>
With this what you can do is submit the form to wherever your table is held and populate the table where the textbox is the new data and the hidden one will be your old data. From there you can submit it onward using more hidden values or whatever means you see fit. This is just one option for you based upon technology everyone has available! :)
When the user submits, the form data will be sent to the action="" page you defined.
In that page, run the select query again, which will give you the current values.
Then run the update query.
In your log you can then output the current values (from the select you did before), and the new values (from the update fields you got from the form).
--- Added this after the OP`s comment:
Let`s say you have a page like profile.php to update a user.
User A goes to profile.php?username=A
User B goes to profile.php?username=B
you can then build your select and update queries and specify which user you want to update.
Another method, which is better and more secure is to setup a session cookie. You then read that cookie to know which session, and hence which user is using the page.
Obviously you want something to ensure the person modifying users is allowed to do so, but that is outside your question (users - roles - security management).
There will then be no chance of mixing up users.

Using sessions to display the profile of a logged in user from a database with PHP

I have been having an issue selecting data from my database based on the user id column. I know that I have to make use of PHP sessions to enable each user see their profile when they login, but I haven't been able to work out the code for this.
Here is what I have so far:
<?php session_start(); include 'dpconfig.php'; $id = $_SESSION['uid'] ?>
<?php
$run = mysqli_query($conn,"Select * from user Where first = '$id'");
$row = mysqli_fetch_array($run, MYSQLI_BOTH); {}
$showid = $row[0];
$showfirst = $row[1];
$showlast = $row[2];
echo $showid;
echo $showfirst;
echo $showlast;
?>
If I run the above code I get nothing echoed out, but if I remove the WHERE clause from my SELECT statement, all logged in users see the first column of my database.
I want each user to see their own profile, I learnt that I need to authenticate session, and I am confused. Please help.
Assumptions
I'm assuming your database has three columns, uid (the id of a user, int, primary key, auto_increment), first (the user's first name, varchar) and last (the user's last name, varchar).
I'm also assuming that when the user logs in, $_SESSION["UID"] is set to the value of the id column in their row.
Solution
As far as I can see, your WHERE clause is wrong. You wrote
Select * from user Where first = '$id'
which essentially means "Select everything from the user table where the first name is equal to the currently logged in user's id". I think you meant something more like
SELECT first, last FROM user WHERE uid='$id'
which means "Select the first and last names from the user table where the id is equal to the currently logged in user's id".
Code
I have re-written your PHP file, to make it a bit more readable and clear. You'll need to change the MySQL connection to whatever you were originally using, but apart from that, everything should work fine.
<?php
session_start();
require("dpconfig.php");
$q = "SELECT first, last FROM user WHERE uid='".$_SESSION["UID"]."'";
$r = mysqli_query($conn,$q);
$a = mysqli_fetch_assoc($r);
echo "First Name: ".$a["first"]."<br>";
echo "Last Name:".$a["last"];
?>
Second Question
For your form:
<form method="post" action="update.php">
<input type="text" name="status"><br>
<button>Submit</button>
</form>
For update.php:
<?php
session_start();
require("dpconfig.php");
if (isset($_POST["status"])) {
$q = "UPDATE user SET status='".addslashes($_POST["status"])."' WHERE uid='".$_SESSION["uid"]."'";
mysqli_query($conn,$q);
}
header("Location:./");
?>

Print specified page, update DB MYSQL and PHP

I have the following code:
<iframe onscroll ="caise4.php" name="frame4"></iframe>
<input type= "submit" onclick ="frames['frame4'].print()" value = "WATER/ELECTRICITY" id = "id4"></a>
This would allow me to print a different page when the button/frame is clicked.
And i have the following PHP code:
<?php
if(isset($_POST['frame4'])){
$SQL_1 = "INSERT INTO `tick4` (`ticket_id`, `ticket_date`) VALUES (NULL, CURRENT_TIMESTAMP)";
$result_1 = mysqli_query($db, $SQL_1);
$SQL_2 = "UPDATE v_attente_service AS vas
JOIN (SELECT COUNT(ticket_id) AS cnt FROM tick4) AS ti
SET vas.NOMBATTE = ti.cnt
WHERE vas.CODESERV=4";
$result_2 = mysqli_query($db, $SQL_2);
}
?>
When the user clicks, a new row needs to be inserted in the table "tick4". Then "NOMBATTE" in "v_attente_service" is updated.
Basically, when the user prints a ticket, the number of tickets is updated, and the number of people waiting in the queue is also updated.
The problem I have: I get redirected to the page preview, but nothing is displayed there. Also, nothing gets updated in my tables.
I am using phpMyAdmin by the way.
Any help would be greatly appreciated. Thank you.

how to see online users status

how can i update to database status = 1 where id =$id using an update statement after a login for example or a separate page
please any one how the string would look like?
this is for a member status script im trying to make it so online members can have there own page
this is just the viewing part and the updating part next
mysql_select_db("messages") or die(mysql_error());
$data = mysql_query("SELECT on_status FROM users WHERE id='$user_id'")
or die(mysql_error());
echo "<table border cellpadding=3 bgcolor=\"00FF00\">";
while($info = mysql_fetch_array( $data ))
{
echo "<tr>";
echo "<th>User:</th> <td>".$info['user_name'] . "</td> ";
echo "<th>Status:</th> <td>".$info['on_status'] . " </td></tr>";
}
echo "</table>";
?>
<p><br>
Go Back<p>
Add a field called status in users table. where users table containing all data of user.
UPDATE status to online when user login.. this code will be placed after validating the username and password..
// Eg: When you starting a session for the user write this update
When a user trying to logout UPDATE status to offline .. this code will be placed beside the session_destroy() statement
Try this update statement:
"UPDATE `users` SET `on_status` = 1 WHERE `id` = '$user_id'";
Though, if I may suggest this: Keep a timestamp of the last activity. That way, if the page hasn't been refreshed for more than 5 minutes (or longer), you can display the user as 'offline', even if the user hasn't hit the Logout button.

can't get mysql query to output user and count at the same time

I'm new at mysql queries so forgive me if I'm missing something stupid.
I have a table called marketeers with fields id, user, site, link, ip.
People click on different links on my site and it gathers info about the ip addresses and the user who made the link etc so I can determine which link performs best.
I want it to count the unique ip's that a link has and then display the user, counted unique links, and the link itself.
It almost works except it won't display the user. Here is the code.
Thanks for any help...
Dan
$query = "SELECT link, COUNT(DISTINCT(ip)) FROM marketeers GROUP BY link ORDER BY COUNT(DISTINCT(ip))";
// Execute the query
$result = mysql_query( $query );
echo $result;
echo "<br><br>";
if (!$result){
die ("Could not query the database: <br />". mysql_error( ));
}
else{
while($row = mysql_fetch_array($result))
{
echo "{$row['user']} ".
"{$row[count('ip')]} " .
"{$row['link']}<br><br> ";
}
}
}
You're not selecting the user field from your database. You're fetching two fields only. 'link', and the count of IPs. Try this:
SELECT link, COUNT(DISTINCT(ip)) as cnt, user FROM ...
and then
echo "{$row['user']} {'$row['cnt']} {$row['link']}<br />";
You're not asking for the user, so it's logical it's not there :)
try:
SELECT user, link, COUNT(DISTINCT(ip)) FROM ....

Categories