将数据插入mysql数据库无法正常工作

时间:2012-08-03 07:17:25

标签: php html

我正在从iphone应用程序向mysql插入数据,但它没有将数据插入表中 我回显结果和输入值但它也没有显示任何值

HTML CODE对于页面

  <html>


  <head>
  <title>data to server</title>
  </head>
  <body>
  <form action="surveyAnswer.php" method="post" enctype="multipart/form-data"><br>

  <INPUT TYPE = "Text" VALUE ="1" NAME = "survey_question_response_id">

  <INPUT TYPE = "Text" VALUE ="1" NAME = "survey_id">
  <INPUT TYPE = "Text" VALUE ="1" NAME = "question_id">
  <INPUT TYPE = "Text" VALUE ="1" NAME = "survey_response_answer_id">
   <input type="submit" value="Upload File">
  </form>
  </body>
  </html>



     <?php
     $host = ""; 
     $user = ""; 
     $pass = ""; 
     $database = ""; 

     $linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 
     mysql_select_db($database, $linkID) or die("Could not find database."); 

     $survey_question_response_id=$_POST['survey_question_response_id'];
     $survey_id=$_POST['survey_id'];
     $question_id=$_POST['question_id'];
     $survey_response_answer_id=$_POST['survey_response_answer_id'];
     echo($survey_question_response_id);
     $query=("INSERT INTO survey_question_responses   (survey_question_response_id,survey_id,question_id,survey_response_answer_id)
     VALUES ('$survey_question_response_id', '$survey_id','$question_id','$survey_response_answer_id')");
     mysql_query($query,$con);
     printf("Records inserted: %d\n", mysql_affected_rows());
     echo($survey_id) 
    ?>

3 个答案:

答案 0 :(得分:1)

您的form方法为POST,您正在使用$_GET来捕获变量。使用$_POST代替$GET来解决您的问题。

另外,标签语法错误。

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

这指向surveyAnswer.php。因此,将以下代码放在surveyAnswer.php页面中,并将其从显示html表单的页面中删除。

    <?php

       $survey_question_response_id=$_POST['survey_question_response_id'];
       $survey_id=$_POST['survey_id'];
       $question_id=$_POST['question_id'];
       $survey_response_answer_id=$_POST['survey_response_answer_id'];
       $query=("INSERT INTO survey_question_responses     (survey_question_response_id,survey_id,question_id,survey_response_answer_id)
      VALUES ('$survey_question_response_id', '$survey_id','$question_id','$survey_response_answer_id')");
     mysql_query($query,$con);
     printf("Records inserted: %d\n", mysql_affected_rows());
     echo($survey_id);

 ?>

答案 1 :(得分:0)

你用 POST 来拍摄你的动作,但在你的PHP代码中你使用$ _GET

更改所有$ _GET:

 <?php
   $survey_question_response_id=$_POST['survey_question_response_id'];
   // OR eighter to
   $survey_question_response_id=$_REQUEST['survey_question_response_id'];
   ....

答案 2 :(得分:0)

在表单中,您有form method =“post”,当您使用GET存储值时,更改表单方法=“GET”或使用

$_POST['survey_question_response_id'];  

而不是

$_GET['survey_question_response_id'];

等其他变量也是如此。

你也错过了“;”这里“echo($ survey_id)”