Insert into MySQL Database with PHP POST Method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ctrap
    New Member
    • Dec 2007
    • 2

    Insert into MySQL Database with PHP POST Method

    Hi Everyone,

    Here's my situation: I'm making a website for an employment agency, there is a page where potential employees can visit and "pre-register". When the submit button is hit the information is supposed to be stored in a MySQL database. Number one, when I hit submit nothing happens. I have to manually execute the PHP script. When the PHP script is executed it inserts a blank record into the database and I get my Thank You message. I realize my dilemma is pretty basic and has probably been answered several times, I have searched the forum and tried different pieces code but nothing seems to work for me. I've tried code using $_POST in front of the variables but it just leads to more errors and confusion. I'm using PHP5 and MySQL Server 5.0. Here is my HTML code:

    <code>
    <form action="AddAppl icant.php" method="POST">
    <font face="Times New Roman", size="3", color="151B8D">
    <b>*First Name: <input type="text" name="firstname " id="firstname" >
    *Last Name: <input type="text" name="lastname" id="lastname">< br><Br>
    *Address: <input type="text" name="address" id="address" size="25">
    *City: <input type="text" name="city" id="city"><br>< br>
    *State: <input type="text" name="state" id="state" size="1">
    *Zip Code: <input type="text" name="zipcode" id="zipcode" size="5">
    *Phone: <input type="text" name="phono" id="phono"><br> <br>
    Alternate Phone: <input type="text" name="altphono" id="altphono">
    E-mail: <input type="text" name="email" id="email"><br> <br>
    *Objective</b><br>Provide a brief description of the type of employment you are seeking.<br>
    <Textarea name="objective " rows="5" cols="50" id="objective"> </textarea><br><b r>

    <b>*Education </b><br>Select One<br>
    <select name="education " "id="education" >
    <option value="High School Diploma">High School Diploma</option>
    <option value="College Credits">Colleg e Credits</option>
    <option value="Associat es Degree">Associa tes Degree</option>
    <option value="Bachelor s Degree">Bachelo rs Degree</option>
    <option value="Past Grad Creidts">Past Grad Credits</option>
    <option value="Masters Degree">Masters Degree</option>
    <option value="Other">O ther</option></select><br><br>


    <b>Additional Certifications, Seminars, & Licenses<br>
    <Textarea name="certnsems " rows="3" cols="50" "id="certnsems" ></textarea><br><b r>

    *Work Experience</b><br>
    Describe your work experience or copy and paste your resume.
    <Textarea name="workexp" rows="7" cols="50" id="workexp"></textarea><br><b r>

    <center><inpu t type="submit" name="submit" value="Submit"> </center><br><br>
    </form>
    </code>

    Here is my PHP code:

    <code>
    <body>
    <?php

    $host="localhos t";
    $username="root ";
    $password="363m arket";
    $db_name="svesa pplicants";
    $tbl_name="appl icants";

    global
    $firstname, $lastname, $address, $city, $state, $zipcode, $phono, $altphono, $email, $objective, $education, $certnsems, $workexp;

    mysql_connect(" $host", "$username" , "$password" )or die("Cannot Connect to Server");
    mysql_select_db ("$db_name") or die("cannot select DB");

    $sql="INSERT INTO $tbl_name(first name, lastname, address, city, state, zipcode, phono, altphono, email, objective, education, certnsems, workexp)
    VALUES('$firstn ame', '$lastname', '$address', '$city', '$state', '$zipcode', '$phono', '$altphono', '$email', '$objective', '$education', '$certnsems', '$workexp')";
    $result=mysql_q uery($sql);


    if($result){

    echo "Thank You";
    }
    else {
    echo "ERROR";
    }

    mysql_close();
    ?>

    </body><html>
    </code>

    As you can tell I'm not an advanced programmer, I would truly appreciate any help or suggestions. :) Thanks in advance. ctrap
  • luttkens
    New Member
    • Jul 2007
    • 23

    #2
    Hi!

    I think this is your problem.

    Code:
    global
    $firstname, $lastname, $address, $city, $state, $zipcode, $phono, $altphono, $email, $objective, $education, $certnsems, $workexp;
    As far as I understand you think that $firstname will contain the value from the textbox 'firstname'. It doesn't work like this. Change the following code

    Code:
    VALUES('$firstname', '$lastname', '$address', '$city', '$state', '$zipcode', '$phono', '$altphono', '$email', '$objective', '$education', '$certnsems', '$workexp')";
    ... with this code ...
    Code:
    VALUES($_POST['firstname'], $_POST['lastname'], $_POST['address'], $_POST['city'], $_POST['state'], $_POST['zipcode'], $_POST['phono'], $_POST['altphono'], $_POST['email'], $_POST['objective'], $_POST['education'], $_POST['certnsems'], $_POST['workexp'])";
    Good luck!

    Comment

    • ctrap
      New Member
      • Dec 2007
      • 2

      #3
      Hi there,

      Thanks so much for the reply, I have tried the code you suggested and it gives me this error (line 23 being the VALUES line):

      PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_ WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\SVES\SVES Website\- on line 23

      When I put the single quotes around the entire piece of code i.e. (I've seen examples of code in both formats):

      '$_POST[firstname]' as opposed to $_POST['firstname']

      I get this error message for each variable, firstname, lastname, address, and so on:

      Thank You PHP Notice: Undefined index: firstname in C:\SVES\SVES Website\- on line 23 PHP

      Still not sure what's going on. Thanks again!

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        First, you haven't seen this anywhere
        Code:
        '$_POST[firstname]' as opposed to $_POST['firstname']
        It is better practice to copy POST values into variables and validate them.
        [PHP]$firstname = $_POST['firstname'];
        //Check firstname is valid etc[/PHP]Then insert using the variables.
        You have got a syntax error because the POST variables are in an array, so cannot be parsed.
        The same error can occur echoing out arrays.
        The way around it is to use curly braces
        Also text values need quotes around them when inserting.
        I think this is your confusion.
        So try this
        [PHP]VALUES('{$_POST['firstname']}','{$_POST['lastname']}', '{$_POST['address']}'... etc";[/PHP]

        Comment

        Working...