Jump to content


Photo

not posting to mysql


  • Please log in to reply
4 replies to this topic

#1 devilmyarse

devilmyarse
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 02 August 2006 - 11:30 PM

I'm using the windows installation of xampp, the newest version. and i'm quite literally stumped. I used to code php a while ago, started dabbling in it but then I got caught up in other things at the time so it took a back burner. Now I may be rusty but I just can't understand why I can't get my code to post to the database. I've tried on a tripod free hosted site and it still doesn't work. anyways heres the code.

<form method="post" action="guestbookpost.php">
<table cellpadding="6" cellspacing="0">
 <tr>
  <td>Name :</td>
  <td><input type="text" name="name" /></td>
 </tr>

 <tr>
  <td>Email :</td>
  <td><input type="text" name="email" /></td>
 </tr>

 <tr>
  <td valign="top">Message :</td>
  <td><textarea name="message" cols="30" rows="6"></textarea></td>
 </tr>

 <tr>
  <td> </td>
  <td>
  <input type="submit" name="submit" value="Add Message" />
  <input type="reset" name="reset" value="Clear Message" />
  </td>
 </tr>
</table>
</form>

<?php
$dbhost ='localhost';
$dbusername = '';//blanked for security
$dbpasswd = '';
$database_name = '';//blanked for security

if($_POST['submit'])
{
$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd")
	or die ("Couldn't connect to the database.");
$db = mysql_select_db("$database_name", $connection)
	or die("Couldn't select database.");
	  $sql[addpost] = mysql_query("INSERT INTO guestbook (name, email, message, date)
VALUES ('$_POST[name]','$_POST[email]','$_POST[message]',time())");

mysql_close($db);

echo "Thanks ".$_POST['name']." your message has been added\n";
include('guestbookentries.php');
}
?>

<?php
$dbhost ='localhost';
$dbusername = ''; //blanked for security
$dbpasswd = '';
$database_name = ''; //blanked for security


$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd")
	or die ("Couldn't connect to the database.");
$db = mysql_select_db("$database_name", $connection)
	or die("Couldn't select database.");

$sql[getPosts] = mysql_query("SELECT name, email, message, date, FROM guestbook order by id DESC LIMIT 10");

while($data = mysql_fetch_array($sql[getPosts]))
{
    $post_date = date('D F jS Y @ g:ia', $data[date]);

    echo "<div>On $post_date <a href=\"mailto:$data[email]\">$data[user]</a> said:</div>\n";
    echo "<div>$data[message]</div>\n\n";
}

mysql_close($db);

?>

The code gives me no errors, but it just won't insert the data into the database. I just don't know what the heck i've done wrong :| This happens on both the tripod site and my own installation. I've made sure that the username,database name, password etc are all correct. I'm just stumped. afaik this should work!!

Any help would be so greatfully appreciated!

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 02 August 2006 - 11:35 PM

change:

$sql[addpost] = mysql_query("INSERT INTO guestbook (name, email, message, date)
VALUES ('$_POST[name]','$_POST[email]','$_POST[message]',time())");

to:

$sql[addpost] = mysql_query("INSERT INTO guestbook (name, email, message, date)
VALUES ('$_POST[name]','$_POST[email]','$_POST[message]',time())") or die(mysql_error());

Then see if you are getting any errors.

#3 devilmyarse

devilmyarse
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 02 August 2006 - 11:38 PM

Ah of course. I forgot about putting or die at the end of my sql statement. It's given me this error message.

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '())' at line 2

EDIT: Just had a quick look at phpinfo() for tripod and it says the sql api version is 3.23.57 i can't find any manuals for that version so far...

#4 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 02 August 2006 - 11:41 PM

Change your query to:

$sql[addpost] = mysql_query("INSERT INTO guestbook (name, email, message, date) VALUES ('$_POST[name]','$_POST[email]','$_POST[message]'," . time() . ")") or die(mysql_error());

Or if you want to use mysql's date and time functions, look here:

http://dev.mysql.com...-functions.html

#5 devilmyarse

devilmyarse
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 02 August 2006 - 11:46 PM

Right that's fixed it! Thank you very much! I would've hacked away at that for weeks and not found the solution.

Must remember to add error reporting to every bit of code! Hehe!

Wahoo!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users