dropdown other value not saving to database

2019-06-11 03:02发布

问题:

         <script type="text/javascript">
    function showfield(name){
      if(name=='Other')document.getElementById('div1').innerHTML='other: <input type="text" name="other" />';
      else document.getElementById('div1').innerHTML='';
    }
    </script>
      <html>
            <head> 
              <meta name="viewport" content="width=device-width, initial-scale=1.0">
                            <link href = "css/bootstrap.min.css" rel = "stylesheet">
              <title> Insert</title>
            </head>
            <body> 

              <h1>Insert Data In to Db</h1>
              <form action="teachersubmit.php" method="post" id="form">
                <input type="hidden" name="submitted" value ="true" />

             <label>  School Code: <input type="text" name="scode" /></label>
             <label>  Category: <input type="text" name="category" /></label>
            <label>   name: <input type="text" name="sname" /></label>
             <label>  Address: <input type="text" name="sadd"></label><br>
            <label>   name:  <select name="gender" id="gender">
              <option value="male">male</option>
 <option value="female">female</option>
                   <option value="Other">Other</option>
            </select></label>
           <div id="div1"></div>


            <button type="submit" form="form" value="Submit">Submit</button>
    </form>

        if (isset($_POST ['submitted']) ){
         $con = mysqli_connect("localhost","root","","school");
               if (mysqli_connect_errno())
          {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }



                if(!empty($_POST['other'])) 
{

   $sname=$_POST['sname'];
$scode=$_POST['scode'];
$category=$_POST['category'];
$sadd=$_POST['sadd'];
$gender=$_POST['other'];

  $sqlinsert= "INSERT INTO form1 (sname,scode,category,sadd,gender) VALUES ('$sname' , '$scode', '$category', '$sadd', '$gender')";

   if(!mysqli_query($con, $sqlinsert)){
    die ('not inserted');
    }}
    else{
$sname=$_POST['sname'];
$scode=$_POST['scode'];
$category=$_POST['category'];
$sadd=$_POST['sadd'];
$gender=$_POST['gender'];

    $sqlinsert= "INSERT INTO form1 (sname,scode,category,sadd,gender) VALUES ('$sname' , '$scode', '$category', '$sadd', '$gender')";

     if(!mysqli_query($con, $sqlinsert)){
        die ('not inserted');
         }

      }

Solved ..... thanks all of u for helping i just put the condition that if that hidden field is empty then take the dropdown value and if not then take the textbox value ......cheers :)


This is the database structure:

sname    varchar(100)   
category varchar(100)           
sadd     varchar(100)   
scode    varchar(20)
gender   varchar(100)   

回答1:

Value is missing in option that's the reason dropdown value not posting form form and not save into database

 <option>male</option>
 <option>female</option>

It would be

 <option value="male">male</option>
 <option value="female">female</option>

To check error in your query read

http://php.net/manual/en/mysqli.error.php

Your code is open for sql injection . Read this to prevent it form it

How can I prevent SQL injection in PHP?

EDITED

Data is not inserted because sname,scode,category,sadd are integer type in database and you are trying to insert character

ALter sname,scode,category,sadd filed as varchar into your database