I am fairly new to PHP/SQL coding and I am completely stumped as to why this coding isn't working. The database itself was made by my University, and so I am just trying to connect to it (I'm not sure if I'm able to give away the details so I took the connect coding out. I am at a basic level so heavy technical language will go over my head but any advice will be greatly appreciated!
I am trying to insert the results from a form which is linked to the PHP file into the database table. I am unsure if I need to put anything in the PHP file to state this? But this is my code:
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
// Connect to database
$sql = "SELECT runnerid, position, eventid, date, finishtime, categoryid, agegrade, pb FROM Results";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
{
echo "<table>";
echo '<table border=1><tr><th>Runner ID</th><th>Position</th><th>Event ID</th><th>Date</th><th>Finish Time</th><th>Category ID</th><th>Age Grade</th><th>Personal Best</th></tr>';
echo "<tr><td>";
echo $row['runnerid'];
echo "</td><td>";
echo $row['position'];
echo "</td><td>";
echo $row['eventid'];
echo "</td><td>";
echo $row['date'];
echo "</td><td>";
echo $row['finishtime'];
echo "</td><td>";
echo $row['categoryid'];
echo "</td><td>";
echo $row['agegrade'];
echo "</td><td>";
echo $row['pb'];
echo "</td>
</tr>";
}
echo "</table>";
}
} else {
echo "0 results";
}
$sql = "INSERT INTO Results VALUES ('$_POST[runnerid]', '$_POST[position]' '$_POST[eventid]' '$_POST[date]' '$_POST[finishtime]' '$_POST[categoryid]' '$_POST[agegrade]' '$_POST[pb]')";
$result = $conn->query($sql);
if (!$result) {
die('Could not insert data' . mysql_error());
}
$conn->close();
?>
I have even tried the code without the $_POST
coding in to just add new data but that's not working either.