/    Sign up×
Community /Pin to ProfileBookmark

Hey, im sorry if I have posted this in the wrong section but im new to both php and sql infact this was a tutorial that i copied word for word but to no avail.. It is a guestbook and from what I can gather the sql side of things is working ‘If I enter the data into sql it will display the results’ but the php will not post the variables to sql.

<html>
<head>
<title>THE GUESTBOOK</title>
</head>
<body>
<h1>Welcome to my Guestbook</h1>
<h2>Please write me a note below</h2>
<form action=”<?= “$PHP_SELF#results”?>” method=”POST”>
<textarea cols=40 rows=5 name=note wrap=virtual></textarea>
<input type=”submit” value=” send it “>

</form>

<?php
$conn = mysql_connect(‘localhost’, ‘root’, ‘password’);

mysql_select_db(‘mydb’);
if(isset($note)) {
$ts = date(“Y-m-d H:i:s”);

mysql_query(“INSERT INTO comments values (‘0’, ‘$note’, ‘$ts’)”);

}

?>
<h2>The entries so far:</h2>
<?php
$result = mysql_query(“SELECT * FROM comments”);
while ($row=mysql_fetch_row($result)) {
echo $row[0] . ” ” . $row[1] . ” ” . $row[2] . “<br> n”;
} ?>

</body>
</html>

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@fideltfgSep 21.2011 — I can not really see a problem with your code, but could I make a few recommendations...


if you have more than just the three columns in the table then you will need to format the query like this
[CODE]INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...) [/CODE]

its always good practice to do this anyway so your code is easier to understand.

Is the date being inserted in to a date, datetime, or timestamp column? You could let the database set the time for the post also instead of sending it in the query. set the colunm as a time stamp and set ON UPDATE CURRENT_TIMESTAMP
Copy linkTweet thisAlerts:
@criterion9Sep 21.2011 — Hey, im sorry if I have posted this in the wrong section but im new to both php and sql infact this was a tutorial that i copied word for word but to no avail.. It is a guestbook and from what I can gather the sql side of things is working 'If I enter the data into sql it will display the results' but the php will not post the variables to sql.

<html>

<head>

<title>THE GUESTBOOK</title>

</head>

<body>

<h1>Welcome to my Guestbook</h1>

<h2>Please write me a note below</h2>

<form action="<?= "$PHP_SELF#results"?>" method="POST">

<textarea cols=40 rows=5 name=note wrap=virtual></textarea>

<input type="submit" value=" send it ">

</form>

<?php

$conn = mysql_connect('localhost', 'root', 'password');


mysql_select_db('mydb');

if(isset($note)) {

$ts = date("Y-m-d H:i:s");

mysql_query("INSERT INTO comments values ('0', '$note', '$ts')");


}

?>

<h2>The entries so far:</h2>

<?php

$result = mysql_query("SELECT * FROM comments");

while ($row=mysql_fetch_row($result)) {

echo $row[0] . " " . $row[1] . " " . $row[2] . "<br> n";

} ?>

</body>

</html>[/QUOTE]

Are you getting any error messages?
Copy linkTweet thisAlerts:
@fideltfgSep 21.2011 — Are you getting any error messages?[/QUOTE]

I can not run your code as I do not have the tables in my database to test against

try changing
[code=php]mysql_query("INSERT INTO comments values ('0', '$note', '$ts')");
[/code]

to
[code=php]
$query = "INSERT INTO comments values ('0', '$note', '$ts')";
print_r($query);
mysql_query($query);
[/code]

this will ouput the query string to your page, then copy the query in to phpmyadmin, and run it there, it will tell you if there is a problem with the query.

other than that check your var/log/httpd/error.log file it may contain clues
Copy linkTweet thisAlerts:
@NedalsSep 21.2011 — use $_POST['note'], not $note
Copy linkTweet thisAlerts:
@DasherSep 22.2011 — Should be

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Copy linkTweet thisAlerts:
@dirty_dillonauthorSep 22.2011 — Hey Thanks for the Advice. I still have not got it working, these are the error messages I am getting:

PHP Notice: Undefined variable: PHP_SELF in /var/www/guestbook.php on line 8, referer: http://localhost/guestbook.php

PHP Stack trace:, referer: http://localhost/guestbook.php

PHP 1. {main}() /var/www/guestbook.php:0, referer: http://localhost/guestbook.php
×

Success!

Help @dirty_dillon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...