Bind textfield value to array and put in database - php

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!!

Related

How to insert data to another table by id

I want to complete user sign up.
firstly user will write username and password, table called users, I have id as primary key.
secondly user will write address, phone number and zipcode, table called userdata, I have userid as index(key).
I have did a relation between id from table user and userid from table userdata.
So now I want to insert data from php code I did two forms one for user and it has two input for username and password it works well and data inserted.
in the second form I have select option has id from table users and it works well.
then in the same form I have three inputs phone number, address and zipcode.
so the question is how I can insert the data to userdata by the same to the same id. so userid in table user will be the same id in table userdata.
I need sql code.
I used that :
"INSERT INTO userdata(address, phone, zipcode) VALUE (:address, :phone, :zip) SELECT 'id' FROM 'users';"
First :
$q = "NSERT INTO users(username, password) VALUE ('Adam', '12345678')";
$r = $connect->query($q);
//get the last id u inserted
$last_id = mysqli_insert_id($connect);
Second :
$q = "NSERT INTO userdata(address, phone, zipcode,user_id) VALUE ('California', '12345678', '1111','$last_id')";
$r = $connect->query($q);
and if you want to make the (id) not the (userid) the same just :
$q = "NSERT INTO userdata(id, address, phone, zipcode) VALUE ('$last_id','California', '12345678', '1111')";
$r = $connect->query($q);

php code for uploading/changing Image for an existed user from android after logging in

My table into the databases has 5 columns: id, username, email, password, image_path. id, username, email and password columns are filled while user registering but iamge_name column must be filled after logging in by uploading a picture from gallery. Also i need that user can change his/her picture.
for example the table is like this:
id------username------email------password------image_path
1----------alex--------a#g.com-----123456-------
2----------alex2------a#g2.com----123456-------
now i need to insert an image for alex2. please help me first to find where alex2 there is and then insert the picture to the image_path column of id=2(where username is alex2). So please help me to complete this code:
public function insertImageByUsername($username){
//here i want to find row related to alex2
$stmt = $this->con->prepare("SELECT id FROM my_table WHERE username = ?");
//here i want to insert image to the image_path column of alex2 related row
$stmt2 = $this->con->prepare("INSERT INTO `my_table` (`image_path`) VALUES ( ?);");
}
thanks a lot for any help.

INSERT Multiple Database Values To Specific Rows

How can I INSERT the values of this function to only WHERE the value of the username column (located in the login table, contains the exact same value as the value of the column nickname (located in a table called active_users).
$stmt = $this->pdo->prepare('INSERT INTO login (device_token) VALUES (?)');
$stmt->execute(array($token));
Additional info: when a user logs in, I set up the API to add the user's name to both username (in the login table) and nickname in the active_users table. Since these values will always be the same for their respective tables, I was hoping we could add some conditional logic to achieve this type of specificity. My goal is to take the device_token parameter and pass it to a different spot in the database.
Edit: Schema
Update AFNetworking command for login
NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:command, #"command", _nicknameTextField.text = fldUsername.text, #"username", hashedPassword, #"password", nil ];
Update: active_users INSERT statment
$stmt = $this->pdo->prepare('INSERT INTO active_users (user_Id, device_token, nickname, secret_code, ip_address) VALUES (?, ?, ?, ?, ?)');
$stmt->execute(array($userId, $token, $name, $code, $_SERVER['REMOTE_ADDR']));
If I understand you correctly. What you really want is an update and not an insert.
You want to add the value of device_token to the row with nickname, correct?
You would do that with an update:
UPDATE login
SET device_token = #token
WHERE username = #nickname
If you're trying to update the record with an existing record from active_users table than:
UPDATE login AS L
JOIN active_users AU ON L.username = AU.nickname
SET L.device_token = AU.device_token
Though, looking at your tables. I'm wondering if you're repeating too much of the data. It seems like you can link login and active_users by user_id. Well, this would get off topic, so I'll just suggest double checking db design concepts and your needs.
Using PDO it should be
UPDATE login
SET device_token = :token
WHERE username = :nickname
where the params array that will be bound looks like
$params = array("token" => $token, "nickname" => $nickname);
Full example:
$stmt = $this->pdo->prepare('UPDATE login SET device_token=:token WHERE nickname=:nickname');
$stmt->execute(array("token" => $token, "nickname" => $nickname));
Or you use the method of binding with ?
UPDATE login
SET device_token = ?
WHERE username = ?
where the parameter array is
$params = array($token, $nickname);
Full example:
$stmt = $this->pdo->prepare('UPDATE login SET device_token=? WHERE nickname=?');
$stmt->execute(array($token, $nickname));

Get data from other column details

My table looks like this:
check_in_id | amount | date | user_id (foreign key of user table )
In the interface it asks user to enter amount, date, username, password. When running the insert into query, how do I get user_id from that user table to insert into this table?
I suppose you have one main table (say: tbl_login ) to store username, password, user_id (primary key).
So, You first retrieve user_id with username and password like:
SELECT user_id FROM tbl_login WHERE username = '$username' AND password = 'password';
Where $username and $password is your entered username and password from interface.
So after getting user_id , now you have all needed values, so you can easily insert these in your given table.
Since the information in you query is incomplete, I will try to give an answer with certain assumptions.
check_in_id: an auto increment id in
You want to insert into table only if username and password matches.
The variables with '$' are the user inputs
insert into <your table>
(check_in_id, amount, date, user_id)
select null, $amount, $date, user_id from user
where username = $username and password = $password

Adding variable to database without replacing current value?

I'm trying to do a MySQL Query that adds information to a database in a format that doesn't overwrite it's current value, but instead appends it as in the friends column would have friend1, friend 2, friend 3 ... etc.
First I am unsure if INSERT INTO is right:
$username = $_SESSION['username'];
mysql_query("INSERT INTO members (friends) WHERE username = $username
VALUES ('$friendtoadd')");
What I want to do is have this do two things
1) Add the friend in a format mentioned so that it can later be called out that if the $username set in session is active then the results posted on the page are only from those other users contained in their friends column.
2) If the $friendtoadd already exists in their column for friends then it does nothing.
UPDATE members SET friends = CONCAT_WS(', ', friends, '$friendtoadd') where username = '$username'
I hope $friendtoadd as well as $username are sanitized through mysql_real_escape_string
Also, this design isn't very good. You should have a seperate friends table
so
table friend
id (auto increment), userID, name)
then do
Insert into friend SET userID = $userID, name = '$friendName'

Categories