Beginners pb with PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mlewis
    New Member
    • Sep 2008
    • 4

    Beginners pb with PHP

    Hello and thanks in advance for any help.

    I'm just starting out on PHP and read the very useful exercise at http://bytes.com/serversidescripting...ses/index.html

    I turned on debugging as suggested and on running I got the following error messages:
    Code:
    Notice: Undefined variable: id in C:\Program Files\EasyPHP 2.0b1\www\test\add2.php on line 27
    
    Notice: Undefined variable: name in C:\Program Files\EasyPHP 2.0b1\www\test\add2.php on line 27
    
    Notice: Undefined variable: email in C:\Program Files\EasyPHP 2.0b1\www\test\add2.php on line 27
    
    Notice: Undefined variable: opinion in C:\Program Files\EasyPHP 2.0b1\www\test\add2.php on line 27
    which relate to the line 27:
    [code=php]
    $sqlquery = "INSERT INTO $table VALUES('$id','$ name','$email', '$opinion')";
    [/code]

    So this suggests that I need to declare variables or that the html does not pass the data to the php script?

    My final result does not as expected write to the table "informatio n".

    Thanks for your help in debugging as this seems to be a good exercise in learning the basics of writing to mysql data tables.

    Regards

    Mark
    Last edited by Atli; Sep 9 '08, 09:37 AM. Reason: Added [code] tags.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    So this suggests that I need to declare variables or that the html does not pass the data to the php script?
    Your second guess is almost certainly the problem,
    Just relying on error reporting does not always reveal the picture.
    It is advisable to echo the values of your variables at various stages.
    [PHP]echo "<br>id $id name $name email $email opinion $opinion";[/PHP]

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi. Welcome to Bytes!

      The problem is that that tutorial is using an old version of PHP. (PHP 4, I would guess).

      In PHP5, value passed to PHP through HTML <input> elements must be retrieved from the $_POST array. (or $_GET if you used the GET protocol.)

      So, you would either have to create the variables you use in your query, like so:
      [code=php]
      $value = $_POST['value'];
      [/code]
      Or simply replace the values inside the query with the $_POST array, like so:
      [code=php]
      $query = "SELECT stuff FROM table WHERE value = '{$_POST['value']}'";
      [/code]
      I would recommend the former. Once you start using this is real applications you are going to want to validate the values before using them, so having them in variables is going to come in handy.

      Comment

      • mlewis
        New Member
        • Sep 2008
        • 4

        #4
        Got it thanks. This does the trick
        [code=php]
        <?php

        // turns on or off debugging
        error_reporting (E_ALL);
        ini_set('displa y_errors', true);

        // supposed to cause your script to halt when a MySQL query fails, and report the error message:
        //mysql_query($qu ery, $link_id) or die('<hr />MySQL Error: ' .mysql_error(). '<hr />');

        $DBhost = "127.0.0.1" ;
        $DBuser = "root";
        $DBpass = "";
        $DBName = "service_si te";
        $table = "informatio n";
        $name = $_POST["name"];
        $email = $_POST["email"];
        $opinion = $_POST ["opinion"];

        mysql_connect($ DBhost,$DBuser, $DBpass) or die("Unable to connect to database");

        mysql_select_db ("$DBName") or die("Unable to select database $DBName");

        $sqlquery = "INSERT INTO $table VALUES('id','$n ame','$email',' $opinion')";

        $results = mysql_query($sq lquery);

        mysql_close();

        print "<HTML><TIT LE> PHP and MySQL </TITLE><BODYBGCO LOR=\"#FFFFFF\" ><center><tab le border=\"0\"wid th=\"500\"><tr> <td>";
        print "<p><font face=\"verdana\ " size=\"+0\"> <center>You Just Entered This Information Into the Database<p><blo ckquote>";
        print "Name : $name<p> E-Mail : $email<p>Opinio n : $opinion</blockquote></td></tr></table></center></BODY></HTML>";

        ?>
        [/code]
        Thanks and Regards

        Mark
        Last edited by Atli; Sep 9 '08, 06:03 PM. Reason: Added [code] tags.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Originally posted by mlewis
          [PHP]$DBhost = "127.0.0.1" ;
          $DBuser = "root";
          $DBpass = "";
          $DBName = "service_si te";[/PHP]
          I recommend not using a root account for db access, this could crash your db when using an incorrect administrative query.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Mark,

            Please use &#91;code&#9 3; tags when posting code examples.

            &#91;code&#93;. . code goes here... &#91;/code&#93;

            Thanks.

            Comment

            Working...