Data being added when loading page, not pressing 'Submit' button

Hi,

I’m having problems when loading a page, it’s adding blank database to my MySQL table.
What have i done wrong that is causing blank data to be in putted when loading the page?

I have the following;

Index.php
– Shows 2 links, 1 to add, 1 to display

addtour.php
– 4 input boxes and 1 submit button

showtour.php
– Shows the details added from the above page.

[size=12pt]INDEX.PHP[/size]

<html>
<head>
<title>Title</title>
</head>

<body>
<h3>Header</h3>

<a href="addtour.php">Add Show</a> | <a href="showtour.php">Show Dates</a>

</body>
</html>

[size=12pt]ADDTOUR.PHP[/size]

<html>
<head>
<title>Titlen</title>
</head>

<body>
<h3>Header</h3>

<a href="addtour.php">Add Show</a> | <a href="showtour.php">Show Dates</a>

<br>

<form action="addtour.php" method="post">
Location: <input type="text" name="TourLocation"><br>
Date: <input type="text" name="TourDate"><br>
Time: <input type="text" name="TourTime"><br>
Comments: <input type="text" cols="45" name="TourComments"><br>
<input type="Submit">
</form>

<br>

<?php
$dbhost="xxx";
$username="xxx";
$password="xxx";
$database="xxx";

$TourLocation=$_POST['TourLocation'];
$TourDate=$_POST['TourDate'];
$TourTime=$_POST['TourTime'];
$TourComments=$_POST['TourComments'];

mysql_connect($dbhost, $username, $password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO TableName VALUES ('','$TourLocation','$TourDate','$TourTime','$TourComments')";

mysql_query($query);

mysql_close();

?>

</body>
</html>

[size=12pt]SHOWTOUR.PHP[/size]

[code]

Title

Show Details


Add Show | Show Dates
<?php $dbhost="xxx"; $username="xxx"; $password="xxx"; $database="xxx"; mysql_connect($dbhost, $username, $password); @mysql_select_db($database) or die( "Unable to connect to the database"); $query="SELECT * FROM TableName Order By TourID DESC"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); ?> <?php $i=0; while ($i < $num) { $f1=mysql_result($result,$i,"TourLocation"); $f2=mysql_result($result,$i,"TourDate"); $f3=mysql_result($result,$i,"TourTime"); $f4=mysql_result($result,$i,"TourComments"); ?> <?php $i++; } ?> [/code]
Location Date Time Other Information
<?php echo $f1; ?> <?php echo $f2; ?> <?php echo $f3; ?> <?php echo $f4; ?>

Do you mean it is adding blank record to your database table? Try to explicitely set fields order in you insert query, like in this example:

INSERT INTO TableName (Field1, Field2) VALUES ('$Value1','$Value2')

Also, to see what is wrong with your sql query, you can add this after myswl_query() function call:
[php]echo mysql_error();[/php]

Edit:
Actually to fix your problem you just need to add this condition before adding frecord in your ADDTOUR.PHP
[php]if(isset($_POST[‘TourComments’])){

// insert record here

}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service