www.webdeveloper.com
Results 1 to 4 of 4

Thread: Php inserting form data to mysql issue

Hybrid View

  1. #1
    Join Date
    Apr 2012
    Posts
    39

    Php inserting form data to mysql issue

    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

    HTML Code:
    <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>
    Below is the PHP processing code

    PHP 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>";
        }
    }
    ?>
    Thank you in advance, appreciate it

    Ian

  2. #2
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,125
    You're mixing the name. You have both "contractype" and "contracttype."

    1 "t" vs. 2 "t's"
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  3. #3
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,125
    Ah, maybe not. Don't give "name" to your options though. Only have it on the select.
    Code:
    <select name="contracttype">
            <option value="temp">Temporary</option>
            <option value="perm">Permenant</option>
    </select>
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  4. #4
    Join Date
    Apr 2012
    Posts
    39
    Hi all

    Thank you for all the replies and help, appreciate it

    Got it working now, thank you appreciate it

    Kind regards

    Ian

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles