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.
Related
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:./");
?>
I have a table in mysql and two web pages 1)admin.php 2)student.php. in admin.php web page he inserts records of students.
$sql = "insert into student_data values('$p_id','$p_name','$p_surname','$p_email','$address','$circle','$p_cont','$loc' ,'$user_p','$pass_p','$title')";
$data = mysql_query($sql,$conn);
if($data)
{
echo $p_name ." account created";
}
else
{
echo "insertion not successful"; echo mysql_error();
and in the second page i.e. student.php these records are retrieved
<?php include"config.php";
$sqli = mysql_query("select * from student_data where p_id='$name_perm'",$conn);
while($rows = mysql_fetch_array($sqli))
{
echo '<div class="gallerycontainer">';
echo '<table>';
echo '<tr>';
echo '<td class="thumbnail" href="#thumb"><img height="120" width="200" src="data:image;base64,'.$rows['Image']. '"> <span><img height="240" width="400" src="data:image;base64,'.$rows['Image']. '" /></td>';
echo '<td>'.$rows['p_name'].'</td>';
echo '<td>'.$rows['p_surname'].'</td></tr>';
echo '</table>';
echo '</div>';
}
in a table format and students can see all the records. this is just an example.
now what I want is that, student wants to update his information so, HE REQUESTS ADMIN TO DO THE CHANGES. in student.php there is update section and he fills out the form and submits it.
ON THE ADMIN SIDE> when admin logs in, he gets a notification that there is a update request from student side. he reviews it and do the necessary change.
for now, I made a table called update_request , students fills the form and insert the record in those table. there is already stack of updates received by students and just want the admin to be notified that, a new record has been inserted and this is the record, kindly do the change.
I don't have enough reputation to comment so I'll answer here.
Why not add a status column to your update_request table? Example : If you add a new record to update_request, the status is 0 or pending, when an admin logins you retrive all records that are status 0. When the admin treats the request, you update the record to status 1 or completed.
I'm trying to create a page that shows the name of multiple mysql tables and creates a link for each one. When a user clicks a link, he's taken to a page that shows him the table contents.
For example:
Link with table name.
Table name
The user clicks the link and is taken to the page contents.php. That page prints the table contents.
For example if I have a table with Name and Age columns and John and 24 inserted in the columns, the page would print John and 24.
I appreciate if anybody can help me.
It would be way easier to do this with a GET request rather than a post request.
Table name
Then in your contents.php file:
<?php
$tablename = $_GET["tableName"]
/* all your queries that you want to do with tablename goes here */
?>
try this one...
$result = mysql_query("show tables"); // run the query and assign the result to $result
while($table = mysql_fetch_array($result)) { // go through each row that was returned in $result
echo "<a href=\"content.php?tableName=".$table[0]."\" >".$table[0] . "</a><BR>"; // print the table that was returned on that row.
}
content.php...
if(isset($_GET["tableName"]))
{
$select="select *from ".$_GET["tableName"];
$result=mysql_query($select);
while($row=mysql_fetch_array($result))
{
echo $row["name"];
echo $row["age"];
}
}
Here's what I have. This code grabs the data from all of the users and subtracts 1 from user_days then updates every user's user_days row.
$result = mysqli_query($con,"SELECT * FROM users");
while($row = mysqli_fetch_array($result))
{
$minusone = $row['user_days']-1;
mysqli_query($con,"UPDATE users SET user_days=$minusone");
echo "<br />";
echo $row['user_days'];
}
The problem I'm having is this:
Instead of subtracting 1 from each user and updating each users field,
it's updating each user's field with the value from the first user.
example:
before updating
user 1 has 30 days
user 2 has 60 days
after updating
user 1 has 29 days
user 2 has 29 days (instead of 59 days)
Any help is appreciated and I hope this question is easy to understand.
Just to clarify, I DO want to update every field.
I just don't want the updates to be duplicated from the first result.
Thanks for all of the answers, this has given me a lot of help.
Why don't you just run UPDATE users SET user_days = user_days-1 WHERE id=XXXXX? And then select the whole thing?
When you update your record, you need to specify the user id for the record of interest, otherwise you current query updates all the rows in your table.
You should point which record to UPDATE
mysqli_query($con,"UPDATE users SET user_days=$minusone WHERE id=XXXXX");
The problem is with the UPDATE statement. Without a WHERE clause it will apply the SET clause to every row in the database. Assuming you have a unique id column named id in the users table, you could modify your code like this:
while($row = mysqli_fetch_array($result))
{
$minusone = $row['user_days']-1;
$user_id = $row['id'];
mysqli_query($con,"UPDATE users SET user_days=$minusone WHERE id=$user_id");
echo "<br />";
echo $row['user_days'];
}
Use the following Query:
mysqli_query($con,"UPDATE users SET user_days=$minusone WHERE user_ID = '".$row['user_ID']."'");
You're updating all the users with the same $minusone value. You need a WHERE clause in your update statement, like this:
$result = mysqli_query($con,"SELECT * FROM users");
while($row = mysqli_fetch_array($result))
{
$minusone = $row['user_days']-1;
$id_user = $row['id_user'];
mysqli_query($con,"UPDATE users SET user_days=$minusone WHERE id_user = $id_user");
echo "<br />";
echo $row['user_days'];
}
Another way of doing what you want would be:
...
$result = mysqli_query($con,"UPDATE users SET user_days = user_days - 1");
...
This assuming you want to substracts 1 to all user_days.
As already pointed out by other answers, the best way to do this is to replace your entire code block with one line.
mysqli_query($con, "UPDATE users SET user_days=user_days-1");
Then you can SELECT and display the information as needed.
Without knowing exactly what you are using user_days for, I'm thinking there may be a better approach to what you are trying to do. I am assuming this is some kind of subscription service, and this code will be run once per day to decrement the number of days to allow them to access the service.
A better approach would be to have a subscriptionExpires field in your database, which would hold a datetime value. Using your approach, if the job that runs this fails, every user will get an extra day. What if a web spider or a user finds your script, your users accounts will expire early. If you use an actual date for when the account expires, there's no guessing if the current value is correct.
I'm sorry if this question frustrates anyone ... I am truly a beginner to PHP + MYSQL but I would love to make some progress on this one!
My Goal:
To create a table that I can view in a web page.
To be able to add data (rows) to this table and still have the data be there the next time the page is loaded.
Step 1: Creating the table
<html>
<body>
<?php
// Conect to MYSQL
$con = mysql_connect("localhost", "My_Username", "My_Password") or die(mysql_error());
echo "Connected to MYSQL </br>";
mysql_select_db("My_Database") or die(mysql_error());
echo "Connected to Database";
// Create a MySQL table in the selected database (called ExampleTable)
$sql = "CREATE TABLE ExampleTable
(
// Set the primary key (personID)
personID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(personID),
ColumnOne varchar(15)
ColumnTwo varchar(15)
ColumnThree varChar(30)
)";
// Execute query
mysql_query($sql,$con);
mysql_query("INSERT INTO ExampleTable(ColumnOne, ColumnTwo, ColumnThree)
VALUES ('Data1', 'Data2', 'Data3')");
mysql_query("INSERT INTO ExampleTable(ColumnOne, ColumnTwo, ColumnThree)
VALUES ('Data_Data1', 'Data_Data2', 'Data_Data3')");
mysql_close($con);
?>
</body>
</html>
So, I have now created a MYSQL table with two three columns, and two rows of data. How can I get this table to show up on a web page?
Step 2: Saving / Retrieving saved data
Is there some way that I can add data to a table, so that the data will be there permanently? - or is this how it works by default?
For example: Let's say that I have a form with a button on it. When the button is clicked a new row is added to the table 'ExampleTable'. The next time the user visits the page the table will be updated with his newly added data.
Thank you very much for any help. I understand that I am a beginner and do not fully understand these topics yet. Any responses will be greatly appreciated.
I'd recommend creating the table in a separate step, not from PHP; you don't want every time your web page is refreshed to create a new table.
Once your table is created, you can get the data from it by executing (within your PHP, using mysql_query) a query like "SELECT * from {tablename}". Once you've got that query result from mysql, then you can use the various PHP looping and mysql record reading methods to output the results from your query in the form you want into the page your PHP script will be serving to your client.
There are SELECT and UPDATE queries to do this. For example if you want to show data in a table, you would use query looking like this:
SELECT * FROM ExampleTable
In PHP, you can work with these data for example like this:
$query = mysql_query("SELECT * FROM ExampleTable");
while ($row = mysql_fetch_array($query)) {
echo $row["ColumnOne"];
}
And to the UPDATE query:
UPDATE ExampleTable SET ColumnOne = 'some value'
Usage in PHP is also with mysql_query. You can also use WHERE conditions in the query.
1 - Displaying All Data
<?php
// Conect to MYSQL
$con = mysql_connect("localhost", "My_Username", "My_Password") or die(mysql_error());
echo "Connected to MYSQL </br>";
mysql_select_db("My_Database") or die(mysql_error());
echo "Connected to Database";
// Get all the data from the "ExampleTable" table
$result = mysql_query("SELECT * FROM ExampleTable") or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>ColumnOne</th> <th>CoumnTwo</th> <th>CoumnThree</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['ColumnOne'];
echo "</td><td>";
echo $row['ColumnTwo'];
echo "</td><td>";
echo $row['ColumnThree'];
echo "</td></tr>";
}
echo "</table>";
?>
From: http://www.tizag.com/mysqlTutorial/mysqlselect.php
2 - Data Storage
Anything INSERT-ed into a database will remain* in the database until it is explicitly deleted.
*with few exceptions - but you'll learn about them a little later in your database journey :)