/    Sign up×
Community /Pin to ProfileBookmark

add data to mysql

I have this form and added it to my host and also added a db in PHPMyAdmin.

So I created a db called “the_form” and created a table called: form and added a id and a first_name to that table….so…

something is missing that when i hit submit I only get : “please fill out the whole form”

index:

[CODE]
<form action=”set.php” method=”post”>
<input name=”first_name” type=”text” value=”” />
</form>
[/CODE]

set.php:

[code=php]
<?php
include ‘add.php’;

$first_name = $_POST[‘first_name’];
if (!$_POST[‘submit’]) {
echo “please fill out the whole form”;
header(‘Location: index.html’);
} else {
mysql_query(“INSERT INTO form (`ID`, `first_name`)
VALUES(NULL,’$first_name’)”) or die(mysql_error());
echo “user is added”;
header(‘Location: index.html’);
}

?>
[/code]

add.php:

[CODE]
<?php
$dbhost = ‘localhost’;
$username = ‘admin’;
$password = ”;
$db = ‘the_form’;

$conn = mysql_connect($dbhost,$username, $password);
mysql_select_db($db);
?>
[/CODE]

Any ideas?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 21.2011 — I don't see a form element named "submit", and therefore there is no corresponding $_POST['submit'] (unless you left something out in the code snippet here?).
Copy linkTweet thisAlerts:
@kingdmApr 21.2011 — As NogDog had said, <input type="submit"> tag is missing. If that current code you posted (index), is the one you had actually on your script, it will display only a textbox on run. I wonder how you managed to pass [B]first_name[/B] to set.php if the submit button is missing.
Copy linkTweet thisAlerts:
@riskmodauthorApr 21.2011 — I don't see a form element named "submit", and therefore there is no corresponding $_POST['submit'] (unless you left something out in the code snippet here?).[/QUOTE]

Sorry I didn't put the whole thing but heres the whole index:

[CODE]
<html>
<head>
<title>form</title>

</head>

<body>
<form action="set.php" method="post">
<input name="first_name" type="text" value="" /><br />
<input type="submit" value="submit" />
</form>
</body>
</html>

[/CODE]
Copy linkTweet thisAlerts:
@wwwapprenticeApr 21.2011 — You simply did not name your submit button, so simply add it;

[code=html]<input type="submit" value="submit" name="submit" />[/code]

You should also check if the text field is not empty,

Oh and you also get a warning when you try to echo before a header() function, you probably want something like this:
[code=php]if (!isset ($_POST['submit']) || $first_name == "") {
header('Refresh:3;index.html');
echo "please fill out the whole form";
} else {
mysql_query("INSERT INTO form (ID, first_name)
VALUES(NULL,'$first_name')") or die(mysql_error());
header('Refresh:3;index.html');
echo "user is added";
}[/code]


Let me know if you get it to work!

grtz

wwwapprentice
Copy linkTweet thisAlerts:
@riskmodauthorApr 21.2011 — Yes it worked thanks!
×

Success!

Help @riskmod spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...