PHP / MySQL - 无法更新表单

时间:2012-09-08 13:32:13

标签: mysql

我一直在尝试更新包含复选框的表单,这比我预期的要难一些。

现在我认为我做的一切都是正确的,它只是没有更新。我现在还不知道为什么会这样。

这是包含以下格式的页面:

<?php 

include "style.css";
include "connect.php";
include "header.php";


//This code runs if the form has been submitted

$id = htmlspecialchars($_GET["id"]);
$query  = "SELECT * FROM draaiboek_users WHERE id = '$id'";
$result = mysql_query($query);
$data=mysql_fetch_array($result);

?>


<table>
<p align="right"><button onclick="history.go(-1);">Terug</button></p>
</table>

<form action="users-update.php" method="post">
<input type="hidden" name=ud_id" value="<? echo $id; ?>">

<table border="0">
<br>
<tr><td>Gebruikersnaam:</td><td>

<input type="text" name="ud_username" maxlength="60" value="<? echo ($data['username']); ?>">

</td></tr>

<tr><td>Module Basisgegevens:</td><td>

<input type="checkbox" name="ud_mod_basisgegevens" value="1"<?php if($data['mod_basisgegevens'] == '1') echo 'checked'; ?>>

</td></tr>

<tr><td>Module Personeel:</td><td>

<input type="checkbox" name="ud_mod_personeel" value="1"<?php if($data['mod_personeel'] == '1') echo 'checked'; ?>>

</td></tr>

<tr><td>Module Vrijwilligers:</td><td>

<input type="checkbox" name="ud_mod_vrijwilligers" value="1"<?php if($data['mod_vrijwilligers'] == '1') echo 'checked'; ?>>

</td></tr>

<tr><td>Module Gasten:</td><td>

<input type="checkbox" name="ud_mod_gasten" value="1"<?php if($data['mod_gasten'] == '1') echo 'checked'; ?>>

</td></tr>

<tr><td>Module Artistiek:</td><td>

<input type="checkbox" name="ud_mod_artistiek" value="1"<?php if($data['mod_artistiek'] == '1') echo 'checked'; ?>>

</td></tr>

<tr><td>Module Publiekswerking:</td><td>

<input type="checkbox" name="ud_mod_publiekswerking" value="1"<?php if($data['mod_publiekswerking'] == '1') echo 'checked'; ?>>

</td></tr>

<tr><td>Module Techniek:</td><td>

<input type="checkbox" name="ud_mod_techniek" value="1"<?php if($data['mod_techniek'] == '1') echo 'checked'; ?>>

</td></tr>

<tr><td>Module Financien:</td><td>

<input type="checkbox" name="ud_mod_financien" value="1"<?php if($data['mod_financien'] == '1') echo 'checked'; ?>>

</td></tr>
<tr><th colspan=2><br><input type="submit" name="submit" 
value="Update"></th></tr> </table>

</form>

这是更新页面:

<html>
<head>
<?php

include 'style.css';
include 'connect.php';
include 'security.php';
include 'header.php';

?>
</head>


<body>

<table>

<?php 
  $ud_id = $_POST['ud_id'];
  $ud_username = $_POST['ud_username'];
  $ud_mod_basisgegevens = ($_POST['ud_mod_basisgegevens'] == '1') ? "1" : "0";
  $ud_mod_personeel = ($_POST['ud_mod_personeel'] == '1') ? "1" : "0";
  $ud_mod_vrijwilligers = ($_POST['ud_mod_vrijwilligers'] == '1') ? "1" : "0";
  $ud_mod_gasten = ($_POST['ud_mod_gasten'] == '1') ? "1" : "0";
  $ud_mod_artistiek = ($_POST['ud_mod_artistiek'] == '1') ? "1" : "0";
  $ud_mod_publiekswerking = ($_POST['ud_mod_publiekswerking'] == '1') ? "1" : "0";
  $ud_mod_techniek = ($_POST['ud_mod_techniek'] == '1') ? "1" : "0";
  $ud_mod_financien = ($_POST['ud_mod_financien'] == '1') ? "1" : "0";


  $query = "UPDATE draaiboek_users SET username = '$ud_username' mod_basisgegevens = '$ud_mod_basisgegevens' mod_personeel = '$ud_mod_personeel' mod_vrijwilligers = '$ud_mod_vrijwilligers' mod_gasten = '$ud_mod_gasten' mod_artistiek = '$ud_mod_artistiek' mod_publiekswerking = '$ud_mod_publiekswerking' mod_techniek = '$ud_mod_techniek' mod_financien = '$ud_mod_financien' WHERE id = '$ud_id'";

  mysql_query($query);
  echo "Record updated";
  mysql_close();
?>

</table>
</body>
</html>

任何想法是如何产生的?

谢谢,
大安

3 个答案:

答案 0 :(得分:0)

列/值分配之间没有逗号

$query = "UPDATE draaiboek_users SET username = '$ud_username', mod_basisgegevens = '$ud_mod_basisgegevens', mod_personeel = '$ud_mod_personeel', mod_vrijwilligers = '$ud_mod_vrijwilligers', mod_gasten = '$ud_mod_gasten', mod_artistiek = '$ud_mod_artistiek', mod_publiekswerking = '$ud_mod_publiekswerking', mod_techniek = '$ud_mod_techniek', mod_financien = '$ud_mod_financien' WHERE id = '$ud_id'";

参考:UPDATE Syntax

在执行查询时考虑添加or die

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

答案 1 :(得分:0)

Daan,您在SQL查询中使用了错误的语法 - 您忘记了逗号,您还必须正确使用引号并至少添加mysql_real_escape_string,以使代码免受SQL Injections的攻击。<​​/ p>

您的代码的正确示例应为:

$query = "
UPDATE 
    draaiboek_users 
SET 
    username = '".mysql_real_escape_string($ud_username)."',
    mod_basisgegevens = '".mysql_real_escape_string($ud_mod_basisgegevens)."',
    mod_personeel = '".mysql_real_escape_string($ud_mod_personeel)."',
    mod_vrijwilligers = '".mysql_real_escape_string($ud_mod_vrijwilligers)."',
    mod_gasten = '".mysql_real_escape_string($ud_mod_gasten)."',
    mod_artistiek = '".mysql_real_escape_string($ud_mod_artistiek)."',
    mod_publiekswerking = '".mysql_real_escape_string($ud_mod_publiekswerking)."',
    mod_techniek = '".mysql_real_escape_string($ud_mod_techniek)."',
    mod_financien = '".mysql_real_escape_string($ud_mod_financien)."' 
WHERE 
    id = '".mysql_real_escape_string($ud_id)."'";

mysql_query($query);
echo "Record updated";
mysql_close();

您的代码易受SQL注入攻击。我强烈建议您在将数据插入数据库时​​使用mysql_real_escape_string以防止SQL injections,作为快速解决方案或更好地使用PDOMySQLi

mysql_*函数的PHP手册章节的介绍中说明了 建议不要使用此扩展名来编写新代码。相反,您应该使用MySQLiPDO_MySQL扩展程序。

除了在HTML中,您必须在checked="checked"之前使用空格,例如:

<input type="checkbox" name="ud_mod_techniek" value="1"<?php if($data['mod_techniek'] == '1') echo ' checked="checked"'; ?>>

否则,在输出中,属性valuechecked可能会一起打印出来,因此您的HTML将不会W3C Valid返回No space between attributes错误。< / p>

答案 2 :(得分:0)

表单中缺少某些内容

您尚未为ud_id添加双引号

<input type="hidden" name=ud_id" value="<? echo $id; ?>">

您尚未添加输入的ID并设置值ID,您必须在更新之前获取行的ID,以实现

<input type="hidden" name="ud_id" value="<?php echo $data['id'];?>" id="<?php echo $data['id'];?>">

所以只有当id通过POST正确传递时,才会对属于行id的特定列进行更新