PHP SQL Database for saving scores - php

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

Related

How can I insert into a table both a select max function from another table and variables input by the user, using PHP, SQL and Microsoft Access?

I want to insert a record into the Booking_Sheet table when the user enters their information on an HTML document. First, some of the information is stored in the Client_Database table, and then I want the rest of the information to be stored in the Booking_Sheet but I want the record on the Booking_Sheet to contain information that was just inserted, such as the AutoNumber Client_ID from Client_Database, which I am doing with a select max statement. Although this is not working. I was wondering if it is possible to have a SELECT statement imbedded within VALUES?
$sql="INSERT INTO Booking_Sheet (Client_ID, Client_Name)
VALUES ((SELECT MAX(ID) FROM Client_Database), '$name');";
if(odbc_exec($con,$sql))
{
echo "<br>Data saved<br>Please Wait Page redirect..";
header("refresh:1;url=index.php");
}
else
{
echo "Error";
}
Apparently not. I just tried within Access and it fails. However, don't need VALUES.
"INSERT INTO Booking_Sheet (Client_ID, Client_Name) SELECT MAX(ID), '$name' FROM Client_Database";

How can i insert data in table with a join sql?

i have 2 tables on db.
First table:
users
id|name|mail|password|group
second table
scores
id|name|score
The idea is get the name from users using the id (this id already know because i get that by php), then insert a score in table scores using the name obtained by a id.
I suppose that can i do with a inner join between users and scores.
How can i do that?
Insert into scores(id, name, score)
select ID, name, score(that you can pass)
from users
where id = (parameter pass by PHP)
Agree. You can simply use this:
INSERT INTO scores values (null, (select name from users where users.id= 1),100);
Replace the score and id with the values you get.
Assume you have the auto increment for your ids.

Score not calculating correctly

I'm using the code below to calculate the total score for my teams :
My tables are also listed bellow .
The problem is that the scores are being incorrectly calculated for every team except the first one . The image should explain the desired outcome based on the info in my database.
Query :
mysqli_query($connection, "UPDATE teams SET totalscore=overall_score+IFNULL((SELECT sum(overall_user_score) FROM users WHERE team_id=id),0)") or die(mysqli_error($connection));
Tables :
users(name, team_id, overall_user_score)
teams(id, name, team_score, totalscore)
Image:
I'm not sure why you have overall_score and totalscore as two different columns, but anyways:
If you are summing the totalscore column based on the overall_user_score column from the users table, you don't need to keep adding overall_score (in the teams table) to the totalscore column. See here as an example.
Again, I'm not sure why you have both overall_score and totalscore as two different columns. If I'm misunderstanding the question, let me know.
Really stupidly left out the t. infront of the last id so query should be :
mysqli_query($connection, "UPDATE teams t SET totalscore=overall_score+IFNULL((SELECT sum(overall_user_score) FROM users WHERE team_id=t.id),0)") or die(mysqli_error($connection));

PHP MySql Insert columns form one table to another

I want to insert my table data from copying another table with some new given data.
I use this query
$sql = "INSERT INTO table2(name, city, email,money)
hasib,SELECT table1.city, table1.email,
newsletter_subscribers.email
FROM table1 WHERE name='jesy',100";
But its not Work
You must give all columns inside the select statement columns, even constant values, e.g.
insert into table2(name, city, email, money)
select 'hasib', city, email, 100
from table1
where name = 'jesy'
If you want to take values from multiple tables, as your select statement suggests, you must look into joins.

Insert the data from the data select in other table in PDO and MYSQL

There are two table
user:
id
uid
board:
id
message
user_id
The problem is , I got the uid now , and I would like to insert data to board, but I want to simplify all things to one query, so I tried
"INSERT INTO board (message,user_id) VALUES (:message, SELECT id FROM users WHERE uid=:uid)";
but in this way is not working, would anyone kindly teach me the right syntax? Thanks
Enclose SELECT in parentheses like this
"INSERT INTO board (message,user_id) VALUES (:message, (SELECT id FROM users WHERE uid=:uid))";
You either use a VALUES clause or a SELECT clause, not both.
INSERT INTO board (message, user_id)
SELECT :message, id
FROM users
WHERE uid = :uid

Categories