Is there a way to get a UserId in and sql table called Accounts based on a UserEmail also in Accounts? In php i have a page where people will login with a UserEmail from an Accounts table. this is all fine, but they need to write a review that is stored in a Reviews table and im having trouble pulling the UserId and putting it into the Review table.
Yes, many ways; a very simple one is to just use an inline SELECT statement in the INSERT query. A very rough example, assuming you're using something like PDO, might look like:
$pdo = new PDO(...);
$stmt = $pdo->prepare("insert into review(score, user_id) values(?, select user_id from accounts where useremail = ?)");
$stmt->execute($score, $useremail);
Related
I am wondering if it is possible to insert from another table (which I have managed to do) whilst also inserting a VALUE of a variable from the current php file?
I am aiming to get the user ID from another table, which I have gotten from selecting the email from the user input. I then need to insert a hash which is automatically created via a variable.
This is my current code that gets the correct id from the users table.
$forgot = $pdo->prepare("
INSERT INTO
forgot (
user_id
) SELECT
id
FROM
users
WHERE
email = :email
");
Now I just need to insert the VALUE of :hash too.
Would this need to be done with a separate query?
Thanks.
Try the following:
INSERT INTO
forgot (
user_id, hash
) SELECT
id, :hash
FROM
users
WHERE
email = :email
I am looking for a way to write 2 MySQL queries into 1 PHP variable since I am looking to insert into 2 different tables.
I am building a simple registry page where the name is in table "users" and email and password is in table "auth".
Tables as follows:
table users
table auth
so I thought something like this:
$sql = "INSERT INTO users VALUES('', '$name','','') INSERT INTO auth VALUES('', '', '$email', $password', '')";
since I am using
$result = mysql_query($sql);
I would need to have the query in one variable ($sql) as far as I understand.
Thanks.
If the two tables are related, a straightforward insert will put the data in no problem. Then just get your data into a $result variable.
SELECT username, auth
FROM users
INNER JOIN auth
ON users.id=auth.id;
Just add ; to sql query to execute double query
$sql = "INSERT INTO users VALUES('', 'name');INSERT INTO auth
VALUES('', '', '$email', $password', '')";
Hello all,
I am working on a site were a member can send message to all of his/her friends ..
so all of their friends are stored in an array like:
$selectfrnds=mysql_query("select sender_id,receiver_id from fk_friends where (sender_id='$id' or receiver_id='$id') and friendtofriend='freq' ");
$friendis=array();
I have imploded the query into a variable and now all their friends are represented:
$frndsall='4','8','2','12','13','14','15','16','18','19','21','23','24','27','35','36','40','43','29','45','44','38','46','22','1'
so I just want to ask: How do I insert query that will send a message to all these ids?
I want something like:
insert into tablename (message,id// id of friend ) values ('$message//this is same','$xyz// which is stored into varialbe $frndsall one by one it should insert with all the values')
any help please...
Use Prepared Statements in a foreach loop:
$stmt = $pdo->prepare(
"INSERT INTO tablename (message, id) VALUES (:message, :id)");
$stmt->bindParam(':message', $message);
$stmt->bindParam(':id', $id);
foreach ($frndsall as $frndId) {
$id = $frndId;
$stmt->execute();
}
check out php foreach statements (http://www.php.net/manual/en/control-structures.foreach.php) - that should be what you're looking for.
It's possible to do bulk insert by first preparing a select statement that produces the desired result set to be inserted, then prefixing INSERT into .
Example
INSERT INTO tablename (message, id)
SELECT '$message', receiver_id
FROM fk_friends
WHERE (sender_id='$id' or receiver_id='$id') AND friendtofriend='freq'
If your database abstraction layer supports prepared statements, something like this is probably the best way to do this query.
$db->query($db->prepare('
INSERT INTO tablename (message, id)
SELECT ?, receiver_id
FROM fk_friends
WHERE (sender_id=? or receiver_id=?) AND friendtofriend=?
'), array($message, $id, $id, 'freq')));
Otherwise, if not using prepared statements, be sure to properly escape all data you insert into the query before querying it from the database, using the relevant escaping function for your method of accessing the database.
Hope that helps.
You can do Insert in foreach, but this solution will bad on real project, where user will have more than 3k friends.
Here is a question of database architecture in my mind.
You say:
member can send message to all of his/her friends
If you want to add possibility to send a message only to all, will be better to use separate table like
`mass_messages`
id (int 11)
sender_id (int 11)
message (text)
send_date (timestamp)
Also you can create another table to store friends who already read your message.
But this way not useful if you try to send message to all except for a few friends.
I am totally new to PHP and I'm trying to create a registration page. To register, a user has to create a username, password, email, which are put into a table called users.
They also enter address details which is put into a table called customer_info.
In both tables I have created an auto increment, primary key called 'user_id'.
When the form is completed it fills out and enters the data, but the data is not banded and so there are two user_id, one in users and one in customer_info.
First I create values (from the post) that have been entered and assign them to variables. Then I put the variables into my table using the following query:
$result = mysql_query(
"INSERT INTO `users`(username, password, email) VALUES ('$value1', '$value2','$value3')"
);
and
$result = mysql_query(
"INSERT INTO `customer_info`(firstname, lastname, b_add_num, b_add_road, b_add_town, b_add_pc, p_add_num, p_add_road, p_add_town, p_add_pc) VALUES ('$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12','$value13')"
);
How would I set it so that it creates only one user id for both tables (making a connection between the sets of data)?
Is there something missing in my query, that should connect the tables?
Before anything, you should not use mysql_* extension anymore. Go towards PDO or mysqli
Your technique generates two different unique ids. The point is to have only one, so that it can be unique, and link information on that unique id.
The users table is the one with that unique id, user_id, which is your auto_increment column. The customer_info table can also have a info_id unique column, but must contain a user_id column, which will contain the user's user_id, linking the rows together.
It would also be a great moment to add a foreign key to your tables so that integrity of the data won't be compromised.
so after this query:
$result = mysql_query(
"INSERT INTO `users`(username, password, email) VALUES ('$value1', '$value2','$value3')"
);
get the insert id:
$id = mysql_insert_id();
then run your other query with it:
$result = mysql_query(
"INSERT INTO `customer_info`(user_id,firstname, lastname, b_add_num, b_add_road, b_add_town, b_add_pc, p_add_num, p_add_road, p_add_town, p_add_pc) VALUES ('$id','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12','$value13')"
);
I would configure USER_ID as an AUTO_INCREMENT column only in Users table, insert the data into Users table first, then get the ID of the user inserted, using the mysql_insert_id ($connection_id); and use that while inserting data into Customer_Info table. This way, you can leverage the ID generation (sequence) feature of MySQL as well.
I am a total newbie for php and mysql. Now I want to create a simple website for practice. I have created two tables named user_info and acct_info.
The primary key for user_info is the user_id. There is another user_id in acct_info as well.
I have relate these two tables by linking the user_id (referenced key is the user_id in user_info, that of acct_info is foreign key). However, when new user register new account on my website, new data will be added into the user_info table, but nothing happened in acct_info table.
Could anyone provide me some advice?
Sorry for not stating the problem clearly.
I have created a new user, however, data is added into user_info table but nothing in acct_info table. I thought if I build relationship between these two tables, for example, I create new user, then data like user_id and all those default data should be also added into acct_info table. I wrote php codes in my registration page to add user information into user_info table but I didn't do it to add data into acct_table. If I need to write codes for acct_table, what is the purpose to relate these two tables?
Sorry for asking dummy questions... :)
This is the php code for the data adding part:
if($reachByPOST && $okay == true && $NotExistingUsername == true && $NotExistingEmail == true) {
$sql = "INSERT INTO user_info SET user_id = '',user_name = '".$_POST['username']."',password = '".$_POST['password']."',email = '".$_POST['email']."'";
$res = mysql_query($sql);
$_SESSION['user_id'] = $userid;
$sql2 = "INSERT INTO acct_info SET user_id = '".$userid."'";
$res2 = mysql_query($sql2);
}
However, I got error saying $userid is undifiend...I just have no idea on how to add data into acct_info table...By the way the user_id is auto_increment in user_info table, it is generated automatically, not post input by user.
if you insert in user_info, it has no affect to acct_info.
But if you insert into acct_info, there should exist user_id in user_info(pri key), which will be inserted in acct_info.
Try to insert random data in acct_info.... If you don't have right user_id, you should get error.