I'm trying to build a status/post. I have 4 fields in my table(post) id, user_id, status_time and status_content. I want to get the data on my status_content of 1 user_id only, can someone help me?
For example:
I have 3 users
user 1 = posted 3times
user 2 = posted 2times
user 3 = posted 5times
How can i only get all the data posted by user 3?
Sorry for my english.
Code to send data to my database
profile.php
if(isset($_POST['status_content'])){
$status_content = $_POST['status_content'];
if(!empty($_POST['status_content'])){
add_status($_SESSION['accts_id'], $status_time, $status_content);
}
}
core.php
function add_status($accts_id, $status_time, $status_content){
$sql = "INSERT INTO `post` (user_id, status_time, status_content) VALUES ('$accts_id', '$status_time', '$status_content')";
mysql_query($sql);}
you can use this as follows
to get data particularly for user 3 then
select status_content from post where user_id = '3'
to get data for any users
select status_content from post where user_id = $_SESSION['accts_id'];
[UPDATED]
Related
the code works fine when it is only one user but once i create a second user, the data doesn't get inserted into the database
$sql = "INSERT INTO images ( users_id, picture, Carehome, dateworked, hours)
VALUES ((SELECT id FROM users ), '$target_file', '$Carehome', '$dateworked', '$hours')";
You call all users ID
SELECT id FROM users
You can solve it by get it id by id
<?php
$sql_ids = "SELECT id FROM users";
//get users row by row
//your_db_conn is example you must replace it by your variable
while($row = mysqli_fetch_assoc(mysqli_query(your_db_conn,$sql_ids))){
//save user id in $id
$id = $row['id'];
$sql = "INSERT INTO images ( users_id, picture, Carehome, dateworked, hours)
VALUES ('$id', '$target_file', '$Carehome', '$dateworked','$hours')";
mysqli_query(your_db_conn,$sql);
}
?>
Make sure user id is not a primary key in images table
I'm making a simple private messing system. When a user sends a message he/she has to fill in three fields: 'to', 'subject' and 'message'. In the 'to' field a user can enter a username to send his/hers message to. Then, using mySql (phpmyadmin), I try to fetch the corresponding user_id from the users table.
for example, user_id '1' has user_name 'testing'. When a user types in 'testing' in the 'to' field, my code searches in the users table for the user with the user_name 'testing' and takes the corresponding user_id.
After taking the user_id it puts the user_id in the conversations_members table.
My problem is that the code DOES take the correct user_id (I checked this by printing the array), but it doesn't put the correct user_id in the table. It always puts 0.
See code below, here I add the user_id to the array $user_ids
else if(!empty($_POST['to'])){
$stmt = $db->prepare("SELECT user_id FROM user WHERE user_name = :user_name");
$stmt->bindValue(':user_name', $_POST['to'], PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if ($result) {
$user_ids['user_id'] = $result['user_id'];
}
See code below, here I try to add the user_id to the table
$sql4 = $db->prepare("INSERT INTO conversations_members
(conversation_id, user_id, conversation_last_view, conversation_deleted)
VALUES (:conid, :usid, :clv, :condel)");
$sql4->execute(array(
"conid" => "{$conversation_id}",
"usid" => "{$user_ids[0]['user_id']}",
"clv" => "0",
"condel" => "0"
));
I really hope anyone can help, I've been struggling for a few hours now :(
Thanks in advance!!
I have a database that consists of 3 tables.
user: 4 columns userId [primary key], name, emailId, password
residentController: 3 columns userId [foreign], departmentId [foreign], checkDefault
checkDefault will be set to 1.
department with 6 columns: departmentId [primaryKey], departmentName, departmentDesc, departmentLink, checkDefault, departmentCategory
Here is the sequence that follows when a user registers for an android application. The following php script will be called.
It will add a record of the user into the database. After insertion of the data in the user table, I want to get all the "departmentNames" from department table and insert it into residentController. In the end, after particular user registers, then residentController must have these values populated into the residentController table.
userId departmentId checkDefault
---------------------------------
1 1 1
1 2 1
1 3 1
1 4 1
1 5 1
Here is my php script.
<?php
require "init.php";
$fullname = "Fahman Khan";//$_POST["user"];
$emailid = "fxk120#hotmail.com";//$_POST["user_name"];
$userpass = "Windows";//$_POST["user_pass"];
$sql_query = "insert into user values(NULL,'$fullname','$emailid','$userpass')";
if(mysqli_query($connection, $sql_query))
{
echo "<h2> Data insertion success</h2>";
//Get the user id of the the registered user
$sql_queryTwo = "SELECT userId FROM user WHERE emailId='".$emailid."'";
//Store the result of the query in a variable
$result= mysqli_query($connection,$sql_queryTwo);
$row = mysqli_fetch_array($result);
//Retrive the user id
$data = $row[0];
//Insert default departments into residentController
}
else
{
//echo "Data insertion error".mysqli_error($connection);
}
?>
My question is that in my current php script, I don't know how I can insert departmentID into residentController table. departmentId is a foreign key and in my current department table there are 5 departments. I just need help writing the part in my php where I can insert in values into my residentController table. As of now, the current php script only adds a new user, but I need help inserting values into my residentController table.
I am having a problem locating comments for a given user with the following table structure:
usertable (id, userid, name)
comments (id, commentname, date)
Note: usertable.id is not the same as comments.id, and they are both autoincrement
How should I go about updating these tables to fix this problem?
Update
Is this code good for all users get their own votes when someone voted as thilo savage told me ?
$sth = thumbsup::db()->prepare(
'INSERT INTO'
.thumbsup::config('database_table_prefix')
.'votes_users(vid, userid) VALUES (?,?)');
$sth->execute(array($this->vid, $userid));
You've got two options:
Add a 'uid' column to the comments table which references the usertable's 'id' column. That way, you have a way to keep track of which comments belong to which users.
Create a table 'user_comment' with the columns 'uid' and 'cid'. This option leaves the two existing tables as they are, and the 'user_comment' table is responsible for keeping track of which comments belong to which users.
EDIT: Rewritten to use many-to-many relationship because current tables can't be altered.
Create a new table called comments_users with these fields:
cuid (primary key and auto increment) | cid | uid
Then get all of a user's comments with this code:
$user_id = '1234';
// get all the user's comment ids from comments_users table
$find = mysql_query("SELECT `cid` FROM `comments_users` WHERE `uid` = '".$user_id."'");
// generate a query that grabs all those comments
$load = "SELECT * FROM `comments` WHERE ";
while ($row = mysql_fetch_array($find) {
$load .= "`id` = '".$row['cid']."' OR ";
}
// shop off the last OR
$load = substr($load,0,-4);
// put all the user's comments into comments array
$q = mysql_query($load);
while ($comment = mysql_fetch_array($q)) {
$comments[] = $comment
}
print_r($comments);
As far as inserting goes, you'll insert comments into the comments table like you normally would, but then you'd ALSO insert a row into comments_users table filling in the appropriate cid and uid for that comment
I have some insurance information in a website, and I'd like to only edit certain fields the user wants to change like for example:
user, id, phone, address, city
and the user wants to change his city and phone...do i have to make a query for each specific case or is there a code that can help me retrieve the key(phone) and value (9397171602)??
to then send it in a query
Basic update would take the form of:
UPDATE table_name SET column_1 = value_1, column_2 = value_2 WHERE column_3 = value_3
Where col1, col2 would be your city and phone, and col3 would be the user id. Check out the MySQL website http://dev.mysql.com/doc/refman/5.0/en/update.html for more info
There are number of ways to update a record safely. Conside the following pseudo code + php program.
class Example
{
function UpdateRecord($editedRecord)
{
//Fetch existing record from the database
$originalRecord=DB::fetchExample($editedRecord->getId())
//validate each edited field and it is valid then assign its value
//to the originalRecord's field
if(IsValid($editedRecord->getName())
{
$originalRecord->setName($editedRecord->getName());
}
.....
//update the record.
$originalRecord->Update();
}
}
Just add some sql to it:
$sql = "UPDATE example SET col_1 = val_1, col_9 = val_9 WHERE col_7 = val_7";
mysql_query($sql);
Then replace the columns and values with you stuff. For further info: PHP MySql Update