So, I'm new to PHP. I spent a lot of time today teaching myself in the ways of inserting data into SQL tables via html forms with PHP and MySQL, and retrieving said data so it can be displayed on a web page. I felt proud of myself when I had finally coded a working, though very simple system for this. Now I find out that there is newer, more secure API available and that 'mysql' functions will no longer be supported... which my noob code is full of. I've been trying to figure out how to update my code with the new API (MySQLi) for about 3 hours but everything is ending in failure, I'm just not competent enough in PHP or SQL to understand how the changes represent what was already there and working. I did manage to get a connection using the new functions, but I could not display anything like with the old ones.
I will post the code here, and hopefully someone can help me out in figuring this out because I'm just not getting it.
Here is the code that is calling the information from the database, and displaying it in simple rows.
<?php
$host="HOST";
$username="USRNAME";
$password="PASS";
$db_name="USRNAME";
$tbl_name="TABLE";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<table width="800" border="1" cellspacing="0" cellpadding="3" align="center">
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td width="75%"><? echo $rows['gamename']; ?></td>
<td width="25%"><? echo $rows['region']; ?></td>
<td width="20%"><? echo $rows['mode']; ?></td>
<td width="75%"><? echo $rows['notes']; ?></td>
</tr>
<?php
}
?>
<?php
mysql_close();
?>
Here is the code for the insert action on form submit.
<?php
$host="HOST";
$username="USRNAME";
$password="PASS";
$db_name="USRNAME";
$tbl_name="TABLE";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$gamename=$_POST['gamename'];
$region=$_POST['region'];
if(isset($_POST['mode'])) {
$mode = implode(",", $_POST['mode']);
} else {
$mode = "";
}
$notes=$_POST['notes'];
$sql="INSERT INTO $tbl_name(gamename, region, mode, notes)VALUES('$gamename', '$region', '$mode', '$notes')";
$result=mysql_query($sql);
$gamename = mysql_escape_string($_POST['gamename']);
$region = mysql_escape_string($_POST['region']);
$notes = mysql_escape_string($_POST['notes']);
if($result){
echo "Thank you for your submission.";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysql_close();
?>