/    Sign up×
Community /Pin to ProfileBookmark

php database problem

I am new to web development, and all the languages related to it. I am trying to make a form connect to a database using php. When I run the script, I get the page saying “Thank you! Information entered.” that I set up, but when I look at my database (I use mysql with phpmyadmin), no information has been received. Any help would be appreciated.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsMar 30.2005 — post your code
Copy linkTweet thisAlerts:
@NogDogMar 30.2005 — Can't really say much without any code to look at. The only suggestion I can give right now is to do a lot of defensive coding. This means never assuming anything worked without checking to see if it worked. As an example:
[code=php]
$connection = mysql_connect('localhost', 'user', 'password');
if(!$connection)
{
die("Error connecting to database: " . mysql_error());
}
$db = mysql_select_db('db_name');
if(!$db)
{
die("Error selecting database: " . mysql_error());
}
$query = "INSERT INTO table_name VALUES('one', 'two', 'three')";
$result = mysql_query($query);
if(!$result)
{
die("Error inserting record: " . mysql_error());
}
if(mysql_num_rows() == 0)
{
die("Error: row was not entered into the database.");
}
[/code]
Copy linkTweet thisAlerts:
@kq105authorMar 30.2005 — <html>

<body>



<?php



if ($submit) {

// process form

$db = mysql_connect("localhost", "********", "**********");

mysql_select_db("kf99kq105_com_-_visitor",$db);

$sql = "INSERT INTO visitor (name,age,email,) VALUES ('$name','$age','$email')";

$result = mysql_query($sql);

echo "Thank you! Information entered.n";

} else{



// display form



?>



<form method="post" action="<?php echo $PHP_SELF?>">

Name:<input type="Text" name="name"><br>

Age:<input type="Text" name="age"><br>

Email Adress:<input type="Text" name="email"><br>

<input type="Submit" name="submit" value="Enter information">

</form>



<?php



} // end if



?>



</body>



</html>
Copy linkTweet thisAlerts:
@NogDogMar 30.2005 — I don't like that comma at the end of the list of columns in the query string.

Also, it's always safer to refer to your form input variables by their full array name, as they may not be accessible by just the form item name, depending on your PHP version and configuration. So I'd write the query:
[code=php]
$sql = "INSERT INTO visitor (name,age,email) VALUES ('{$_POST['name']}', '{$_POST['age']}', '{$_POST['email']}')";
[/code]

Similarly, I'd write the first if statement as:
[code=php]
if (isset($_POST['submit'])) {
[/code]
Copy linkTweet thisAlerts:
@kq105authorMar 31.2005 — Problem fixed...THANKS!!!
×

Success!

Help @kq105 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,
)...