Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Having trouble with first attempt at interacting with MySQL using PHP

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading