Jump to content

Archived

This topic is now archived and is closed to further replies.

devilmyarse

not posting to mysql

Recommended Posts

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.

[code="guestbook.php"]<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>
[/code]

[code="guestbookpost.php"]<?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');
}
?>
[/code]

[code="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);

?> [/code]

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!

Share this post


Link to post
Share on other sites
change:

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

to:

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

Then see if you are getting any errors.

Share this post


Link to post
Share on other sites
Ah of course. I forgot about putting or die at the end of my sql statement. It's given me this error message.

[code]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 [/code]

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...

Share this post


Link to post
Share on other sites
Change your query to:

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

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

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

×

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.