StackOverflow professionals. I've run into a bit of a snag. I'm just trying to create a simple add site function for a directory of mine, and I was able to create the query, run my program, and have a column added to my table no problem. But then I tried adding a condition which would tell me whether or not my record added successfully or not. When I did, I ran it and my if statement ran "else" meaning it didn't add my record, and from there I couldn't add anymore records. I've since removed that condition but to no avail. No record gets added anymore whenever I run my page and add all the stuff to the text fields.
Here's my code...
<html>
<body>
<?php
$user_name = "root";
$password = "";
$database = "mydirectory"; // name of database
$server = "127.0.0.1";
$Sitetbl = "tbl_mydirectory";
//define the variables sent from the form
//if form has been posted, do this
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$genre = trim($_POST["genre"]);
$site = trim($_POST["siteadd"]);
$name = trim($_POST["nameadd"]);
//connecting to database
$db_handle = mysql_connect($server, $user_name, $password);
//creating a query to add site
$addSQLQuery = "INSERT INTO $Sitetbl (Genre,Site_Address, Site Title)
VALUES ('$genre','$site','$name')";
$result=mysql_query($addSQLQuery,$db_handle);
}
?>
<!-- send page to itself using server variable -->
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<h2>Add Site</h2>
Genre:<input type="text" name="genre"></br>
Http://<input type="text" name="siteadd"><br>
Name:<input type="text" name="nameadd"></br>
<input type="submit" value="Add">
</form>
</body>
</html>
and the if statement (that's no longer in my code) was this
if($result)
{
echo "Website added successfully!";
}
else
{
echo "Error adding website. Please check fields.";
}
Which was at the bottom of my program, right above where the PHP declarations close.
$addSQLQuery = "INSERT INTO $Sitetbl (Genre,Site_Address, Site Title)You have a space in column name, try$addSQLQuery = "INSERT INTO $Sitetbl (Genre,Site_Address,Site Title)mysql_function is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.