www.webdeveloper.com
Results 1 to 7 of 7

Thread: PHP variables

  1. #1
    Join Date
    Jun 2011
    Posts
    8

    PHP variables

    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>

  2. #2
    Join Date
    Jan 2011
    Posts
    62
    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,...)
    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

  3. #3
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by dirty_dillon View Post
    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>
    Are you getting any error messages?

  4. #4
    Join Date
    Jan 2011
    Posts
    62
    Quote Originally Posted by criterion9 View Post
    Are you getting any error messages?
    I can not run your code as I do not have the tables in my database to test against

    try changing
    PHP Code:
    mysql_query("INSERT INTO comments values ('0', '$note', '$ts')"); 
    to
    PHP Code:
    $query "INSERT INTO comments values ('0', '$note', '$ts')";
    print_r($query);
    mysql_query($query); 
    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

  5. #5
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    use $_POST['note'], not $note

  6. #6
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    Should be
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">

  7. #7
    Join Date
    Jun 2011
    Posts
    8
    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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles