Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6203131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:51:35+00:00 2026-05-24T04:51:35+00:00

i’m new to php , i have been searching for a tutorial regarding inserting

  • 0

i’m new to php , i have been searching for a tutorial regarding inserting form’s input(text) , radio and selection data to MySQL database’s table using php. i found some tutorials but most are confusing. So i decided to ask.

Okay here’s what i want to do. I have a form which have two types of input and a selection
1. input type text
2. input type radio
3. selection

Here’s the HTML code :

<form action="" method="post" enctype="multipart/form-data">

  <strong>Your Name: </strong><br>
     <input type="text" name="myname" value="" />
  <br /><br/>    

  <strong>Which class type you want:</strong><br>
    <select name="selection">
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
    </select>

  <strong>Do you agree?</strong><br>
    <input type="radio" name="agree" value="Yes"> or 
    <input type="radio" name="agree" value="No">


  <input type="submit" name="submit" value="Submit">

</form>  

I have set the form action to blank because the php code will be in the same file as the HTML (it’s a php file btw)

MySQL table : info
structure :
1. name
2. class
3. agree

I want the php code to insert myname into name , selection’s selected data into class , radio selected data into agree

P/S Yes i have added a connect to database php script , i just want to know how to get the form data into mysql.

Can someone write a php code example on how can i do this?

Thanks and have a nice day . I hope i have provided enough information. Thanks again if you help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:51:36+00:00Added an answer on May 24, 2026 at 4:51 am

    I’ve taken it a little further here, there is still plenty more that can be done and many way’s to do it, for instance you could extend the $errors array to include a field id and then highlight the HTML form field so the user can see exactly where they went wrong.
    Considering your form is fairly simple you would not need this.
    @Shef’s code would certainly do the job but I thought you might be interested in some more.

        <?php
        // check the form has been submitted
        if (isset($_POST['submit'])) {
            // escape the form fields and assign them to variables
            // validate myname to ensure the user entered data
            if (isset($_POST['myname']) && $_POST['myname']!='') {
                $myname = mysql_real_escape_string($_POST['myname']);
            } else {
                // create an error variable array to store errors to display
                $errors[] = 'Please enter your name'; 
            }
    
            // no need to validate selection here as it alway's has a value
            $classtype = mysql_real_escape_string($_POST['selection']);
    
            // validate agree unless you want to add 'checked' to one of the values
            if (isset($_POST['agree']) && $_POST['agree']!='') {
                $agree = mysql_real_escape_string($_POST['agree']);
            } else {
                $errors[] = 'Please tell us if you agree?'; 
            }
    
            //if errors found tell the user else write and execute the query
            if ($errors) {
                $message = '<p class="error">We found a problem:</p><ul>';
                foreach($error as $msg){
                    $message .= '<li>'.$msg.'</li>';
                }
                $message .= '</ul><p>Please fix the error/s to continue.</p>';
            } else {
                // write the query
                $query = "INSERT INTO table (myname, classtype, agree) VALUES ";                           
                $query .= "('$myname','$classtype','$agree')"
                // run the query
                mysql_query($query);
                $message = '<p class="sucessful">Thanks '.htmlspecialchars($myname).'. Your selection has been saved.</p>';
            }
        }
    
        // print the message
        // show the variables in the form field so they don't need re-input
        if ($message!='') { echo $message; }
        ?>
        <form action="" method="post" enctype="multipart/form-data">
    
          <strong>Your Name: </strong><br>
            <input type="text" name="myname" value="<?php echo htmlspecialchars($myname) ?>" />
          <br /><br/>    
    
          <strong>Which class type you want:</strong><br>
            <select name="selection">
              <option value="A"<?php if ($classtype=='A') { echo ' selected'; } ?>>A</option>
              <option value="B"<?php if ($classtype=='B') { echo ' selected'; } ?>>B</option>
              <option value="C"<?php if ($classtype=='C') { echo ' selected'; } ?>>C</option>
            </select>
    
          <strong>Do you agree?</strong><br>
            <input type="radio" name="agree" value="Yes"<?php if ($agree=='Yes') { echo ' checked'; } ?>> or 
            <input type="radio" name="agree" value="No"<?php if ($agree=='No') { echo ' checked'; } ?>>
    
          <input type="submit" name="submit" value="Submit">
    
        </form> 
    

    Also: @sqwk, Don’t point people towards w3schools, see this: http://w3fools.com/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.