1

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>
7
  • What exactly do you mean by that: the "input1 field remains empty? You mean the column imput1 only holds an empty string in the database? Commented Sep 28, 2016 at 10:12
  • check for empty($value) and insert only if it is not empty Commented Sep 28, 2016 at 10:14
  • Yes input 1 is both the name of my form and database fields. So in my database I only have 2 fields, ID (on AI) and input1. So on submission of the form whatever entered should be sent to the input1 field in the database. But it's not. However I do get the ID entry. Commented Sep 28, 2016 at 10:15
  • Haha totally forgot about my connection error messages. Thought it would be a laugh to "Scottish" them up a little ;) Commented Sep 28, 2016 at 10:16
  • try making it text instead of varchar. Commented Sep 28, 2016 at 10:21

2 Answers 2

1
<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).

Sign up to request clarification or add additional context in comments.

2 Comments

Good spot! I've updated it, but doesn't resolve the issue. :(
i would basically look out 2 things, 1. if value is being posted if its there means there is some issue with insert. 2. what is type of input1 column in DB? does your code inserts a value appended in SQL insert statically like "insert into demo (input1) values ('hello')".
0

Change this line

$sql = "INSERT INTO demo (input1) VALUES ('$value')";

with

$sql = "INSERT INTO demo (input1) VALUES ('".$value ."')";

does this inserts proper values?

3 Comments

No, unfortunately that makes no difference.
Weird ! what is $value in var_dump()? does it contains some special chars, if so think about Sanitization. Anyways above code is not sanitized as its directly sending posted value to DB.
Hmmm it seems to work now. I just changed the name of the HTML form from insert1 to insertform and bobs your uncle. Perhaps there was a space between the last character of insert one and the quotation mark, but it sure doesn't look like it. I.e. $value = $_POST['input1 '];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.