Jump to content

Some users can't enter info through my form to my DB


koolkat67

Recommended Posts

sure...im a total newbie to php so dont laugh at my coding...lol.  there is probably more than i need to post there.  I appreciate the help

 

 

<div id="blogcommentform">

<?php

if($submit)

{

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

mysql_select_db("akbhangu");

$blog="sleepyheads";

$sql = "INSERT INTO mainblogcomments (name, location, comment, blog)

VALUES ('$name', '$location', '$comment', '$blog')";

$result = mysql_query($sql);

$sql = "ALTER TABLE mainblogcomments ORDER BY `id` DESC";

$result = mysql_query($sql);

echo "Thank you! Information entered.\n";

mysql_close();

header("Location: /mainblogcomments.php");

}

?>

<FORM ACTION="<?php echo $PHP_SELF; ?>" METHOD=POST>

<table>

<tr><td align="right">First name:</td><td align="left"><input type="Text" name="name" width="200"></td></tr>

<tr><td align="right">Location:</td><td align="left"><input type="Text" name="location" width="200"></td></tr>

<tr><td colspan="2">Comments (Remember:Family site so keep it clean):</td></tr>

<tr><td colspan="2"><TEXTAREA NAME="comment" ROWS=5 COLS=40></TEXTAREA></td></tr>

<tr><td align="center" colspan="2"><input type="Submit"name="submit" value=" Enter information"></td></tr></table>

</form>

</div>

<hr />

<div id="blogcomments">

<?php

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

mysql_select_db("akbhangu");

$result = mysql_query("SELECT * FROM mainblogcomments WHERE blog='sleepyheads'");

?>

<table>

<tr align="center"><td width="500">Comment</td><td width="200">Last Post</td></tr>

<?php

$i=0;

while($myrow = mysql_fetch_array($result))

{

$i++;

if ($i % 2 ==0)

{

?>

<tr align="left" valign="top">

<td><?php echo $myrow["comment"];?></td>

<td>| by <?php echo $myrow["name"];?> in <?php echo $myrow["location"];?><br />| on <?php echo $myrow["date"];?></td>

</tr>

<?php

}

else

{?>

<tr align="left" valign="top">

<td bgcolor="#FFFFCC"><?php echo $myrow["comment"];?></td>

<td bgcolor="#FFFFCC">| by <?php echo $myrow["name"];?> in <?php echo $myrow["location"];?><br />| on <?php echo $myrow["date"];?></td>

</tr>

<?php

}

} ?>

</table>

</div>

 

Link to comment
Share on other sites

$sql = "INSERT INTO mainblogcomments (name, location, comment, blog)
VALUES ('$name', '$location', '$comment', '$blog')";
$result = mysql_query($sql);
$sql = "ALTER TABLE mainblogcomments ORDER BY `id` DESC";

 

I take it id is an auto_increment value? If so this should work with no problems unless name is UNIQUE if so that is where the problem lies.

 

Also I do not know why you would want to use the alter table right there when you can specify the order by when retrieving data. I would think the alter by every time would cause a major slowdown if the table contained a lot of data.

Link to comment
Share on other sites

php coding should be browser independant.

 

You also have no commenting showing anyone else what you are doing, this is a bad thing especially when your looking for support.

 

I have changed the code as follows:

Removed the header comment, this is not needed as the user is redirected to the page after he submits.

Added error checking. My guess is that one or all of the fields in the database are set to not accept null values, so I check to see if the values are null before submitting to the db.

changed the date format -

 

I store the date in unix format - I didnt see where you had that in your code - so I added $date = time(), and stored to the database in the date field. Hope this helps. you can view the code in action at http://www.sandynpaul.com/test/addcomment.php

 

<div id="blogcommentform">
<?php
//connect to the database
$db = mysql_connect("localhost", "*", "*");
mysql_select_db("*");
if(isset($_POST['submit'])) { //if the user has submitted the form
//Should have some checking variable checking going on here so that the variables are defined regardless of the users input;
if (isset($_POST['name'])) {
	$name = $_POST['name'];
} else {
	$name = NULL;
}
if (isset($_POST['location'])) {
	$location = $_POST['location'];
} else {
	$location = NULL;
}
if (isset($_POST['comment'])) {
	$comment = $_POST['comment'];
} else {
	$comment = NULL;
}
$blog="sleepyheads";
$date = time();
if ($name && $location && $comment) { //if the variables are set
	$sql = "INSERT INTO mainblogcomments (name, location, comment, blog, date)
	VALUES ('$name', '$location', '$comment', '$blog', '$date');";
	$result = mysql_query($sql);
	$sql = "ALTER TABLE mainblogcomments ORDER BY `id` DESC";
	$result = mysql_query($sql);
	echo "Thank you! Information entered.\n";
	mysql_close();
} else { //variables arent set, lets echo an error message for the ones not set
	if ($name == NULL) {
		echo 'You must enter a name!';
	}
	if ($comment == NULL) {
		echo 'You must enter a comment!';
	}
	if ($location == NULL) {
		echo 'You must enter a location!';
	}
}
}
?>
<FORM ACTION="<?php echo $PHP_SELF; ?>" METHOD=POST>
<table>
<tr><td align="right">First name:</td><td align="left"><input type="Text" name="name" width="200"></td></tr>
<tr><td align="right">Location:</td><td align="left"><input type="Text" name="location" width="200"></td></tr>
<tr><td colspan="2">Comments (Remember:Family site so keep it clean):</td></tr>
<tr><td colspan="2"><TEXTAREA NAME="comment" ROWS=5 COLS=40></TEXTAREA></td></tr>
<tr><td align="center" colspan="2"><input type="Submit" name="submit" value=" Enter information"></td></tr></table>
</form>
</div>
<div id="blogcomments">
<?php
//connect to the database
$db = mysql_connect("localhost", "*", "*");
mysql_select_db("*");
$result = mysql_query("SELECT * FROM mainblogcomments WHERE blog='sleepyheads'");
?>
<table>
<tr align="center"><td width="500">Comment</td><td width="200">Last Post</td></tr>
<?php
$i=0;
while($myrow = mysql_fetch_array($result))
{
$i++;
if ($i % 2 ==0)
{
?>
<tr align="left" valign="top">
<td><?php echo $myrow["comment"];?></td>
<td>| by <?php echo $myrow["name"];?> in <?php echo $myrow["location"];?>
| on <?php echo date("Y-m-d H:i:s",$myrow["date"]);?></td>
</tr>
<?php
}
else
{?>
<tr align="left" valign="top">
<td bgcolor="#FFFFCC"><?php echo $myrow["comment"];?></td>
<td bgcolor="#FFFFCC">| by <?php echo $myrow["name"];?> in <?php echo $myrow["location"];?>
| on <?php echo date("Y-m-d H:i:s", $myrow["date"]);?></td>
</tr>
<?php
}
} ?>
</table>
</div>

Link to comment
Share on other sites

ah man, i totally have to beef up my commenting, i know!  thanks so much for putting all this effort into this.

www.alaskabhangu.com is my site by the way and if you click on Have a Comment it goes to this page.  I'll try your code tonight but even if you miss entering in a field it still goes to my db and leaves the unentered fields empty.

Link to comment
Share on other sites

Sometimes using

isset($_POST['submit'])

can be dangerous.  Some browsers (I'm not sure which off hand) will not send the submit variable if the submit button is not pressed.  This means if the user simply types in the form, then presses the "Enter" key, the submit button's value is not sent, which means that the $_POST array key "submit" will not exist.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.