2012-07-11 125 views
0

我正在做一个PHP/mySQL程序,我第一次可能需要一些建议。读取和写入到mysql

我想要创建的程序的工作方式如下...如果出现错误,它将会发送给服务器。你希望能够自己填写表格。这个问题会显示在另一个计算机上。

我想我需要写入一个mySQL数据库,然后在将它们连接到我的数据库之后从下一个computor读取它。但我如何以最好的方式做到这一点?

我已经成功下载XAMPP并在mySQL中创建表。此外,我把信息和提交按钮的字段。

我在得到错误:

$operationstep = $_POST['Operation step']; 
$problem_detail = $_POST['Problem detail']; 

Notice: Undefined index: Operation step in E:\xampp\htdocs\avvikelse.php on line 21

Notice: Undefined index: Problem detail in E:\xampp\htdocs\avvikelse.php on line 22

Notice: Undefined variable: operation_step in E:\xampp\htdocs\avvikelse.php on line 25 Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'step, Problem detail) VALUES ('', '',' at line 1

// blabla, connetcting... 

$operationstep = $_POST['Operation step']; 
$problem_detail = $_POST['Problem detail']; 

$sql = "INSERT INTO avvikelsekort (Operation step, Problem detail) VALUES 
('$operation_step', '$problem_detail')"; 

if (!mysql_query($sql)) { 
     die('Error: ' .mysql_error()); 
} 

mysql_close(); 
?> 

我现在如你所说的马可和我得到阵列(0){}我的形式看起来像这样

<form action="avvikelse.php" method="POST" /> 
    <p>Operation step: <input type="text" name"Operation step" /></p> 
    <p>Problem detail: <input type="text" name"Problem detail" /></p>  
    <input type="submit" value="Submit" /> 
    </form> 
+2

在'$ operationstep = $ _POST ['Operation step'];'之前加'var_dump($ _ POST);'检查您传入的'POST'数据。 – 2012-07-11 07:28:29

+0

在我做了你告诉我的事情之后,我更新了我的问题。为什么我的POST是空的? – user1516844 2012-07-11 10:01:33

+0

看看我编辑的答案 – Marco 2012-07-11 14:34:16

回答

2

做如果在字段名称中使用空格,则必须使用反引号。
更多,如果你调用变量

$operationstep = $_POST['Operation step']; 
$problem_detail = $_POST['Problem detail']; 

您的查询应该是

$sql = "INSERT INTO avvikelsekort (`Operation step`, `Problem detail`) VALUES 
    ('$operationstep', '$problem_detail')"; 

而且,最后但并非最不重要的,记得你必须始终过滤用户输入避免SQL注入!

编辑
你的表格是错误的。试试这个:

<form action="avvikelse.php" method="POST" /> 
    <p>Operation step: <input type="text" name="Operation step" /></p> 
    <p>Problem detail: <input type="text" name="Problem detail" /></p>  
    <input type="submit" value="Submit" /> 
</form> 
+0

+1,但不仅如此 - 所有读取POST参数,空格都转换为%20 – alfasin 2012-07-11 07:26:52

1

不能有空格在Post /获取名称:

$operationstep = $_POST['Operation step']; 
$problem_detail = $_POST['Problem detail']; 

尝试

$operationstep = $_POST['Operation_step']; 
$problem_detail = $_POST['Problem_detail']; 

或形式对其进行重命名。

另外:

$operationstep = $_POST['Operation step']; 
$problem_detail = $_POST['Problem detail']; 

不匹配,你在做刀片 - 注意,您在插入使用,变量名如何用空格列名应被称为:

$sql = "INSERT INTO avvikelsekort (`Operation step`, `Problem detail`) VALUES 
('$operation_step', '$problem_detail')"; 
0

您会收到通知未定义索引,因为该索引不存在于$_POST变量中。不要在索引中使用空格。编辑您的表单并在输入字段的名称属性中使用下划线,并在查询中使用$operation_step,同时在分配时使用$operationstep。并在查询中使用反引号查询表名。

0

由于其他人已经回答,您会收到未定义的索引消息,因为该值不存在于$_POST阵列中。

我建议您在将$_POST数据放入变量之前清理数据,因为任何发布数据可能包含某些可能导致SQL注入攻击的恶意数据。您可以在以下网址查看其他安全开发实践:https://www.owasp.org/index.php/OWASP_Guide_Project