Problem with PHP & MySQL

Associate
Joined
12 Aug 2004
Posts
1,009
Location
Glasgow, Scotland
Hi,

I'm working from a book and trying to add new data via a simple form on my website. The problem is when I go to submit the form I just get forwarded to my index page :(

I was hoping someone with some more knowledge on the subject could see where I'm going wrong.

Thanks.

Code:
<?php require 'secure.inc.php'; ?>

<strong>Add New Villa</strong>

<br />
<br />

<?php if(isset($_POST['categoryid'])):

	// Connect to the database server
	$dbcnx = @mysql_connect('mysql7.streamline.net', 'pinesvill2', '*****');
	
	if(!$dbcnx)
	{
		exit('<p>Unable to connect to the database server at this time.</p>');
	}
	
	// Select the villa database
	if(!@mysql_select_db('pinesvill2'))
	{
		exit('<p>Unable to locate the villa database at this time.</p>');
	}
	
	$categoryid = $_POST['categoryid'];
	$name = $_POST['name'];
	$description = $_POST['description'];
	$pricing = $_POST['pricing'];
	$contact = $_POST['contact'];
	
	$sql =	"INSERT INTO villas SET
			categoryid='$categoryid',
			name='$name',
			description='$description',
			pricing='$pricing',
			contact='$contact',
			dateadded=CURDATE()";
			
	if(@mysql_query($sql))
	{
		echo '<p>New villa added succesfully</p>';
	}
	else
	{
		echo '<p>Error adding new villa: mysql_error().</p>';
	}
	
?>

<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Add another villa</a></p>
<p><a href="index.php?page=admin">Return to Administration Area</a></p>

<?php else: ?>
	
	<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">	
		<table>
			<tr>
				<td>Category:</td>
				<td><input type="text" name="categoryid" /></td>
			</tr>
			
			<tr>
				<td>Villa name:</td>
				<td><input type="text" name="name" /></td>
			</tr>
			
			<tr>
				<td>Description:</td>
				<td><textarea name="description" rows="10" cols="40"></textarea></td>
			</tr>
			
			<tr>
				<td>Pricing:</td>
				<td><input type="text" name="pricing" /></td>
			</tr>
			
			<tr>
				<td>Contact:</td>
				<td><input type="text" name="contact" /></td>
			</tr>
			
			<tr>
				<td></td>
				<td><input type="submit" value="Add Villa" /></td>
			</tr>
		</table>
	</form>

<?php endif; ?>

(I have scored out the password for obvious reasons)
 
MastermindUK said:
Try <form action="" method="post">

Cheers mate,

Works perfectly now. After I removed that the SQL error appeared which turned out I was using the wrong database, d'oh!, after changing the name it's working brilliantly now :)

Thanks again,

Steven.
 
Back
Top Bottom