I'm building a guestbook. There's a few things I would like to have advice and hints on how to get working....please do me a dis-service and give me a solution as I want to figure it out on my own as I am learning. I might appreciate it if you could point me to a tutorial on how to accomplish the task I want.

  1. If a user hits the submit button, and a field isn't filled in - how do I say that it isn't filled it - left blank? maybe with a if/else statement? what is the word I need to now for being left "blank" used in php terminology..??

  2. Once the user submits the info in the forms, I want to data to be pushed into my MySQL database table called "guestbook" with the following fields of fullname, email, and location....and then outputted nicely on a php page for viewing all the people who have signed the guestbook.

Is this feasible and within my grasp as a php noob?

Thanks.

Adrian

    Hi Adrian,

    It sounds like you don't want me to actually tell you the code for your project. Instead I'll try to point you in the right direction.

    1. When a form is submitted, all of the form fields get passed to the next page as name-variable pairs. The name of the form field become the name of a variable, and the value become the value of the variable. If your form uses method=POST then on the next page you can access the variables like this:

    $_POST['fieldname']

    If a field is left blank, the variable will still be sent (in most cases) but it will be empty.

    Test to see if the value of the variable is empty.

    1. This is a very nice and simple project for someone just learning PHP / MySQL. For a good tutorial, why don't you search google or go to webmonkey. As you encounter problems, come on back and we'll answer your questions.

      Great book, gave me a great start (and not intended to be an insult). PHP and MySQL for dummies. This will tell you everything you need to know.

        OK - I've got objective number 1 figured out. Now I need to figure out how to complete objective number 2.

        I checked Webmonkey , but didn't see any references on how to push data from a form into a dataase.....

          what did you find?

          are you looking for how to do MySQL queries?

            This is what I have so far. What I want to learn is how to setup a the paremeters for the database in order to have data pushed into it from my form:

            My html form page:

            <html>
            <head>
            <title>Guestbook</title>
            </head>
            <body>
            <h2><center>Welcome</center></h2>
            <h3><center>Please sign the Guestbook</center></h3>
            
            <form action="guestresult.php" method="POST">
            
            Full Name: <input type="text" name="fullname"><br<br>
            E-Mail: <input type="text" name="email"><br><br>
            Location: <input type="text" name="location"><br><br>
            Comments: <textarea name="comments"></textarea><br><br>
            <input type="submit" value="Submit">
            
            <?php
            $user = "safe_mysqld";
            $pass = "";
            $db = "techigloo";
            
            $link = mysql_connect("localhost",$user,$pass);
            if (!$link) {
               die('Could not connect: ' . mysql_error());
            } else {
            echo 'Connected successfully';
            }
            
            mysql_close($link);
            ?>

            and my guestresult.php page...

            <html>
            <head>
            <title>Guestbook</title>
            </head>
            <body>
            
            <b>Here's the summary of what you inputted:</b>
            <br><br>
            
            <?php
            
            if (empty($fullname)) {
               echo "<font color=red><b>You didn't fill in your full name!</font>b</b><br><br>";
            // Evaluates as true because $var is set
            } else  {
               echo "Hi! $fullname.  Welcome to the best website in the 
            //world! - Techigloo.com<br><br>";   
            } if (empty($email)) { echo "<font color=red><b>You didn't fill in your e-mail address!</font></b><br><br>"; // Evaluates as true because $var is set } else { echo "Your e-mail address is $email<br><br>"; } if (empty($location)) { echo "<font color=red><b>You didn't fill in your location!</font></b><br><br>"; // Evaluates as true because $var is set } else { echo "Your location is $email<br><br>"; } echo "Here is what you had to say:<br><br>"; print "$comments<br><br>"; ?> <br> <A HREF="guestbook.php">Return to Guestbook Sign-up page</A> <br> <A HREF="guestview.php">View Guestbook</A> </body> </html>

            I have setup a database called techigloo.
            A table called guestbook with 3 tables called fullname, email, and location.

            Thanks.

            Adrian

              well first off, for each page that you interact with the database, you need to reconnect.

              as far as putting the information into the database:

              $query = "insert into $news (fullname,email,location,comments) values ('$fullname','$email','$location','$comments')";
              $result = mysql_query($query)
                      or die("couldn' execute query.");
              
                25 days later

                $query = "insert into $news

                Where did you get $news variable from???? The name of the database is techigloo and the name of the table is guestbook....so what would I replace $news with??

                Thanks.

                  INSERT INTO <tablename>

                  so replace $news with the name of your table.

                    OK....I figured out that $news is supposed to be the table name.....so when I change it to the following, and execute my code, it can't put the data from the form into the database......below is the code I was told (and I tweaked) to put in to put the data from my forms in my database -

                    $query = "insert into guestbook (fullname,email,location,
                    comments) values ('$fullname','$email','$location',
                    '$comments')";
                    $result = mysql_query($query)
                            or die("couldn' execute query.");

                    The above code I put just above the closing php tag of my guestresult.php page....once again, it gives me the error - "couldn't execute query". Am I missing something??

                    Thanks.

                      It usually helps to make the error messages more verbose, when debugging:

                      $query = "INSERT INTO `guestbook`
                      	(`fullname`,`email`,`location`,`comments`)
                      	VALUES ('$fullname','$email','$location','$comments')";
                      $result = mysql_query($query)
                      	OR die(mysql_error());

                        Woot! Thanks laserlight - I didn't get that error message anymore - but I now get a new one! - "no database selected".

                        Thanks for the help so far...

                          I've added the variable:

                          $db = "techigloo";

                          plus the following code to select the database"

                          $connection = mysql_select_db($db, $link);

                          but it is still giving the message - "no database selected"

                            Test, e.g.

                            $link = mysql_connect("localhost",$user,$pass)
                            	OR die(mysql_error());
                            mysql_select_db("techigloo", $link)
                            	OR die(mysql_error());
                            mysql_query("SELECT `fullname`,`email`,`location`,`comments` FROM `guestbook`")
                            	OR die(mysql_error());

                              alright - that worked! But now it is giving me the following error:

                              Access denied for user: '@' to database 'techigloo'

                              I gave root all the permissions to the database - I am doing this locally on my linux laptop - both developement and viewing.

                                Write a Reply...