根据用户ID更新行

时间:2013-01-13 09:09:03

标签: php mysql database phpmyadmin dreamweaver

我有一个包含列的用户表:userid,username,password,email,agree_1,comment_1,agree_2,comment_2,............

我用它来更新表格:

UPDATE users SET Agree_1=%s, comment_1=%s, Agree_2=%s, comment_2=%s, Agree_3=%s, comment_3=%s, Agree_4=%s, comment_4=%s, Agree_5=%s, comment_5=%s, Agree_6=%s, comment_6=%s, Agree_7=%s, comment_7=%s, Agree_8=%s, comment_8=%s, Agree_9=%s, comment_9=%s, Agree_10=%s, comment_10=%s, Agree_11=%s, comment_11=%s, Agree_12=%s, comment_12=%s, Agree_13=%s, comment_13=%s, Agree_14=%s, comment_14=%s, Agree_15=%s, comment_15=%s, Agree_16=%s, comment_16=%s, Agree_17=%s, comment_17=%s, WHERE id=%s",
                   GetSQLValueString($_POST['grp_1'], "text"),
                   GetSQLValueString($_POST['text_1'], "text"),
                   GetSQLValueString($_POST['grp_2'], "text"),
                   GetSQLValueString($_POST['text_2'], "text"),
                   GetSQLValueString($_POST['grp_3'], "text"),
                   GetSQLValueString($_POST['text_3'], "text"),
                   GetSQLValueString($_POST['grp_4'], "text"),
                   GetSQLValueString($_POST['text_4'], "text"),
                   GetSQLValueString($_POST['grp_five'], "text"),
                   GetSQLValueString($_POST['text_five'], "text"),
                   GetSQLValueString($_POST['grp_six'], "text"),
                   GetSQLValueString($_POST['text_six'], "text"),
                   GetSQLValueString($_POST['grp_seven'], "text"),
                   GetSQLValueString($_POST['text_seven'], "text"),
                   GetSQLValueString($_POST['grp_eight'], "text"),
                   GetSQLValueString($_POST['text_eight'], "text"),
                   GetSQLValueString($_POST['grp_nine'], "text"),
                   GetSQLValueString($_POST['text_nine'], "text"),
                   GetSQLValueString($_POST['grp_ten'], "text"),
                   GetSQLValueString($_POST['text_ten'], "text"),
                   GetSQLValueString($_POST['grp_eleven'], "text"),
                   GetSQLValueString($_POST['text_eleven'], "text"),
                   GetSQLValueString($_POST['grp_twelve'], "text"),
                   GetSQLValueString($_POST['text_twelve'], "text"),
                   GetSQLValueString($_POST['grp_thirteen'], "text"),
                   GetSQLValueString($_POST['text_thirteen'], "text"),
                   GetSQLValueString($_POST['grp_fourteen'], "text"),
                   GetSQLValueString($_POST['text_fourteen'], "text"),
                   GetSQLValueString($_POST['grp_fifteen'], "text"),
                   GetSQLValueString($_POST['text_fifteen'], "text"),
                   GetSQLValueString($_POST['grp_sixteen'], "text"),
                   GetSQLValueString($_POST['text_sixteen'], "text"),
                   GetSQLValueString($_POST['grp_seventeen'], "text"),
                   GetSQLValueString($_POST['text_seventeen'], "text"),
                   GetSQLValueString($_POST['id'], "int"));

我的表单有一个隐藏字段,我称之为id,当我使用上面的查询进行更新时,它是成功的,但是当我创建另一个id为2的用户时,使用他的信息登录,使用上面的查询来更新用户,它继续更新id为1的用户,请如何让它更新我创建的任何用户。请帮忙。下面是我用来从数据库中选择的查询集:

$query_draft1b_query = sprintf("SELECT id, Agree_1, comment_1, Agree_2, comment_2, Agree_3, comment_3, Agree_4, comment_4, Agree_5, comment_5, Agree_6, comment_6, Agree_7, comment_7, Agree_8, comment_8, Agree_9, comment_9, Agree_10, comment_10, Agree_11, comment_11, Agree_12, comment_12, Agree_13, comment_13, Agree_14, comment_14, Agree_15, comment_15, Agree_16, comment_16, Agree_17, comment_17, Agree_18, comment_18, Agree_19, comment_19, Agree_20, comment_20, Agree_21, comment_21, Agree_22, comment_22, Agree_23, comment_23, Agree_24, comment_24, Agree_25, comment_25, Agree_26, comment_26, Agree_27, comment_27, Agree_28, comment_28, Agree_29, comment_29, Agree_30a, comment_30a, Agree_31a, comment_31a, Agree_30b, comment_30b, Agree_31b, comment_31b, Agree_32, comment_32, Agree_33, comment_33, Agree_34, comment_34, Agree_35, comment_35, Agree_36, comment_36, Agree_37, comment_37, Agree_38, comment_38, Agree_39, comment_39, Agree_40, comment_40, Agree_41, comment_41, Agree_42, comment_42, Agree_43, comment_43, Agree_44, comment_44, Agree_45, comment_45, Agree_46, comment_46, Agree_47, comment_47, Agree_48, comment_48, Agree_49a, comment_49a, agree_49b, comment_49b, Agree_50, comment_50 FROM users WHERE id = %s", GetSQLValueString($colname_draft1b_query, "int"));$draft1b_query = mysql_query($query_draft1b_query, $NAWFIA_1) or die(mysql_error());$row_draft1b_query = mysql_fetch_assoc($draft1b_query);$totalRows_draft1b_query = mysql_num_rows($draft1b_query);

1 个答案:

答案 0 :(得分:0)

请使用结构

UPDATE [table] SET [Column] = [Value] WHERE id = [id]

解决您的问题

Update users Set .... WHERE id = 2

但我认为你的数据库设计并不好。请将其拆分为两个表(评论,用户)。 在注释中,使用外键给用户。

相关问题