php form inserting drop down values to mysql database

Discussion in 'PHP' started by ianhaney, Nov 19, 2013.

  1. #1
    Hi

    I am struggling with a php form that inserts data to a mysql database

    I got it working ok with just text fields and a textarea field but now I want to add a dropdown menu top the form which I have done but I can't seem to get the form to insert any of the data, can someone help please

    I have posted the code below for my form

    <form action="add.php" method="post" name="form1">
      <table width="25%" border="0">
        <tr>
          <td>Job Title</td>
          <td><input type="text" name="jobtitle"></td>
        </tr>
        <tr>
        <td>Contract Type</td>
        <td>
        <select name="contracttype">
            <option name="contractype" value="temp">Temporary</option>
            <option name="contractype" value="perm">Permenant</option>
        </select>
        </td>
        </tr>
    <tr>
          <td>Location</td>
          <td><input type="text" name="location"></td>
        </tr>
    <tr>
          <td>Salary</td>
          <td><input type="text" name="salary"></td>
        </tr>
        <tr>
          <td>Description</td>
          <td><textarea type="text" name="description"></textarea></td>
        </tr>
        <tr>
          <td>Added By</td>
          <td><input type="text" name="addedby"></td>
        </tr>
    <tr>
          <td></td>
          <td><input type="submit" name="Submit" value="Add"></td>
        </tr>
    
      </table>
     
    </form>
    HTML:
    Below is the PHP processing code

    <?php
    include_once("config.php");
    
    if(isset($_POST['Submit']))
    {
    $jobtitle=$_POST['jobtitle'];
        $contracttype = $_POST["contractype"];
        $location=$_POST['location'];
        $salary=$_POST['salary'];
        $description=$_POST['description'];
        $addedby=$_POST['addedby'];
           
        // checking empty fields
        if(empty($jobtitle) || empty($contracttype) || empty($location) || empty($salary) || empty($description) || empty($addedby))
        {
            //if job title field is empty
            if(empty($jobtitle))
            {
                echo "<font color='red'>Job Title field is empty.</font><br/>";
            }
            //contracttype drop down
            if(empty($contracttype))
            {
            echo "<option value='".$select_query_array['contracttype_id']."'>".htmlspecialchars($select_query_array["contractype"])."</option>";
            }
            //if location field is empty
            if(empty($location))
            {
                echo "<font color='red'>Location field is empty.</font><br/>";
            }
            //if salary field is empty
            if(empty($salary))
            {
                echo "<font color='red'>Salary field is empty.</font><br/>";
            }
            //if description field is empty
            if(empty($description))
            {
                echo "<font color='red'>Description field is empty.</font><br/>";
            }
            //if added by field is empty
            if(empty($addedby))
            {
                echo "<font color='red'>Added By field is empty.</font><br/>";
            }
           
            //link to the previous page
            echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
        }
        else // if all the fields are filled (not empty)
        {   
            //insert data to database   
            $result=mysql_query("INSERT INTO sgrjobs(jobtitle,contractype,location,salary,description,addedby) VALUES('$jobtitle','$contracttype','$location','$salary','$description','$addedby')");
           
            //display success message
            echo "<font color='green'>Data added successfully.";
            echo "<br/><a href='index.php'>View Result</a>";
        }
    }
    ?>
    PHP:
    Thank you in advance, appreciate it

    Ian
     
    ianhaney, Nov 19, 2013 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    <option> tags should not have a name. Plus, your <select>'s name is "contracttype" but you're trying to access it with $_POST['contractype'] (missing "t").

    But worse, your code is horribly vulnerable to SQL injections. Please take a look at mysql_real_escape_string().
     
    nico_swd, Nov 19, 2013 IP
    sarahk likes this.