Registration Page: Writing 2 MySQL queries into 1 PHP variable - php

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', '')";

Related

Multiple Insert in different tables with insert new AI

I have a PHP-script in which I can register news users.
Of course I want to insert things like username, password etc. (these values come from the user) but on the other side I want to insert his user_id (new user!) into another table.
My SQL
$sql = "
BEGIN;
INSERT INTO users (username, password, auth_lvl, realname, usercolor) VALUES ('$username', '$password', '$auth_lvl', '$realname', '$usercolor');
INSERT INTO users_startmodules (user_id, startmodule, enabled) VALUES ('(SELECT MAX(user_id)+1 FROM users)', 'newsmodule', '1');
COMMIT;
";
How can I solve this problem and is BEGIN; ... COMIT; the right choice when I want to get the query canceled if just one thing didn't work, because I don't want to have just a few entries in the worst case.
I suggest to change the type of user_id in users table to auto_increment with something like this:
alter table users alter column user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
Now, Assuming that you have escaped the user input data:
$query="INSERT INTO users (username, password, auth_lvl, realname, usercolor) VALUES ('$username', '$password', '$auth_lvl', '$realname', '$usercolor');"
and run that:
$mysqli->query($query);
then get the userid with mysql_insert_id() function of MySQL or alternatively in PHP:
$userid=$mysqli->insert_id;
and:
$query="INSERT INTO users_startmodules (user_id, startmodule, enabled) VALUES ('$userid', 'newsmodule', '1');";
$mysqli->query($query);
This is too long for a comment.
To get the id that was just inserted, use last_insert_id(). This function is documented here.
Next, the answer to your question is to start a transaction. I would give you the syntax, but it is better for you to read the documentation on transactions before you start using them.
The key idea is that a transaction groups data modifications together. Either all take place or none do. Along the way, you can use commit or rollback, depending on whether or not you want to keep the changes or keep none of the changes.

how to get userid from $_session username

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);

Insert two table rows, then join in relational table by ID

I have a form that takes two names and two email addresses, then a single password.
I am looking to write multiple queries so that the names and emails get inserted in to one table, then the auto-incrementing IDs generated by the database for both of those get inserted into the second table along with the password.
I run one insert query, grab last_insert_id, then run the other and do the same.
As I'm using last_insert_id to grab the auto-incrementing values, is there any way to store both of these as a variable, so that I can easily insert both their relevant IDs into the second table at the same time? Once I use last_insert_id a second time, it seems to lose the first value.
Pointers on methods to use, or a general direction in which to head, would be much appreciated. I like figuring out the exact code to use on my own.
EDIT:
These are my queries, which work fine except the UIDs created are 1 and 2, but inserted in to the second table are IDs 0 and 1. Any idea why?
$your_query = "INSERT INTO MEMBERS (NAME, EMAIL) VALUES ('$your_name', '$your_email')";
$your_id = (mysql_insert_id());
mysql_query($your_query);
$partner_query = "INSERT INTO MEMBERS (NAME, EMAIL) VALUES ('$partner_name', '$partner_email')";
$partner_id = (mysql_insert_id());
mysql_query($partner_query);
$couples_query = "INSERT INTO COUPLES (UID_1, UID_2, PASSWORD, SALT) VALUES ('$your_id', '$partner_id', '$password', '$salt')";
mysql_query($couples_query);
Turns out I had some extra brackets around each mysql_insert_id, and it should have been run after the mysql_query.
$your_query = "INSERT INTO MEMBERS (NAME, EMAIL) VALUES ('$your_name', '$your_email')";
mysql_query($your_query);
$your_id = mysql_insert_id();
$partner_query = "INSERT INTO MEMBERS (NAME, EMAIL) VALUES ('$partner_name', '$partner_email')";
mysql_query($partner_query);
$partner_id = mysql_insert_id();
$couples_query = "INSERT INTO COUPLES (UID_1, UID_2, PASSWORD, SALT) VALUES ('$your_id', '$partner_id', '$password', '$salt')";
mysql_query($couples_query);

PHP Getting Previous MySQL Query's ID For Next Queries, How?

I have a simple question about MySQL and PHP here. Let's say I have this PHP syntax :
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin',35)");
on that Person table, there's a column named ID (Auto Increment). how to get Peter Griffin's ID after INSERT process is done without making another SELECT query?
or is it possible to do INSERT for 2 tables using single query? for example I want to INSERT Peter's address as well on Address table :
mysql_query("INSERT INTO Address (City, State, Zip)
VALUES ('Cupertino', 'California', 35212)");
that's all..
$new_id = mysql_insert_id();
Put that right after your INSERT query and it will give you the id.
I would't recommend trying to do two INSERTs in one, and the mysql_insert_id() method will make it simplest for you and your code.

PHP SQL Database for saving scores

I've got a database setup for scores, each score is out of 100 and i need to be able to save more than one value and output it so that a user can see it. I have no idea however how to save the scores; my knowledge of php is limited and i only know how to save one value.
Thanks in advance
A good practice is in this case is to have 2 tables: one for names, one for scores. Then associate them with a joining table. This way your database is "normalized" and won't have repeating data whenever "John" enters his score of "95" more than once. Your two tables will need an ID field (ID, Name) and (ID, Score), with a third table called something like "Names_Scores" (Name_ID, Score_ID). Then use joining queries to match up what Name ID has what Score ID, and list them for the user when they log in.
With PHP, you need to make a connection to the database, then initiate a SQL (or MySQL) call to the data you want and place the retrieved data into a variable/array you can use elsewhere, then close the connection.
Example:
<?php
$con = mysql_connect("[hostname]", "[username]", "[password]");
mysql_select_db("db_name") or die(mysql_error());
$result = mysql_query("SELECT Users.UID, Users.Name, Scores.Score FROM Users, Scores WHERE Users.UID = Scores.UID GROUP BY Users.UID, Scores.Score") or die(mysql_error());
echo $result;
mysql_close($con);
?>
Based on your comments I think you could get by with something like this:
mysql_query("INSERT INTO scores(name, score) VALUES ('John', 100),('John', 75),('Bob', 68)");
Or
mysql_query("INSERT INTO scores(name, score) VALUES ('John', 100)");
mysql_query("INSERT INTO scores(name, score) VALUES ('John', 75)");
mysql_query("INSERT INTO scores(name, score) VALUES ('Bob', 68)");
If you wanted a specific users score you could do:
mysql_query("SELECT score FROM scores WHERE name='John'");
If you wanted a list of users:
mysql_query("SELECT name FROM scores GROUP BY name");

Categories