-1

请有人帮助我,为什么我的代码不与 mySQL 表对话?我真的不知道为什么它不起作用,我花了几天时间寻找解决方案,非常感谢您的帮助......

我的html...

<!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">
<head>
<script type="text/javascript" src="Locus.js"></script> <!-- LINKS TO THE EXTERNAL JAVASCRIPT -->

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Locus of control test</title>
<link href="Locus.css" rel="stylesheet" type="text/css" /> <!-- LINKS TO THE EXTERNAL CS SHEET -->

</head>

<body>

<form action="Locus.php" id="Locus" method="post" name="Locus" onsubmit="validateForm()"> <!-- GIVES A LINK TO THE PHP FORM AND THE METHOD -->

<h4>Surname: <input type="text" name="surname"/> <br/>Prison: <input type="text" name="prison"/><br/>National Insurance: <input type="text" name="NI"/></h4>
<h3> Please carefully read all the instructions given on the left hand panel.</h3>
<h1 class="Header"> LOCUS OF CONTROL</h1>
<h2>Please click submit ONLY when all questions have been completed <br/> <input name="submit" id="submit" type="submit" value="Check & Submit" /></h2>

<p>1. I can anticipate difficulties and take action to avoid them. <br />
<input type="radio" name="Q1" value="4" /> Always Agree <input type="radio" name="Q1" value="3" /> Agree <input type="radio" name="Q1" value="2" /> Unsure <input type="radio" name="Q1" value="1" /> Disgree <input type="radio" name="Q1" value="0" /> Always Disagree
</p>


<p>2. A great deal of what happens to me is just a matter of chance.  <br />
<input type="radio" name="Q2" value="0" /> Always Agree <input type="radio" name="Q2" value="1" /> Agree <input type="radio" name="Q2" value="2" /> Unsure <input type="radio" name="Q2" value="3" /> Disgree <input type="radio" name="Q2" value="4" /> Always Disagree
</p>

<p>3. Everyone knows that luck or chance determines the future.  <br />
<input type="radio" name="Q3" value="0" /> Always Agree <input type="radio" name="Q3" value="1" /> Agree <input type="radio" name="Q3" value="2" /> Unsure <input type="radio" name="Q3" value="3" /> Disgree <input type="radio" name="Q3" value="4" /> Always Disagree
</p>

<p>4. I can control my problems only if I have outside support.  <br />
<input type="radio" name="Q4" value="0" /> Always Agree <input type="radio" name="Q4" value="1" /> Agree <input type="radio" name="Q4" value="2" /> Unsure <input type="radio" name="Q4" value="3" /> Disgree <input type="radio" name="Q4" value="4" /> Always Disagree
</p>

<p>5. When I make plans I am almost certain I can make them work. <br />
<input type="radio" name="Q5" value="4" /> Always Agree <input type="radio" name="Q5" value="3" /> Agree <input type="radio" name="Q5" value="2" /> Unsure <input type="radio" name="Q5" value="1" /> Disgree <input type="radio" name="Q5" value="0" /> Always Disagree
</p>

<p>6. My problems will dominate all my life. <br />
<input type="radio" name="Q6" value="0" /> Always Agree <input type="radio" name="Q6" value="1" /> Agree <input type="radio" name="Q6" value="2" /> Unsure <input type="radio" name="Q6" value="3" /> Disgree <input type="radio" name="Q6" value="4" /> Always Disagree
</p>

<p>7. My mistakes and problems are my responsibility to deal with. <br />
<input type="radio" name="Q7" value="4" /> Always Agree <input type="radio" name="Q7" value="3" /> Agree <input type="radio" name="Q7" value="2" /> Unsure <input type="radio" name="Q7" value="1" /> Disgree <input type="radio" name="Q7" value="0" /> Always Disagree
</p>

<p>8. Becoming a success is a matter of hard work, luck has little or nothing to do with it. <br />
<input type="radio" name="Q8" value="4" /> Always Agree <input type="radio" name="Q8" value="3" /> Agree <input type="radio" name="Q8" value="2" /> Unsure <input type="radio" name="Q8" value="1" /> Disgree <input type="radio" name="Q8" value="0" /> Always Disagree
</p>

<p>9. My life is controlled by outside actions and events.  <br />
<input type="radio" name="Q9" value="0" /> Always Agree <input type="radio" name="Q9" value="1" /> Agree <input type="radio" name="Q9" value="2" /> Unsure <input type="radio" name="Q9" value="3" /> Disgree <input type="radio" name="Q9" value="4" /> Always Disagree
</p>

<p>10. I believe people are victims of circumstances beyond their control.   <br />
<input type="radio" name="Q10" value="0" /> Always Agree <input type="radio" name="Q10" value="1" /> Agree <input type="radio" name="Q10" value="2" /> Unsure <input type="radio" name="Q10" value="3" /> Disgree <input type="radio" name="Q10" value="4" /> Always Disagree
</p>

<p>11. To continually manage my problems I need professional help. <br />
<input type="radio" name="Q11" value="0" /> Always Agree <input type="radio" name="Q11" value="1" /> Agree <input type="radio" name="Q11" value="2" /> Unsure <input type="radio" name="Q11" value="3" /> Disgree <input type="radio" name="Q11" value="4" /> Always Disagree
</p>

<p>12. When I am under stress the tightness in my muscles is due to things outside my control.<br />
<input type="radio" name="Q12" value="0" /> Always Agree <input type="radio" name="Q12" value="1" /> Agree <input type="radio" name="Q12" value="2" /> Unsure <input type="radio" name="Q12" value="3" /> Disgree <input type="radio" name="Q12" value="4" /> Always Disagree
</p>

<p>13. 1 believe a person really can be master of his own fate.<br />
<input type="radio" name="Q13" value="4" /> Always Agree <input type="radio" name="Q13" value="3" /> Agree <input type="radio" name="Q13" value="2" /> Unsure <input type="radio" name="Q13" value="1" /> Disgree <input type="radio" name="Q13" value="0" /> Always Disagree
</p>

<p>14. It is impossible to control irregular fast breathing when I am having difficulties.<br />
<input type="radio" name="Q14" value="0" /> Always Agree <input type="radio" name="Q14" value="1" /> Agree <input type="radio" name="Q14" value="2" /> Unsure <input type="radio" name="Q14" value="3" /> Disgree <input type="radio" name="Q14" value="4" /> Always Disagree
</p>

<p>15. I understand why my problems vary so much from one occasion to another. <br />
<input type="radio" name="Q15" value="4" /> Always Agree <input type="radio" name="Q15" value="3" /> Agree <input type="radio" name="Q15" value="2" /> Unsure <input type="radio" name="Q15" value="1" /> Disgree <input type="radio" name="Q15" value="0" /> Always Disagree
</p>

<p>16. I am confident of being able to deal successfully with future problems.<br />
<input type="radio" name="Q16" value="4" /> Always Agree <input type="radio" name="Q16" value="3" /> Agree <input type="radio" name="Q16" value="2" /> Unsure <input type="radio" name="Q16" value="1" /> Disgree <input type="radio" name="Q16" value="0" /> Always Disagree
</p>

<p>17. In my case maintaining control over my problems is mainly due to luck. <br />
<input type="radio" name="Q17" value="0" /> Always Agree <input type="radio" name="Q17" value="1" /> Agree <input type="radio" name="Q17" value="2" /> Unsure <input type="radio" name="Q17" value="3" /> Disgree <input type="radio" name="Q17" value="4" /> Always Disagree
</p>

<p>18. I have often been blamed for events beyond my control. <br />
<input type="radio" name="Q18" value="0" /> Always Agree <input type="radio" name="Q18" value="1" /> Agree <input type="radio" name="Q18" value="2" /> Unsure <input type="radio" name="Q18" value="3" /> Disgree <input type="radio" name="Q18" value="4" /> Always Disagree
</p>
<h5>Please click 'submit' at the start of this test</h5>
</form>
</body>
</html>

这是我的php....

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="Locus.css" rel="stylesheet" type="text/css" /> <!-- LINKED ATTACHED STYLE SHEET-->
<title>Locus Test Complete</title>
</head>
<h4>THANK YOU FOR SUBMITTING THIS TEST, PLEASE CHOOSE ANOTHER FROM THE MENU BAR.</h4>

<body>
<?php
/*DECLARING THE VARIABLES FOR ACCESS*/
$username="root";
$password="";
$database_server="localhost";

/*CONNECTING TO THE SERVER*/
$database="locus";
mysql_connect($database_server,$username,$password) or die("cannot connect");

/*CONNECTING TO THE DATABASE*/
@mysql_select_db($database) or die( "Unable to select database");


/*CHECK THE STATUS AND DECLARE THE VARIABLES*/
$_POST = array( );

if( isset( $_POST['surname'])) { 
    $Surname = $_POST['surname'];
} else { 
    $Surname = "";
} 

if( isset( $_POST['prison'])) { 
    $Prison = $_POST['prison'];
} else { 
    $Prison = "";
}
/*SHORTHAND WAY TO CHECK THE STATUS AND DECLARE THE VARIABLES*/
$NI = isset( $_POST['NI']) ? $_POST['NI'] : "";
$Q1 = isset( $_POST['Q1']) ? $_POST['Q1'] : "";
$Q2 = isset( $_POST['Q2']) ? $_POST['Q2'] : "";
$Q3 = isset( $_POST['Q3']) ? $_POST['Q3'] : "";
$Q4 = isset( $_POST['Q4']) ? $_POST['Q4'] : "";
$Q5 = isset( $_POST['Q5']) ? $_POST['Q5'] : "";
$Q6 = isset( $_POST['Q6']) ? $_POST['Q6'] : "";
$Q7 = isset( $_POST['Q7']) ? $_POST['Q7'] : "";
$Q8 = isset( $_POST['Q8']) ? $_POST['Q8'] : "";
$Q9 = isset( $_POST['Q9']) ? $_POST['Q9'] : "";
$Q10 = isset( $_POST['Q10']) ? $_POST['Q10'] : "";
$Q11 = isset( $_POST['Q11']) ? $_POST['Q11'] : "";
$Q12 = isset( $_POST['Q12']) ? $_POST['Q12'] : "";
$Q13 = isset( $_POST['Q13']) ? $_POST['Q13'] : "";
$Q14 = isset( $_POST['Q14']) ? $_POST['Q14'] : "";
$Q15 = isset( $_POST['Q15']) ? $_POST['Q15'] : "";
$Q16 = isset( $_POST['Q16']) ? $_POST['Q16'] : "";
$Q17 = isset( $_POST['Q17']) ? $_POST['Q17'] : "";
$Q18 = isset( $_POST['Q18']) ? $_POST['Q18'] : "";


/*PLAVE THE VALUES OF THE VARIABLES WITHIN THE DATABASE TABLE*/
$query = "INSERT INTO locus.locusofcontrolscores VALUES 
('','$Surname','$Prison','$NI','$Q1','$Q2','$Q3','$Q4','$Q5','$Q6','$Q7','$Q8','$Q9','$Q10','$Q11','$Q12','$Q13','$Q14','$Q15','$Q16','$Q17','$Q18')";

/*CREATE A QUERY FROM THE VARIABLE*/
mysql_query($query);

/*CLOSE THE SERVER CONNECTION*/
mysql_close();



?>



</body>
</html>
4

3 回答 3

5
$_POST = array( );

错了,去掉这一行。

$_POST是一个array()你不必声明这个。

于 2012-11-28T14:20:16.787 回答
0

可以解决您的问题的事情:

  • mysql_query($query) 或 die(mysql_error() . ' ' . $query);
  • 不要清除帖子数组
  • 不要抑制数据库连接或至少将错误放入模具中

您可能应该解决的问题,但可能不是您的问题:

  • $sql注入通过将变量放入sql而不转义
  • 他们使用 mysql 而不是 mysqli 验证数据,做
  • html 开始使用单选按钮后的控制器内容,例如
  • 复选框没有为您的 url 使用绝对路径
  • 不要使用 root 作为你的用户名 db 并且没有密码
  • 头部和身体之间不应有 html。

另外我不确定您是否可以将“”作为自动增量ID。我会把 Null 放在那里。更好的是,我会明确地将要添加的列放入表中。如果你不这样做,你以后会很头疼……说你有相同的插入到 1000 个位置……好吧,你添加一个列,现在你必须更新 1000 个位置。

此外,我会回显您所做的 sql 语句,并尝试通过命令提示符或 phpmyadmin 运行它。

于 2012-11-28T14:28:41.270 回答
0

according the screed shot in comment you are checking the structure ... click on the Browse tab than you will see the content

also try

mysql_query($query) or die(mysql_error());

to see the error in query

Warning : your code is vulnerable to SQL Injunction

also MySql_* function are deprecated use pdo or mysqli

于 2012-11-28T14:28:08.763 回答