First attempt at php and MySQL.
I set up a Ubuntu 22 VM with APache, PHP, and MySQL.
Apache is working fine serving pages, PHP is working as expected, and I can interact with MySQL from the command line so I think that all that is working fine.
Trying to add a new record to a table using the following:
$StudentNo = $_POST["StudentNo"];
$Fname = $_POST["fname"];
$Lname = $_POST["lname"];
$Phone = $_POST["phone"];
$EMail = $_POST["email"];
$Balance = $_POST["balance"];
$Comments = $_POST["comments"];
$SQLCommand = "INSERT INTO Students (StudentNo, Fname, LName, Phone, email, Balance) Values (".$StudentNo.",'".$Fname."','".$Lname."','".$Phone."','".$EMail."',".$Balance.");";
echo $SQLCommand."<br>";
$link = mysqli_connect($hostname, $username, $password, $database);
$result = mysqli_query($link,$SQLCommand)
mysqli_close($link);
Everything seems to work fine until the $result=…. I say that because if I comment out that line and the line following other echo lines after that are displayed. I am pretty sure that the SQL command is good because if I copy the text displayed by the "echo $SQLCommand" directly into the MySQL command line the data is put into the table.
>Solution :
<?php
$StudentNo = $_POST["StudentNo"];
$Fname = $_POST["fname"];
$Lname = $_POST["lname"];
$Phone = $_POST["phone"];
$EMail = $_POST["email"];
$Balance = $_POST["balance"];
$Comments = $_POST["comments"];
$SQLCommand = "INSERT INTO Students (StudentNo, Fname, LName, Phone, email, Balance) Values (".$StudentNo.",'".$Fname."','".$Lname."','".$Phone."','".$EMail."',".$Balance.");";
echo $SQLCommand."<br>";
$link = mysqli_connect($hostname, $username, $password, $database);
$result = mysqli_query($link,$SQLCommand);
mysqli_close($link);