【问题标题】:Results not correctly being entered into SQL DB结果未正确输入 SQL DB
【发布时间】:2013-11-18 01:14:36
【问题描述】:

我有一个要求安全问题和答案的表单,提交后数据被输入到我的 SQL 数据库中,但由于某种原因,当我运行表单并尝试打印显示的数据以确保正确输入时数据是空白的,所以我检查了 SQL 数据库并且有一个新条目,但它都是空白的。

我试图弄清楚为什么表单没有将任何数据输入到 SQL DB 中。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>

<form method="post" id="myForm" name="input" action="submitSQL.php">

        Security Question: <input type="text" id="sQuestion" ></br>
        Security Question Answer: <input type="text" id="sqAnswer" ></br>


        </br>
        <input type="submit">

        </form>

</body>
</html>

PHP 文件

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>

<?php

$sQuestion = filter_input(INPUT_POST, 'sQuestion');
$sqAnswer = filter_input(INPUT_POST, 'sqAnswer');

$conn = mysql_connect("localhost","root","")or die (mysql_error());

mysql_select_db("assignment_3", $conn);

$insert = "insert into securityquestiontable (securityQuestion, securityAnswer)     values('$sqAnswer', '$sQuestion')";

$result = mysql_query($insert, $conn) or die (mysql_error());

print "<table border=1>
<tr><td>Security Question:</td><td> '$sQuestion'</td></tr></br>
<tr><td>Security Question Answer:</td> <td> '$sqAnswer'</td></tr></br>

</table>";

?>

</body>
</html>

【问题讨论】:

  • 这需要基本的调试。 $sQuestion 是否包含值? (这里的问题是你没有给你的表单元素一个 name 属性,虽然)
  • sql-server 标记是指微软制造的特定关系型数据库,而不是您使用的 mysql。你的mysql代码也是vulnerable to injectionusing an obsolete API。最后,当PHP生成一个空白页面而不是做某事时,你需要enable error reporting

标签: php html mysql sql sql-server


【解决方案1】:

您尚未为您的字段指定 name 属性。

这个:

    Security Question: <input type="text" id="sQuestion" ></br>
    Security Question Answer: <input type="text" id="sqAnswer" ></br>

应该是这样的:

    Security Question: <input type="text" id="sQuestion" name="sQuestion"></br>
    Security Question Answer: <input type="text" id="sqAnswer" name="sqAnswer"></br>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-04
    • 2014-06-23
    • 2014-12-15
    • 2011-04-28
    • 1970-01-01
    • 2023-03-25
    • 2021-06-19
    • 2013-04-03
    相关资源
    最近更新 更多