Reputation: 123
I've built a very simple html form with only one input box. I'm trying to get the value entered into my database.
It only seems to be half working as when I submit the form, I do get an entry and an ID number (which is on auto-incriment) however the "input1 field remains empty.
It's on VARCHAR and NULL. I've changed it to INT but does the same thing.
I'm really not sure whether the problem lies with MySQL or PHP.
I'm a little prone to typos in my code, but for the life of me I can't find one here.
Does anyone have a solution?
Regards
PHP PAGE
<?php
define ('DB_NAME', 'xxxx');
define ('DB_USER', 'xxxx');
define ('DB_PASSWORD', 'xxxx');
define ('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Cannae connect tay the server, sorry pal: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die ('Ya just pure cannae connect tay the big bad mental database min: ' . DB_NAME . ': ' . mysql_error());
}
echo 'Connected successfully';
$value = $_POST['input1'];
$sql = "INSERT INTO demo (input1) VALUES ('$value')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
mysql_close ();
?>
FORM PAGE
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Test form</h2>
<p>This form will send data to the MySQL database.</p>
</br>
<form action="testsend.php" method="post" />
<p>Input 1: <input type="text" name"input1" /></p>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Upvotes: 1
Views: 64
Reputation: 757
Change this line
$sql = "INSERT INTO demo (input1) VALUES ('$value')";
with
$sql = "INSERT INTO demo (input1) VALUES ('".$value ."')";
does this inserts proper values?
Upvotes: 0
Reputation: 12378
<p>Input 1: <input type="text" name"input1" /></p>
This line you may miss =
, try to add it.
<p>Input 1: <input type="text" name="input1" /></p>
in case you are not sure whats being passed from your Form use var_dump() just before adding into database. As suggested by @JPG seems equal sign is missing which in turn creating problem with POST as input1 is not being posted. You can verify it via browser inspection (Firebug / developer tool).
Upvotes: 1