Forum Moderators: coopster

Message Too Old, No Replies

Inconsistant DB entry & display

Opening/Cosing DB too often?

         

neophyte

8:00 am on Apr 24, 2005 (gmt 0)

10+ Year Member



I've got this wierd thing going on.

I've got a page that submits a users information into a table - from a form - and then immediatly draws all infromation back out from the table and displays it to a page. This is a guestbook application.

On my local box, everything works fine. No matter how many times - or how quickly - I submit and display infomation it always gets inserted into the table and drawn back out as per plan.

However, on the web server, sometimes the form data gets into the data table (and is then displayed on the page) and then other times it doesn't.

Hummm.

I'm opening and closing the connection to the db twice when new information is submitted (once to put the new info into the table, and then again to draw all info from the table), but only once if there's no new info to display...just the current stuff already in the table.

I'm using $HTTP_POST_VARS['submit'] to trap for the submit button being pressed when new info is to be supplied to the table - I don't think this has anything to do with it, but I thought I'd toss that in.

Anyway, I've been scratching my head over this and am wondering if I'm opening and closing the DB too many times and the DB on the web server just can't catch up.

Is that possible?

I'd be happy to post my code (via sticky or in the forum) if requested to help solve this issue.

Thanks for everyones help in advance,

Neophyte

neophyte

8:01 am on Apr 24, 2005 (gmt 0)

10+ Year Member



Actually, come to think of it, the functionality is (or should be) exactly the same as what happens when one posts a message to this forum.

Although I don't have an intermediate screen that says "refreshing new message" like happens here.

Neophyte

ncreegan

12:10 pm on Apr 24, 2005 (gmt 0)

10+ Year Member



do you have errors suppressed? Something in the data could be tripping up the insert and sending an error that you're not seeing.

or you may not be giving the script sufficient time to complete the insert, though unless it's really big data or a really archaic server that's pretty unlikely.

If it isn't one of those, post the script

neophyte

3:00 pm on Apr 24, 2005 (gmt 0)

10+ Year Member



ncreegan -

Thanks for the response. I'm using westhost as the hosting company for this site - same company as is used for Webmaster World. So I don't think server age or maintenance is an issue (although, I guess you never know).

I haven't checked if error handling has been supressed on my dev environment - will check that in the morning.

Posting the code here in the event that there is some glaring omission or error that someone else can see that I can't.

Really can't understand why this is working so perfectly on my box, but on the server it's hit and miss.

I'm afraid this code may look pretty amaturish, but I'm just learning this stuff.

As always, thanks to all in advance.

PS: have striped out beginning html and head tags as there's no php in there.

-------------------------------------------------------

<?php
$submit = $HTTP_POST_VARS['submit'];

if ($submit) {
addCommentToDB();
}
?>

<?php
$recordSet = getRecords("SELECT * FROM guest");

$num_rows = mysql_num_rows($recordSet);
?>

<body>
<div id="container">

<div id="bulletHole"></div>

<div id="header"><a href="/"><img src="../a/c/the_header.jpg" alt="" width="364" height="125" /></a></div>

<ul id="topNav">
<li><a href="/latest/" title="latest works." accesskey="1">Latest Works</a>&nbsp;¦&nbsp;</li>
<li><a href="/read/" title="Download a chapter from The Raven and The Hawk." accesskey="2">Read A Chapter</a>&nbsp;¦&nbsp;</li>
<li><a href="/buy/" title="Where to buy The Raven and The Hawk." accesskey="3">Buy The Book</a>&nbsp;¦&nbsp;</li>
<li><a href="/author/" title="biography." accesskey="4">The Author</a>&nbsp;¦&nbsp;</li>
<li><span class="active">Guestbook</span>&nbsp;¦&nbsp;</li>
<li><a href="/contact/" title="Contact via Email" accesskey="6">Contact</a></li>
</ul>

<div id="contentLong">

<h1>Guestbook.</h1>

<hr />

<table class="guestbookheader" cellpadding="0" cellspacing="0">
<tr>
<td class="totalComments">Comments: <?php print "$num_rows";?></td><td class="sign"><a href="guestbook_sign.htm"><img src="i/sign.jpg" />Sign Guestbook</a></td>
</tr>
</table>

<hr class="post" />

<table class="guestbook" cellspacing="0" cellpadding="0">
<?php

$recordSet = getRecords("SELECT name, city, country, rating, comment, `date`, `time`
FROM guest
ORDER BY id DESC");

while ($viewRows = mysql_fetch_row($recordSet)) {

$name=$viewRows[0];// put each name into variable $name
$city=$viewRows;// put each city into variable $city
$country=$viewRows[2];// put each country into variable $country
$rating=$viewRows[3];// put each rating into variable $rating
$comment=$viewRows[4];// put each comment into variable $comment
$date=$viewRows[5];// put each date into variable $date
$time=$viewRows[6];// put each time into variable $time

$comment= nl2p($comment);

print '<tr>';
print '<td class="guestinfo">';
print "<p>$name</p>";
print "<p>$city</p>";
print "<p>$country</p>";
print "<img src=\"i/bullet_$rating.jpg\" />";
print '</td>';

print '<td class="guestcomment">';
print "<p class=\"date\">Posted: $date at $time.</p>";
print '<hr />';
print "$comment";
print '</td>';
print '</tr>';

print '<tr>';
print '<td colspan=2><hr class="comment"></td>';
print '</tr>';
}
?>
</table>
</div>

</div>
</div>

<div id="footer">
<span class="copyright">Copyright &copy; 2005 All Rights Reserved.</span>

<ul id="bottomNav">
<li><a href="/site/" title="Site Map" accesskey="8">Site Map</a>&nbsp;¦&nbsp;</li>
<li><a href="/privacy/" title="Privacy Policy" accesskey="9">Privacy Policy</a>&nbsp;¦&nbsp;</li>
<li><a href="/" title="Home" accesskey="-">Home</a></li>
</ul>
</div>

</body>
</html>

<?php
function addCommentToDB()
{
$name=$_POST['name'];
$city=$_POST['city'];
$country=$_POST['country'];
$rating=$_POST['rating'];
$comment=$_POST['comment'];
$date=date("m/j/y");
$time=date("h:i A");

// Add guestbook entry to database.

include ('../_includes/connect.php');

$db = mysql_connect($db_host,$db_user,$db_pass);

mysql_select_db($db_name, $db)or die("Unable to select database");

$addThis = "INSERT INTO guest (name, city, country, rating, comment, `date`, `time`)
VALUES
('$name','$city','$country','$rating','$comment','$date','$time')";

mysql_query ($addThis);

mysql_close($db);

sendEmail ($comment);
}
?>

<?php // Send email notifying admin that a new post has been added to the guestbook
function sendEmail ($comment)
{
$subject="GUESTBOOK ENTRY ALERT";

$message="Guestbook entry made by\n\n$name\n\n" . "$comment";

$sender="guestbook@example.com";

mail("admin@example.com", $subject, $message, "From: $sender");
}
?>

<?php
function getRecords($query)
{
include ('../_includes/connect.php');

$db = mysql_connect($db_host,$db_user,$db_pass);

mysql_select_db($db_name, $db)or die("Unable to select database");

$result = mysql_query($query, $db);

mysql_close($db);

return $result;
}
?>

<?php // replaces new lines with <p> and </p>
function nl2p($str)
{
return str_replace('<p></p>', '', '<p>' . preg_replace('#\n¦\r#', '</p>$0<p>', $str) . '</p>');
}
?>

[1][edited by: jatar_k at 3:07 pm (utc) on April 24, 2005]
[edit reason] generalized emails [/edit]

neophyte

2:23 am on Apr 25, 2005 (gmt 0)

10+ Year Member



Have checked, and do have error messages enabled locally but I'm not getting any errors.

On the host, globals variables are set to ON.

Have changed the $_HTTP_POST_VARS['variable']; in the page code to simply $_POST['variable'];

Now my stuff gets into the database more consistantly, but not every time.

I've found that if I enter alot of text into the textfield (including /n's), the guestbook updates. But if I add just a little bit of text, with \n characters, it frequently doesn't. For example, a test entry with the following text didn't make it in the DB:

--------------------

really cant

believe it
now

-------------------

Here's the wierd bit:

The last line of addCommentToDB function triggers an email function that emails admin when something has been posted. This email function includes assorted variables including the comment.

Even though some posts don't get added to the DB, the emails come through flawlessly. Amended addComment function code is here:

function addCommentToDB()
{
$name=$_POST['name'];
$city=$_POST['city'];
$country=$_POST['country'];
$rating=$_POST['rating'];
$comment=$_POST['comment'];
$date=date("m/j/y");
$time=date("h:i A");

// Add guestbook entry to database.

include ('../_includes/connect.php');

$db = mysql_connect($db_host,$db_user,$db_pass);

mysql_select_db($db_name, $db)or die("Unable to select database");

$addThis = "INSERT INTO guest (name, city, country, rating, comment, `date`, `time`)
VALUES
('$name','$city','$country','$rating','$comment','$date','$time')";

mysql_query ($addThis);

mysql_close($db);

sendEmail($name, $comment, $date, $time);
}

I really don't see anything wrong with it, and my head's starting to hurt.

The host is running php v4.3.0 - and I'm running v4.3.7 locally. Host will allow me to install 4.3.10 if I desire.

Should I? Think 4.3.0 could be the problem?

neophyte

6:01 am on Apr 25, 2005 (gmt 0)

10+ Year Member



FOUND THE PROBLEM!

Wanted to post it here so if anyone else has the same issue they'll know what to do.

Magic Quotes are off on the server, but on on my dev box.

Sooooo, have found that single or double quotes absolutely screw up an Insert into mySQL. Not just the variable that contins the quote(s) either, but all variables that come along with the post data.

After grabbing all post variables, I filtered those variables that could potentially contain single or double quotes (coming from input text and textarea fields) with addslashes(); PREVIOUS to the INSERT query.

Now - no more problems! Anything I put into the submission form gets put into the DB and is then displayed on the page.

Cool. Hope this helps someone else.

Neophyte