config.php is the global file which has the connection codes.
my UPDATED Insert.php code is:
<?php
include("config.php");
if ($submit) {
if (isset($_POST[firstname]) && isset($_POST[lastname]) && isset($_POST[age])) {
$sql = "INSERT INTO Persons (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
mysql_query($sql, $connect);
echo "1 record added";
}
else {
// do nothing now
}
}
else {
?>
<form action="<?php echo $PHP_SELF ?>" method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
Age: <input type="text" name="age">
<input type="submit">
</form>
<?php
}
//if (!mysql_query($sql,$connect))
//{
//die('Error: ' . mysql_error());
//}
mysql_close($connect);
?>
my View.php code is:
<?php
include("config.php");
$sql = mysql_query("SELECT * FROM Persons");
while($results = mysql_fetch_array($sql)) {
echo $results['FirstName'] . ', ' . $results['LastName'] . ', ' . $results['Age'] . '<br/>';
}
if (!mysql_query($sql,$connect))
{
die('Error: ' . mysql_error());
}
mysql_close($connect);
?>
my Config.php code is:
<?php
$dbhost="MYWEBHOST";
$dbusername="MYUSERNAME";
$dbpassword="MYPASSWORD";
$dbname="MYDATABASE";
$connect = mysql_connect($dbhost, $dbusername, $dbpassword);
mysql_select_db($dbname,$connect) or die ("Could not connect to database");
?>
After the Insert.php is executed and I receive the "1 Record Added message" and when I go to View.php, I get the following error: "Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #4' at line 1"
What did I do wrong?
Also How can I prevent user from refreshing the Insert.php page which keeps adding null values to the table?
insert.php? I'm saying this because I see you commented the query execution in this file. It might be that your connection is invalid and when you pass$connect, it failsecho "1 record added";happens after you setup the string $sql, but you don't actually execute it...