Wolfetone00
12-03-2008, 11:17 PM
As Title says. I am trying to insert data that is held in a variable to my MySQL database, but won't. I can get it to work if I specifically add the string into the mysql_query(), but that kinda defeats the purpose of getting information from a form. Here is my code:
<?php
include 'databaseFunctions.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
</head>
<body>
<div id="commentContainer">
<h3><span><? countComments();?> Comments</span></h3>
<!-- Comments -->
<div id="comments">
<ol id="commentList">
<li id="comment">
<p>On (Date of comment; 24th Sept. 08)Name said . . .</p>
<blockquote>
<p>I am smart comment.</p>
</blockquote>
</li>
</ol>
</div>
<!-- End Comments -->
<h3><span>Post a Comment</span></h3>
<?php
if(isset($_POST['postComment'])){
$commentName = $_POST['commentName'];
$commentEmail = $_POST['commentEmail'];
$commentWebsite = $_POST['commentWebsite'];
$commentMessage = $_POST['commentMessage'];
$errors = array();
if (eregi("^[a-z0-9]{1,30}$", $commentName)){
}else{
$errors[] = ' Name';
}
if (eregi("^[a-z0-9._-]+@[a-z0-9-]+\.[a-z.]{2,5}$", $commentEmail)){
}else{
$errors[] = ' Email';
}
if (strlen($commentWebsite) > 0){
if (eregi("^[a-z0-9.-_\/?]+\.[a-z]{2,5}$", $commentWebsite)){
}else{
$errors[] = ' Website';
}
}
if (strlen($commentMessage) > 0 && strlen($commentMessage) < 300){
}else{
$errors[] = " Comment";
}
if (count($errors) > 0){
echo '<div id="erroreNote">';
echo '<h3>';
echo 'Your';
foreach ($errors as $value){echo $value;}
echo ' is/are not valid.';
echo '</h3>';
echo '</div>';
}else{
$address="10.6.186.112";
$user="*****";
$password="******";
$database="******";
mysql_connect($address,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO comments(name, email, website, comment) VALUES ('$commentName','$commentEmail','$commentWebsite','$commentMessage')";
mysql_query($query);
mysql_close();
}
}
?>
<div id="form">
<form id="commentForm" name="commentForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"">
<p>Required fields are marked with an asterisk (*).
<fieldset id="pInfo">
<legend>Personal Information</legend>
<label for="commentName">*Name: </label>
<input type="text" name="commentName" id="commentName" size="30" maxlength="30" value=""/><br /><br />
<label for="commentEmail">*E-mail: </label>
<input type="text" name="commentEmail" id="commentEmail" size="30" value=""/><br /><br />
<label for="commentWebsite">Website: </label>
<input type="text" name="commentWebsite" id="commentWebsite" size="30" maxlength="30" value="" /><br />
</fieldset>
<fieldset id="comMessage">
<legend>Comment</legend>
<label for="commentMessage">*Comment (300 character limit): </label>
<textarea name="commentMessage" id="commentMessage" wrap="physical" rows="10" cols="50"></textarea><br />
</fieldset>
<br />
<input type="hidden" name="process" value="1">
<input type="submit" name="postComment" value="Post Comment">
</form>
</div>
<!-- End Form -->
</div>
</body>
</html>
The main problems seems to be line 78:
$query = "INSERT INTO comments(name, email, website, comment) VALUES ('$commentName','$commentEmail','$commentWebsite','$commentMessage')";
If I replace the variables with plain strings like I mention:
$query = "INSERT INTO comments(name, email, website, comment) VALUES ('name','a@b.com','blah.com','Hello')";
works fine.
I bet it something simple, but this has me stumped. Thanks in advance for any help:thumbsup:
<?php
include 'databaseFunctions.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
</head>
<body>
<div id="commentContainer">
<h3><span><? countComments();?> Comments</span></h3>
<!-- Comments -->
<div id="comments">
<ol id="commentList">
<li id="comment">
<p>On (Date of comment; 24th Sept. 08)Name said . . .</p>
<blockquote>
<p>I am smart comment.</p>
</blockquote>
</li>
</ol>
</div>
<!-- End Comments -->
<h3><span>Post a Comment</span></h3>
<?php
if(isset($_POST['postComment'])){
$commentName = $_POST['commentName'];
$commentEmail = $_POST['commentEmail'];
$commentWebsite = $_POST['commentWebsite'];
$commentMessage = $_POST['commentMessage'];
$errors = array();
if (eregi("^[a-z0-9]{1,30}$", $commentName)){
}else{
$errors[] = ' Name';
}
if (eregi("^[a-z0-9._-]+@[a-z0-9-]+\.[a-z.]{2,5}$", $commentEmail)){
}else{
$errors[] = ' Email';
}
if (strlen($commentWebsite) > 0){
if (eregi("^[a-z0-9.-_\/?]+\.[a-z]{2,5}$", $commentWebsite)){
}else{
$errors[] = ' Website';
}
}
if (strlen($commentMessage) > 0 && strlen($commentMessage) < 300){
}else{
$errors[] = " Comment";
}
if (count($errors) > 0){
echo '<div id="erroreNote">';
echo '<h3>';
echo 'Your';
foreach ($errors as $value){echo $value;}
echo ' is/are not valid.';
echo '</h3>';
echo '</div>';
}else{
$address="10.6.186.112";
$user="*****";
$password="******";
$database="******";
mysql_connect($address,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO comments(name, email, website, comment) VALUES ('$commentName','$commentEmail','$commentWebsite','$commentMessage')";
mysql_query($query);
mysql_close();
}
}
?>
<div id="form">
<form id="commentForm" name="commentForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"">
<p>Required fields are marked with an asterisk (*).
<fieldset id="pInfo">
<legend>Personal Information</legend>
<label for="commentName">*Name: </label>
<input type="text" name="commentName" id="commentName" size="30" maxlength="30" value=""/><br /><br />
<label for="commentEmail">*E-mail: </label>
<input type="text" name="commentEmail" id="commentEmail" size="30" value=""/><br /><br />
<label for="commentWebsite">Website: </label>
<input type="text" name="commentWebsite" id="commentWebsite" size="30" maxlength="30" value="" /><br />
</fieldset>
<fieldset id="comMessage">
<legend>Comment</legend>
<label for="commentMessage">*Comment (300 character limit): </label>
<textarea name="commentMessage" id="commentMessage" wrap="physical" rows="10" cols="50"></textarea><br />
</fieldset>
<br />
<input type="hidden" name="process" value="1">
<input type="submit" name="postComment" value="Post Comment">
</form>
</div>
<!-- End Form -->
</div>
</body>
</html>
The main problems seems to be line 78:
$query = "INSERT INTO comments(name, email, website, comment) VALUES ('$commentName','$commentEmail','$commentWebsite','$commentMessage')";
If I replace the variables with plain strings like I mention:
$query = "INSERT INTO comments(name, email, website, comment) VALUES ('name','a@b.com','blah.com','Hello')";
works fine.
I bet it something simple, but this has me stumped. Thanks in advance for any help:thumbsup: