Jump to content

not posting to mysql


devilmyarse

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!
Link to comment
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.
Link to comment
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...
Link to comment
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
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.