Jump to content

Error saving data in mysql table


vivis933

Recommended Posts

Hi i made this code but when i run there is no data in the table


<?php
include("conf.php");
$file_name = basename(__FILE__,'.php');
$table = $file_name;
$query = "SELECT HomeTeam FROM " . $table;
$result = mysqli_query($conn, $query);

if(empty($resultat)) {
echo "<p>" . $table . " table does not exist</p>";
$query = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS $file_name (
HomeTeam int NOT NULL PRIMARY KEY,
HomeGoals varchar(255) NOT NULL
)CHARACTER SET utf8 COLLATE utf8_general_ci");
}
else {
echo "<p>" . $table . "table exists</p>";
} // else

$sql = "INSERT INTO $file_name (HomeTeam, HomeGoals) VALUES (
SELECT HomeTeam, COUNT(HomeGoals) As HomeGoals FROM GetHistoricMatchesByLeagueAndDateInterval GROUP BY HomeTeam ORDER BY HomeTeam ASC
)";
?>

 

Link to comment
Share on other sites

+------------+------------+
| HomeTeam   | HomeGoals  |
+------------+------------+
| Team1      |          2 |
+------------+------------+
| Team3      |          5 |
+------------+------------+
| Team1      |          2 |
+------------+------------+
| Team2      |          3 |
+------------+------------+
| Team2      |          2 |
+------------+------------+
| Team3      |          2 |
+------------+------------+
| Team1      |          2 |
+------------+------------+

I need the following results

Number of times the team is on column

Team1 = 3 times

Team2 = 2 times

Team3 = 2 times

And the total number of goals the time has

Team1 = 6 goals

Team2 = 5 goals

Team3 = 7 goals

And inserting it in new table

Link to comment
Share on other sites

Your data shows team as character field, yet you create a table with team name as INT and goals as varchar(). As this table contains only one field for goals, and you say you want both a count and a total, which goes in the new table (count or total).

 

And why are you doing this at all? You can get these by query any time, you should not store data derived from the data in databases.

Link to comment
Share on other sites

<?php
include("conf.php");
$file_name = basename(__FILE__,'.php');
$table = $file_name;
$query = "SELECT HomeTeam FROM " . $table;
$result = mysqli_query($conn, $query);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

if (!mysqli_query($conn, "SET a=1")) {
printf("Errormessage: %s\n", mysqli_error($conn));
}
if(empty($result)) {
echo "<p>" . $table . " table does not exist</p>";
$query = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS $file_name (
HomeTeam int NOT NULL PRIMARY KEY,
HomeGoals varchar(255) NOT NULL
)CHARACTER SET utf8 COLLATE utf8_general_ci");
}
else {
echo "<p>" . $table . "table exists</p>";
} // else

$sql = "INSERT INTO $file_name (HomeTeam, HomeGoals) VALUES (
SELECT HomeTeam, COUNT(HomeGoals) As HomeGoals FROM GetHistoricMatchesByLeagueAndDateInterval GROUP BY HomeTeam ORDER BY HomeTeam ASC
)";
?>
Errormessage: Unknown system variable 'a'
<p>fundtable exists</p>
Process finished with exit code 0

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.