What's wrong with this PHP?

Permabanned
Joined
22 Apr 2007
Posts
1,805
I've done quite a few of these now but for some reason this one refuses to work.

Its based on a Win2K3 box running IIS6, PHP and MySQL5

Table (called contacts)

68532834ht1.jpg


and here is the code. In order to break this down I'm trying to insert one thing into one field

Code:
<?php
session_start();
if(!$_SESSION['username']) {
       //not logged in. redirect to login page
       header("Location: index.php");
       exit;
}

include("connect.php"); 

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

</head>

<body>
<h1>Client Entry Form</h1>
<?php
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
<p><label>Title:</label><input type="text" name="title" maxlength="60" /></p>
<p><input type="submit" name="submit" value="Client Details Submit" /></p>
<p><input type="reset" value="Reset Form" name="reset" /></p>
</form>

<?php
} else {

$title = $_POST['title'];

mysql_query("INSERT INTO 'contacts' (title) VALUES ('$title')");
echo "<h2>Thank you</h2>";
}

mysql_close();

?>
</body>
</html>

Help! :(
 
Tried it without the quotes on the table name? INSERT INTO contacts rather than INSERT INTO 'contacts' I'm not a SQL expert but just suggestion, dunno i it makes any difference.
 
as Robh suggested you don't need quotes round the table name, also I would suggest that you do:

Code:
mysql_query("INSERT INTO contacts (title) VALUES ('$title')") or die(mysql_error());

Even though mysql error messages are sometimes a bit vague it will help you debug your statement
 
Well surely it's pretty silly not to escape your input... telling him to escape it would only be good advice!
 
Back
Top Bottom