2012-11-17 132 views
1

我試圖做一個星級評分系統,我無法將結果插入到MySQL中。我錯過了一些東西,但我不知道是什麼。我會告訴你我的代碼,也許你可以看看問題在哪裏。 在此先感謝!PHP,Mysql插入

首先,我的表格:

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<script src='jquery.js' type="text/javascript"></script> 
<script src='documentation/documentation.js' type="text/javascript"></script> 
<link href='documentation/documentation.css' type="text/css" rel="stylesheet"/> 
<script type="text/javaScript" src="documentation/chili/jquery.chili-2.0.js"></script> 
<script type="text/javascript">try{ChiliBook.recipeFolder="documentation/chili/"}catch(e){}</script> 
<script src='jquery.MetaData.js' type="text/javascript" language="javascript"></script> 
<script src='jquery.rating.js' type="text/javascript" language="javascript"></script> 
<link href='jquery.rating.css' type="text/css" rel="stylesheet"/> 
</head> 

<body> 
<div id="tab-Testing"> 
<script> 
$('form1').submit(function() { 
var ans=$(this).serialize(); 
    $.ajax({ 
    type: "POST", 
    url: "starsubmit.php", 
    data:ans 
}).done(function(msg) { 
alert("Data Saved: " + msg); 
}); 
return false; 
}); 
</script> 

<div class="Clear">&nbsp;</div> 
<form id="form1"> 

     Question 1:bla bla bla 

    <input class="star required" type="radio" name="Question 1" value="1"/> 
    <input class="star" type="radio" name="Question 1" value="2"/> 
    <input class="star" type="radio" name="Question 1" value="3"/> 
    <input class="star" type="radio" name="Question 1" value="4"/> 
    <input class="star" type="radio" name="Question 1" value="5"/> 
    </div> 
    <br/> 
    <div class="Clear"> 

     Question 2:bla bla bla 

    <input class="star required" type="radio" name="Question 2" value="1"/> 
    <input class="star" type="radio" name="Question 2" value="2"/> 
    <input class="star" type="radio" name="Question 2" value="3"/> 
    <input class="star" type="radio" name="Question 2" value="4"/> 
    <input class="star" type="radio" name="Question 2" value="5"/> 
    </div> 
    <br/> 
    <div class="Clear"> 

     Question 3:bla bla bla 

    <input class="star required" type="radio" name="Question 3" value="1"/> 
    <input class="star" type="radio" name="Question 3" value="2"/> 
    <input class="star" type="radio" name="Question 3" value="3"/> 
    <input class="star" type="radio" name="Question 3" value="4"/> 
    <input class="star" type="radio" name="Question 3" value="5"/> 
    </div> 
<br> 
    <div class="Clear"> 

     Question 4:bla bla bla 

    <input class="star required" type="radio" name="Question 4" value="1" /> 
    <input class="star" type="radio" name="Question 4" value="2" /> 
    <input class="star" type="radio" name="Question 4" value="3" /> 
    <input class="star" type="radio" name="Question 4" value="4" /> 
    <input class="star" type="radio" name="Question 4" value="5" /> 
    </div> 
    <br/> 
    <div class="Clear"> 

     Question 5:bla bla bla 

    <input class="star required" type="radio" name="Question 5" value="1"/> 
    <input class="star" type="radio" name="Question 5" value="2"/> 
    <input class="star" type="radio" name="Question 5" value="3"/> 
    <input class="star" type="radio" name="Question 5" value="4"/> 
    <input class="star" type="radio" name="Question 5" value="5"/> 
    </div> 
    <br/> 
    <div class="Clear"> 

     Question 6:bla bla bla 

    <input class="star required" type="radio" name="Question 6" value="1" /> 
    <input class="star" type="radio" name="Question 6" value="2" /> 
    <input class="star" type="radio" name="Question 6" value="3" /> 
    <input class="star" type="radio" name="Question 6" value="4" /> 
    <input class="star" type="radio" name="Question 6" value="5" /> 
    </div> 

    <input type="submit" value="Submit scores!" /> </td> 

</form> 
</body> 
</html> 

現在,我的starsubmit.php:

<?php 
$mysql_hostname = "localhost"; 
$mysql_user = "root"; 
$mysql_password = ""; 
$mysql_database = "cosmote"; 
$prefix = ""; 
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong"); 
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong"); 
if($_SERVER["REQUEST_METHOD"] == "POST") 
{ 
$question1 = mysql_real_escape_string($_POST['question1']); 
$question2 = mysql_real_escape_string($_POST['question2']); 
$question3 = mysql_real_escape_string($_POST['question3']); 
$question4 = mysql_real_escape_string($_POST['question4']); 
$question5 = mysql_real_escape_string($_POST['question5']); 
$question6 = mysql_real_escape_string($_POST['question6']); 

mysql_query("INSERT INTO answers (q1,q2,q3,q4,q5,q6) VALUES ('$question1','$question2','$question3','$question4','$question5','$question6')"); 
echo "<h1>Thank You !</h1>"; 
} 

?> 

最後,我的表:

CREATE TABLE IF NOT EXISTS `answers` (
    `q1` int(3) NOT NULL, 
    `q2` int(3) NOT NULL, 
    `q3` int(3) NOT NULL, 
    `q4` int(3) NOT NULL, 
    `q5` int(3) NOT NULL, 
    `q6` int(3) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 
+0

使用print _r($ _ POST); //知道數據是從或不是從post.check它out.it將打印像數組。 –

+0

關閉:http://php.net/manual/en/function.mysql-query.php **不推薦使用此擴展名(mysql_ *)。相反,MySQLi或PDO_MySQL擴展應該使用** – nu6A

+0

實際上你需要告訴我們什麼是錯誤 - 如果有的話...... 以及在什麼級別或什麼時候你的代碼工作正常.. –

回答

1

我必須說這應該是:

$('form1').submit(function() { // <------you are not submiting the specific form 

這樣的:

$('#form1').submit(function() { // <------ every id should have a '#' in jquery 

這可能是問題,如果你能在jQuery的改變你的.done功能

.done(function() { 
    alert("Data Saved"); // try removing the passed param "msg" 
}); 
+0

我試過但不是從這裏:( – user1820705

0

一個ID有一個 「#」 開頭

$('form1').submit(function() {