我无法使用从HTML表单获取的数据运行INSERT INTO函数

时间:2013-12-21 11:15:21

标签: php html

这是我的HTML表单:

<form id="ajax-contact-form" action="upload.php" method = "post" enctype="multipart/form-data" >

                <div class="col1">

                  <div class="clear"></div>
                  <br><br>
                  <INPUT type="text" name="Name" value="Full name:" onBlur="if(this.value=='') this.value='Full name:'" 
                            onFocus="if(this.value =='Full name:' ) this.value=''">
                  <div class="clear"></div>

                  <INPUT type="text" name="Email" value="Your email:" onBlur="if(this.value=='') this.value='Your email:'" 
                            onFocus="if(this.value =='Your email:' ) this.value=''"><div class="clear"></div>

                  <INPUT type="text" name="University" value="University, College or Institute:" onBlur="if(this.value=='') this.value='University, College or Institute:'" 
                            onFocus="if(this.value =='University, College or Institute:' ) this.value=''">
                  <div class="clear"></div></div>

                  <div class="col1 pad_left5"><br>
                  <h4>Please write about your academic research experiences:</h4>
                  <TEXTAREA NAME="Content" onBlur="if(this.value=='') this.value=''" 
                          onFocus="if(this.value =='' ) this.value=''"></TEXTAREA>
                  <div class="clear"></div></div>
                  <div class="clear"></div>


                  <br><h4>Upload your photo (200 x 200):</h4><br>
                  <INPUT type = "file" name="Picture" id = "picture" lang="en">
                  <div class="clear"></div><br>

                  <h4>Please upload your academic publications under one folder as a compressed file:</h4><br>
                  <INPUT type = "file" name="Publications" id = "Publication" lang="en">
                  <div class="clear"></div><br>

                  <div class="box">
                  <div class="services1">
                  <h4>GPA</h4><br>
                  <INPUT class="capthca" type="text" name="GPA" value="GPA:" onBlur="if(this.value=='') this.value='GPA:'" 
                            onFocus="if(this.value =='GPA:' ) this.value=''">

                  <div class="clear"></div><br>

                  <h4>IELTS</h4><br>
                  <INPUT class="capthca" type="text" name="IELTS" value="IELTS Score:" onBlur="if(this.value=='') this.value='IELTS Score:'" 
                            onFocus="if(this.value =='IELTS Score:' ) this.value=''">
                  <div class="clear"></div>
                  </div>

                  <div class="services1">
                  <h4>TOEFL</h4><br>
                  <INPUT class="capthca" type="text" name="PBT" value="PBT:" onBlur="if(this.value=='') this.value='PBT:'" 
                            onFocus="if(this.value =='PBT:' ) this.value=''"> <div class="clear"></div>

                  <INPUT class="capthca" type="text" name="CBT" value="CBT:" onBlur="if(this.value=='') this.value='CBT:'" 
                            onFocus="if(this.value =='CBT:' ) this.value=''"> <div class="clear"></div>

                  <INPUT class="capthca" type="text" name="IBT" value="IBT:" onBlur="if(this.value=='') this.value='IBT:'" 
                            onFocus="if(this.value =='IBT:' ) this.value=''"> <div class="clear"></div>


                  <div class="clear"></div> 
                  </div>

                  <div class="services1 last">
                  <h4>ALES</h4><br>
                  <INPUT class="capthca" type="text" name="VERBAL" value="Verbal Score:" onBlur="if(this.value=='') this.value='Verbal Score:'" 
                            onFocus="if(this.value =='Verbal Score:' ) this.value=''"> <div class="clear"></div>

                  <INPUT class="capthca" type="text" name="MATH" value="Math Score:" onBlur="if(this.value=='') this.value='Math Score:'" 
                            onFocus="if(this.value =='Math Score:' ) this.value=''"> <div class="clear"></div>

                  <INPUT class="capthca" type="text" name="EQUALWEIGHT" value="Equal Weight Score:" onBlur="if(this.value=='') this.value='Equal Weight Score:'" 
                            onFocus="if(this.value =='Equal Weight Score:' ) this.value=''"> <div class="clear"></div>
                  </div>          
                  <div class="clear"></div> 
                  </div>     
                  <div class="clear"></div>
                  <INPUT class="submit" type="submit" name="submit" value="submit">
                  <div class="clear"></div>  
                  </form>

我为HTML表单的值指定的其他变量。

$Name = (string)$_POST["Name"];
                  $EMail = (string)$_POST["Email"];
                  $University = (string)$_POST["University"];
                  $Academic_Experience = $_POST["Content"];
                  $Photo = $_POST["Picture"];
                  $Publications = $_POST["Publications"];
                  $GPA = (float)$_POST["GPA"];
                  $VERBAL = (float)$_POST["VERBAL"];
                  $MATH = (float)$_POST["MATH"];
                  $EQUALWEIGHT = (float)$_POST["EQUALWEIGHT"];
                  $PBT = (float)$_POST["PBT"];
                  $CBT = (float)$_POST["CBT"];
                  $IBT = (float)$_POST["IBT"];
                  $IELTS = (float)$_POST["IELTS"];

但是当我尝试使用代码

将数据插入数据库时
$record = mysql_query("INSERT INTO Students (NAME, EMAIL, PHOTO, GPA, ALES_VERBAL_SCORES, ALES_MATH_SCORE, ALES_EQUALWEIGHT_SCORES, TOEFL_PBT, TOEFL_CBT, TOEFL_IBT, IELTS, UNIVERSITY, EXPERIENCE, PUBLICATIONS) VALUES ("."$Name".", "."$EMail".", "."$FileName".", $GPA, $VERBAL, $MATH, $EQUALWEIGHT, $PBT, $CBT, $IBT, $IELTS, "."$University".", "."$Academic_Experience".", "."$PublicationsFileName".")");

我的代码有问题。它不会将数据插入数据库。如果你帮助我,我会很高兴的。 感谢...

3 个答案:

答案 0 :(得分:1)

值必须在单引号'$ Name'`

 "INSERT INTO Students (NAME, EMAIL, PHOTO, GPA, ALES_VERBAL_SCORES, 
ALES_MATH_SCORE,ALES_EQUALWEIGHT_SCORES,TOEFL_PBT, 
TOEFL_CBT, TOEFL_IBT, IELTS, UNIVERSITY,EXPERIENCE, PUBLICATIONS) 
        VALUES ('$Name', '$EMail', '$FileName', 
        '$GPA', '$VERBAL', '$MATH', 
        '$EQUALWEIGHT', '$PBT', '$CBT', '$IBT',
     '$IELTS', '$University', '$Academic_Experience', '$PublicationsFileName')"

答案 1 :(得分:0)

看起来你错过了一些(很多)报价。尝试这种更易读的方法

$record = mysql_query("INSERT INTO Students (NAME, EMAIL, GPA, 
    ALES_VERBAL_SCORES, ALES_MATH_SCORE, ALES_EQUALWEIGHT_SCORES, TOEFL_PBT, TOEFL_CBT,
    TOEFL_IBT, IELTS, UNIVERSITY, EXPERIENCE) VALUES 
    ('$Name', '$EMail', $GPA, $VERBAL, $MATH, $EQUALWEIGHT, 
    $PBT, $CBT, $IBT, $IELTS, '$University', '$Academic_Experience')");

您无法使用$Photo = $_POST["Picture"];以不同方式处理文件上传。我从查询中删除了PHOTOPUBLICATIONS

警告: mysql_函数自PHP 5.5.0起已弃用,将来会被删除。相反,应使用MySQLiPDO_MySQL扩展名。

警告:您的代码很容易出现SQL注入,请尝试使用Prepared语句。 (至少正确地逃避字符串)

答案 2 :(得分:-1)

您没有在查询中将字符串包装在引号中。 试试这个:

$record = mysql_query(
    "INSERT INTO Students (
          NAME, EMAIL, PHOTO, 
          GPA, ALES_VERBAL_SCORES, ALES_MATH_SCORE, ALES_EQUALWEIGHT_SCORES, 
          TOEFL_PBT, TOEFL_CBT, TOEFL_IBT, IELTS, 
          UNIVERSITY, EXPERIENCE, PUBLICATIONS
     ) VALUES (
          '$Name', '$EMail', '$FileName', 
          $GPA, $VERBAL, $MATH, $EQUALWEIGHT, 
          $PBT, $CBT, $IBT, $IELTS, 
          '$University', '$Academic_Experience', '$PublicationsFileName'
     )"
);

作为一个好的调试实践,总是将生成的查询打印出来,将其复制并在phpMyAdmin控制台或mysql CLI中运行,如果错误不是很明显的话。

例如,如果你这样做,你会立即看到没有用引号括起来的字符串。

希望有所帮助。