PDA

View Full Version : Newbie - Attempts to write to a db.


ptmuldoon
03-12-2005, 03:32 PM
I hope this belongs here, and not in the mysql forum. If so, I apologize and could a mod please move it accordingly.

I mentioned earlier that I'm listening/watching and following along some php and mysql VTC's to try and learn. I've been making some basic newbie progress and am beginning to grasp some basic concepts. But, as expected of a newbie, I've stumbled, and dont' see whats wrong here.

In a nutshell, I've got two files, a html form, and a php script to write the info to a database. But I keep being told that I haven't enter all the fields even thought I have, and I can't seem to find the error.

The html form is simple and looks like this:
<html>
<head>
<title>Feedback form</title>

<body>
<h2>Feedback</h2>
<br>
<form action="send_feedback.php" method="post">
Your name is:
<input type=text name="user" maxlength=40 size=40>
<br>
Your email address is:
<input type=text name="email" maxlength=40 size=40>
<br><br>
Can we keep you updated with news about our site?
<input name="spam" type=radio value="1" checked>Yes
<input name="spam" type=radio value="1">No
<br><br>
Comments:
<br>
<textarea cols=40 rows=10 name="comments"></textarea>
<br>
<input type=submit value="submit">
</form>
</body>
</html>

And the php script looks like this:
<html?
<head>
<title>Thanks for your feedback</title>
</head>
<body>
<?
if(!$user || !$email || !$comments) {
?>
<h2>Please complete all of the fields</h2>
<br>
<a href="feedback.html">Click here to go back to the feedback page</a>
<?
exit;
}
$user = addslashes($user);
$email = addslashes($email);
$comments = addslashes($comments);

// Connect to the database
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("mots2", $db);

$addfeedback = "INSERT INTO feedback (user, email, spam, comments)
VALUES ('".$user."', '".$email."', '".$spam."', '".$comments."')";
$result = mysql_query($addfeedback);
?>
<h2>Thank you for your feedback</h2>
<br>
We have added your comments to the our database
</body>
</html>

Brandoe85
03-12-2005, 03:50 PM
All of your form values are stored in the $_POST array. You can access them with $_POST['fieldname']. Try something like this for your php page:

<html>
<head>
<title>Thanks for your feedback</title>
</head>
<body>
<?

$user = addslashes($_POST['user']);
$email = addslashes($_POST['email']);
$comments = addslashes($_POST['comments']);
$spam = $_POST['spam'];

if(!$user || !$email || !$comments) {
?>
<h2>Please complete all of the fields</h2>
<br>
<a href="feedback.html">Click here to go back to the feedback page</a>
<?
exit;
}
// Connect to the database
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("mots2", $db);


$addfeedback = "INSERT INTO feedback (user, email, spam, comments)
VALUES ('$user', '$email', '$spam', '$comments')";
$result = mysql_query($addfeedback);
?>
<h2>Thank you for your feedback</h2>
<br>
We have added your comments to the our database
</body>
</html>


Good luck; :)

ptmuldoon
03-12-2005, 03:59 PM
Thanks

That did the trick. But for a newbie, would it be possible for you or someone to explain exactly what that did?

My understanding is that the form was passing the variables, $user, $email, and $comments, to the php script, and the script would look to see if those variables were complete, and if not spit out the warning, or if they are complete to add it the the database.

So the variables were being passed to the php script. But whats the difference between

$user = addslashes($user);
and
$user = addslashes($_Post['user']);

pt

Brandoe85
03-12-2005, 04:17 PM
Sure :)

All of your form variables are stored in the $_POST array.
This (http://us4.php.net/variables.predefined) Has some good info about it. If your book is accessing the form variables through just $user, instead of $_POST['user'] Then they are using a depreciated method.
This line $user = addslashes($_POST['user']), is creating a variable called user which contains what was entered in your user field in your form. Also, instead of this:

if(!$user || !$email || !$comments) {
?>
<h2>Please complete all of the fields</h2>
<br>
<a href="feedback.html">Click here to go back to the feedback page</a>
<?
exit;
}

You probably want to look into Empty (http://us4.php.net/empty) or isset (http://us4.php.net/isset) Which will work out better for checking if something was entered.