0

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.

7
  • check for errors $result=mysql_query($addSQLQuery,$db_handle) or die(mysql_error()); Commented Dec 21, 2013 at 5:28
  • check for errors and tell us what error you are getting. Commented Dec 21, 2013 at 5:29
  • $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) Commented Dec 21, 2013 at 5:30
  • 1
    Warning: 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. Commented Dec 21, 2013 at 5:30
  • @MohammadYaseen that wont make any difference. :) Commented Dec 21, 2013 at 5:35

2 Answers 2

3

You must try this modified code. As you have missed out "mysql_select_db" line.

<?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) or die('Host Connection Error');

 mysql_select_db($database,$db_handle) or die('DB Connection Error');

 //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) or die('Query Error');
 }  
 ?>

 <!-- 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>
Sign up to request clarification or add additional context in comments.

7 Comments

Hey, Mehul. I tried running your version of the code and I get back "Query Error"?
Can you tell me the exact column names of the db? so that i can make you a new query.
OK, it's "ID" (which's auto-incremented), Genre, Site_Address, and Site Title.
I think there is some problem with the column names as you have mentioned the last column "Site Title". A Whitespace cannot be included in column name. You must try renaming the column names to id(auto incremented), genre, site_address, site_title and then use the below query: $addSQLQuery = "INSERT INTO ".$Sitetbl."(genre,site_address, site_title) VALUES ('".$genre."','".$site."','".$name."')";
Hello, Mehul. The revision won't be needed. I ended just adding a few things of your code and changing "Site Title" to "Site_Title" which seemed to have solved my issue. If you wish, you can still add your version. it may help me with issues further down the line. Thank you very much!
|
-1

To begin, you should not be using the mysql extension, you should be using the new mysqli(MySQL Improved) extension that comes on new installations of PHP. The following line:

$result=mysql_query($addSQLQuery,$db_handle);

should read(mysqli extension):

$result=mysqli_query($db_handle,$addSQLQuery);

Hope this helps!

2 Comments

check php.net/mysql_query, OP is passing correct argument to mysql_query
Post changed to reflect use of mysqli extension only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.