Reputation: 241
Here's the code:
<html>
<head>
<title>emarket.com</title>
<link rel="stylesheet" type="text/css" href="./style.css"/>
</head>
<header>
<h1><a href="index.php" style="text-decoration:none">emarket.com</a></h1>
<h1><a href="postad.php" style="text-decoration:none; padding-left:700px">Post Ad</a></h1>
</header>
<body>
<?php
if(!empty($_POST)){
$title=$_POST['title'];
$description=$_POST['description'];
$district=$_POST['district'];
$phone=$_POST['phone'];
$catagory=$_POST['catagory'];
$price=$_POST['price'];
mysql_connect("localhost","root","");
mysql_select_db("maindb");
mysql_query("INSERT into allads VALUES(NULL,'$title','$phone','$description', '$price','$district','$catagory')");
echo "<span style='background-color: deepskyblue;'>Your advert is now online!</span>";
}
?>
<form class="postad" method="post" action="">
<fieldset><caption><h2>Post New Advert</h2></caption></fieldset>
<fieldset>
<label>Title</label><br/>
<input type="text" name="title" class="form-text" required>
</fieldset>
<fieldset>
<label>Description</label><br/>
<input name="description" class="form-text" required>
</fieldset>
<fieldset>
<label>District</label><br/>
<select name="district"required>
<option selected>Colombo</option>
<option>Kandy</option>
<option>Matara</option>
<option>Galle</option>
</select>
</fieldset>
<fieldset>
<label>Category</label><br/>
<select name="catagory"required>
<option value="Electronics">Electronics</option>
<option value="Property">House and Property</option>
<option value="Auto">Automobiles</option>
<option value="Misc">Miscellaneous</option>
</select>
</fieldset>
<fieldset>
<label>Phone</label><br/>
<input type="tel" name="phone" maxlength="10" class="form-text" required>
</fieldset>
<fieldset>
<label>Price</label><br/>
Rs. <input type="number" name="price" class="form-text" required>
</fieldset>
<fieldset>
<input type="submit" name="submit" value="submit"/>
</fieldset>
</form>
</body>
</html>
This happened after I entered the PHP parts, it was visible in Firefox before that. Other files having PHP, like the home page, works fine. In chrome not only the website is visible, it works fine too. Stuff from the form gets inserted into the sql table without any problems.
Upvotes: 1
Views: 54
Reputation: 2229
Do you have the slightest idea of how vulnerable you are to mysql injection attacks? Please start using pdo. mysql_* is deprecated.
I've cleaned the function for you. The php part will only be executed once you click the submit button.
<?php
mysql_connect("localhost","root","");
mysql_select_db("maindb");
if(isset($_POST['submit']))
{
$title=isset($_POST['title']) ? $_POST['title'] : '';
$description=isset($_POST['description']) ? $_POST['description'] : '';
$district=isset($_POST['district']) ? $_POST['district'] : '';
$phone=isset($_POST['phone']) ? $_POST['phone'] : '';
$catagory=isset($_POST['catagory']) ? $_POST['catagory'] : '';
$price=isset($_POST['price']) ? $_POST['price'] : '';
if(!empty($title) && !empty($description) && !empty($district) && !empty($phone) && !empty($catagory) && !empty($price))
{
$q = mysql_query("INSERT into allads VALUES(NULL,'$title','$phone','$description', '$price','$district','$catagory')");
if($q)
{
echo "<span style='background-color: deepskyblue;'>Your advert is now online!</span>";
}
else
{
echo "Could not update DB";
}
}
}
?>
<html>
<head>
<title>emarket.com</title>
<link rel="stylesheet" type="text/css" href="./style.css"/>
</head>
<header>
<h1><a href="index.php" style="text-decoration:none">emarket.com</a></h1>
<h1><a href="postad.php" style="text-decoration:none; padding-left:700px">Post Ad</a></h1>
</header>
<body>
<form class="postad" method="post" action="">
<fieldset><caption><h2>Post New Advert</h2></caption></fieldset>
<fieldset>
<label>Title</label><br/>
<input type="text" name="title" class="form-text" required>
</fieldset>
<fieldset>
<label>Description</label><br/>
<input name="description" class="form-text" required>
</fieldset>
<fieldset>
<label>District</label><br/>
<select name="district"required>
<option selected>Colombo</option>
<option>Kandy</option>
<option>Matara</option>
<option>Galle</option>
</select>
</fieldset>
<fieldset>
<label>Category</label><br/>
<select name="catagory"required>
<option value="Electronics">Electronics</option>
<option value="Property">House and Property</option>
<option value="Auto">Automobiles</option>
<option value="Misc">Miscellaneous</option>
</select>
</fieldset>
<fieldset>
<label>Phone</label><br/>
<input type="tel" name="phone" maxlength="10" class="form-text" required>
</fieldset>
<fieldset>
<label>Price</label><br/>
Rs. <input type="number" name="price" class="form-text" required>
</fieldset>
<fieldset>
<input type="submit" name="submit" value="submit"/>
</fieldset>
</form>
</body>
</html>
?>
Edit :- IN situations like these, it's always better to put your php code before <html>
Also, what's your filename in which all this code exists? Put that filename in the <Form action="filenamehere.php">
.
If it still doesn't work, upload it somewhere to let me have a closer look.
Upvotes: 2
Reputation: 3337
Your header tag should be inside the body tag.
<body>
<header>
<h1><a href="index.php" style="text-decoration:none">emarket.com</a></h1>
<h1><a href="postad.php" style="text-decoration:none; padding-left:700px">Post Ad</a></h1>
</header>
Like @Akshay said, you should be using PDO, however you should be escaping your input using something like mysql_real_escape_string($value) before putting it into the database if you aren't using PDO.
Upvotes: 1