1

I've been so out of touch with using PHP outside of content management systems that I've forgot some of the basic syntax etc. What I'm trying to build is a simple form that collects some user data and sends it to a database table called 'creathive_applications'.

Here is my HTML form:

                <form action="<?php bloginfo('home'); ?>" method="post">
                    <fieldset id="membershipform">                  
                        <ul class="clearfix">
                            <li id="li-status">
                                <span>I am a:</span>
                                <menu>
                                    <li><label for="student"><input disabled="disabled" type="radio" name="status" id="student" checked="checked" value="Graduate" /> Graduate</label></li>
                                    <li><label for="student2"><input disabled="disabled" type="radio" name="status" id="student2" value="Undergraduate" /> Undergraduate</label></li>
                                </menu>
                            </li>
                            <li id="li-firstname">
                                <label for="firstname">First Name</label> <input name="firstname" disabled="disabled" type="text" placeholder="First Name" id="firstname" title="First Name" />
                            </li>
                            <li id="li-lastname">
                                <label for="lastname">Last Name</label> <input name="lastname" disabled="disabled" type="text" placeholder="Last Name" id="lastname" title="Last Name" />
                            </li>
                            <li id="li-email">
                                <label for="email">Email address</label> <input name="email" disabled="disabled" type="text" placeholder="Email address" id="email" title="Email address" />
                            </li>
                            <li id="li-url">
                                <label for="url">URL</label> <input name="url" disabled="disabled" type="text" placeholder="URL of something you've made" id="url" title="URL of something you've made" />
                            </li>
                            <li id="li-buttons">
                                <input name="submit" type="submit" value="Send Application &#9658;" title="Send Application" onclick="alert('Invites available from March 2011');" />
                            </li>
                        </ui>
                    </fieldset>
                </form>

and here is the PHP jazz:

if(isset($_POST['submit']))

{

    $status = $_POST['status'];
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $url = $_POST['url'];

    $host = '####';
    $username = '####';
    $pass = '####';

    mysql_connect($host,$username,$pass);
    mysql_select_db($username);

    $query = "INSERT INTO creathive_applications VALUES (NULL,'".$status."','".$firstname."','".$lastname."','".$email."','".$url."')";

    $result = mysql_query($query);

}

Can anyone help me fix this as I can't remember how to run my SQL statement and send the data to the database :/ Also I've added the database username and password as well as host but what about the database name? Where does that go again?

I know this is a rather n00b question, but any help would be much appreciated.

THANKS A LOT

6
  • Are you getting any specific errors or anything? Commented Feb 18, 2011 at 15:56
  • 3
    mysql_select_db('database name'); ;) Commented Feb 18, 2011 at 15:57
  • @Brian No I haven't tried it yet because I'm not even sure what I'm doing as it's been so long since I did this manually. Commented Feb 18, 2011 at 15:59
  • @Cameron go ahead and try it and post the error . Commented Feb 18, 2011 at 15:59
  • I don't get an error. Probably because the query isn't being sent to the DB. Commented Feb 18, 2011 at 16:03

3 Answers 3

2

Well, with no error it's impossible to tell what to fix.
However, there are some obvious things to do.
See the comments:

<?
//show all possible errors. should be ALWAYS set to that level
error_reporting(E_ALL); 
echo "landed at form handler<br>";

// sometimes buttons not being sent or gets misspelled
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    echo "here goes POST processing<br>";
    $host = '####';
    $username = '####';
    $pass = '####';

    mysql_connect($host,$username,$pass);
    mysql_select_db($username);

    // all strings should be escaped
    // and it should be done after connecting to DB
    $status    = mysql_real_escape_string($_POST['status']);
    $firstname = mysql_real_escape_string($_POST['firstname']);
    $lastname  = mysql_real_escape_string($_POST['lastname']);
    $email     = mysql_real_escape_string($_POST['email']);
    $url       = mysql_real_escape_string($_POST['url']);

    $query = "INSERT INTO creathive_applications 
              VALUES (NULL,'$status','$firstname','$lastname','$email','$url')";

    echo $query;
    // always run your queries this way to be notified in case of error
    $result = mysql_query($query) or trigger_error(mysql_error().". Query: ".$query);
    var_dump($result);
}

in case you still see no error, add this line temporarily to the top of the script

ini_set('display_errors',1);

and remove it immediately after you get the problem solved.

EDIT added some debug info.
If you see none of it's messages, you're sending your form to the wrong URL. If you see some of them, post it here

Sign up to request clarification or add additional context in comments.

3 Comments

@Cameron how do you know? add some debug output. see edited code
Getting no errors BUT the data has appeared in the DB! :) The only change I made was leaving the action blank on the form. Not sure why that fixed it!
the if($_SERVER['REQUEST_METHOD'] == 'POST') helped me a great deal! thanks!
1

As Brian noted, you should post if any errors are happening.

The main thing I notice is that you are using variables that potentially have no values. Radio buttons do not send their values if they are not toggled.

Use something like this to cover your bases:

if(isset($_POST['status']))
{
    $status = $_POST['status'];
}

Don't know if that's your problem. Consider adding more information. :)

2 Comments

My problem is posting the SQL. how do I send the $query to the database? The radio buttons should be fine as I have one of them set to autocheck so the user has to choose one or the other right?
Good observation. It looks like your mysql_select_db line is incorrect, but other than that, I recommend you use a resource like this to walk through the process...and go ahead and try the page...see what happens :) w3schools.com/PHP/php_mysql_insert.asp
1
   if(count($_POST) > 0){
        $status = $_POST['status'];
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $email = $_POST['email'];
        $url = $_POST['url'];

        $host = '####';
        $username = '####';
        $pass = '####';

        $mydb = mysql_connect($host,$username,$pass);
       // mysql_select_db($username);  
        //are you using your username as db name?
        mysql_select_db("yourdbname");
        $query = "INSERT INTO creathive_applications VALUES (NULL,'".mysql_real_escape_string($status)."','".mysql_real_escape_string($firstname)."','".mysql_real_escape_string($lastname)."','".mysql_real_escape_string($email)."','".mysql_real_escape_string($url)."')";  //use mysql_real_escape_string for security

        $result = mysql_query($query);
        //show error if query fails.
        if (!$result) {
             die('Invalid query: ' . mysql_error());
        }
        mysql_close($mydb);  //close the connection
    }

add the mysql_error(); and see if it prints out errors

3 Comments

I'm not getting any errors when submitting. But the data isn't being sent to the database.
go ahead and do a print_r($_POST); first. what is the structure of creathive_applications
I have id(int), status(varchar100), firstname(varchar100), lastname(varchar100), email(varchar100), url(varchar100)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.