为什么mysql INSERT表示错误?

时间:2017-05-18 15:15:44

标签: php mysql

我有一个简单的HTML表单和PHP代码,用于将数据插入到sql数据库中。 但它运作不正常。它不是在我想要的地方存储数据。就像,它正在将名字放在Bangla" in" Name"并安排"学校"在" position2"。 现在如何使这项工作?代码有什么问题吗?

HTML代码:

<form name="information_form" enctype="multipart/form-data" method="post" action="process-new-member.php">
        <center>
                <table class="controls" >
                        <tr>
                            <td>Unique ID:</td> 
                            <td><input type="number" name="id" placeholder="Unique ID Number">
                        </tr>
                        <tr>
                            <td>Name:</td> 
                            <td><input type="text" name="name" placeholder="Write Full Name">
                        </tr>
                        <tr>
                            <td>Name in Bangla:</td> 
                            <td><input type="text" name="namebangla" placeholder="Write Full Name in বাংলা">
                        </tr>
                        <tr>
                                <td>Image</td>
                                <td><input name="img" type="file"></td>
                          </tr>
                        <tr>
                            <td>Father's Name:</td> 
                            <td><input type="text" name="father" placeholder="Father's name">
                        </tr>
                        <tr>
                            <td>Mother's Name:</td> 
                            <td><input type="text" name="mother" placeholder="Mother's name">
                        </tr>
                        <tr>
                            <td>Date of Birth:</td> 
                            <td>    
                                    <input class="inputnumber"  type="number" name="dobDATE" placeholder="Date" style="max-width: 46px;">
                                    <input class="inputnumber"  type="number" name="dobMONTH" placeholder="Month" style="max-width: 60px;">
                                    <input class="inputnumber"  type="number" name="dobYEAR" placeholder="Year" style="max-width: 57px;">
                            </td>
                        </tr>
                        <tr>
                                <td>Facebook ID: </td>
                                <td><input type="text" maxlength="100" name="fbid" placeholder="Write Username or ID Number"></td>
                        </tr>
                        <tr>
                                <td>Email: </td>
                                <td><input type="email" name="email" maxlength="100" placeholder="Write Email Address"></td>
                        </tr>
                        <tr>
                                <td>Contact No: </td>
                                <td>+880<input type="number" name="contact" maxlength="10" placeholder="Write Personal Phone No."></td>
                        </tr>
                        <tr>
                                <td>Emergency Contact No: </td>
                                <td>+880<input type="number" name="Econtact" maxlength="10" placeholder="Write Emergency Phone No."></td>
                        </tr>
                        <tr>
                                <td>Gender: </td>
                                <td>
                                    <select name="sex" >
                                        <option value="male">Male</option>
                                        <option value="female">Female</option>
                                    </select>
                                    </td>
                        </tr>

                        <tr>
                                <td>Editor Status: </td>
                                <td>
                                    <select name="editorstatus" >
                                        <option  value="0">No</option>
                                        <option  value="1">Yes</option>
                                    </select>
                                    </td>
                        </tr>

                        <tr>
                            <td>Present Address:</td> 
                            <td><input type="text" name="presentAdd" placeholder="Write Present Address">
                        </tr>
                        <tr>
                            <td>Permanent Address:</td> 
                            <td><input type="text" name="permanentAdd" placeholder="Write Permanent Address">
                        </tr>
                        <tr>
                            <td>Position: </td>
                            <td><input type="text" name="position" placeholder="Write Position in Rupok"></td>
                        </tr>
                        <tr>
                            <td>Secondary Position: </td>
                            <td><input type="text" name="position2" placeholder="Write Secondary Position"></td>
                        </tr>
                        </br>

                        <tr><td><h2>Educational Infos:</h2></td></tr>

                        <tr>
                            <td>School: </td>
                            <td><input type="text" name="school" placeholder="Write School Name"></td>
                        </tr>
                        <tr>
                            <td>College: </td>
                            <td><input type="text" name="college" placeholder="Write College Name"></td>
                        </tr>
                        <tr>
                            <td>Pass Years :</td> 
                            <td>    
                                    <input class="inputnumber" min="2000" max="2040" type="number" name="jscYear" placeholder="JSC" style="max-width: 46px;">
                                    <input class="inputnumber" min="2000" max="2040" type="number" name="sscYear" placeholder="SSC" style="max-width: 60px;">
                                    <input class="inputnumber" min="2000" max="2040" type="number" name="hscYear" placeholder="HSC" style="max-width: 57px;">
                            </td>
                        </tr>

                        <tr><td><h2>Others Infos:</h2></td></tr>

                        <tr>
                            <td>Personal Skills: </td> 
                            <td>
                                <textarea name="PersonalSkills" placeholder="Write down skills seperating by Comas"></textarea>
                            </td>
                        </tr>

                </table>
        </center>

      </br>

      <center>
        <button type="submit">Submit Data</button>
      </center

    </form> 

这是PHP代码:

<?php

$uploaddir = '../../img/members/';
$uploadfile = $uploaddir . basename($_FILES['img']['name']);
if (move_uploaded_file($_FILES['img']['tmp_name'], $uploadfile)) {
  echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Upload failed";
}

require('connect_db.php');

$id=$_POST['id'];
$name=$_POST['name'];
$namebangla=$_POST['namebangla'];
$pic = $_FILES['img']['name'];
$father=$_POST['father'];
$mother=$_POST['mother'];
$dobDATE=$_POST['dobDATE'];
$dobMONTH=$_POST['dobMONTH'];
$dobYEAR=$_POST['dobYEAR'];
$fbid=$_POST['fbid'];
$email=$_POST['email'];
$presentAdd=$_POST['presentAdd'];
$permanentAdd=$_POST['permanentAdd'];
$sex=$_POST['sex'];
$contact=$_POST['contact'];
$Econtact=$_POST['Econtact'];
$position=$_POST['position'];
$position2=$_POST['position2'];
$school=$_POST['school'];
$college=$_POST['college'];
$jscYear=$_POST['jscYear'];
$sscYear=$_POST['sscYear'];
$hscYear=$_POST['hscYear'];
$editor=$_POST['editorstatus'];
$PersonalSkills=$_POST['PersonalSkills'];



 $query ="Insert Into `member` Values
      (
       '$id',
       '$name',
       '$namebangla',
       '$father',
       '$mother',
       '$dobDATE',
       '$dobMONTH',
       '$dobYEAR',
       '$fbid',
       '$email',
       '$presentAdd',
       '$permanentAdd',
       '$sex',
       '$contact',
       '$Econtact',
       '$position',
       '$position2',
       '$school',
       '$college',
       '$jscYear',
       '$sscYear',
       '$hscYear',
       '$editor',
       '$PersonalSkills',
       '$pic'
      )";

      if(mysql_query($query)){

            header('location: show-data.php');

        }else{
            die(mysql_error());
        }
?>

1 个答案:

答案 0 :(得分:2)

表中字段的顺序可能与您提供值的顺序不同。如果您列出要插入数据的字段,那将会更加简单和安全:

Insert Into `member` (
               id,
               name,
               namebangla,
               father,
               mother,
               dobDATE,
               dobMONTH,
               dobYEAR,
               fbid,
               email,
               presentAdd,
               permanentAdd,
               sex,
               contact,
               Econtact,
               position,
               position2,
               school,
               college,
               jscYear,
               sscYear,
               hscYear,
               editor,
               PersonalSkills,
               pic
              )
Values
      (
       '$id',
       '$name',
       '$namebangla',
       '$father',
       '$mother',
       '$dobDATE',
       '$dobMONTH',
       '$dobYEAR',
       '$fbid',
       '$email',
       '$presentAdd',
       '$permanentAdd',
       '$sex',
       '$contact',
       '$Econtact',
       '$position',
       '$position2',
       '$school',
       '$college',
       '$jscYear',
       '$sscYear',
       '$hscYear',
       '$editor',
       '$PersonalSkills',
       '$pic'
      )"

我刚从变量中删除了$和单引号,假设这些是字段名称。如果它们不同,您需要提供实际的字段名称。