Php 表单无法更新mysql表
因此,我目前正在使用一个表单,管理员可以通过勾选框系统从数据库中选择多个用户,然后在他们登录时将欢迎消息或一般消息更改为客户端:Php 表单无法更新mysql表,php,mysql,forms,Php,Mysql,Forms,因此,我目前正在使用一个表单,管理员可以通过勾选框系统从数据库中选择多个用户,然后在他们登录时将欢迎消息或一般消息更改为客户端: <?php session_start(); include_once("isadmin.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transition
<?php
session_start();
include_once("isadmin.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=iso-8859-1" />
<title>Update Client Message</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
echo '<ul class="err">';
foreach($_SESSION['ERRMSG_ARR'] as $msg) {
echo '<li>',$msg,'</li>';
}
echo '</ul>';
unset($_SESSION['ERRMSG_ARR']);
}
?>
<form id="updateform" name="updateform" method="post" action="updateexec.php">
<table width="500" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<th width="200">Select User</th>
<td>
<?php
require_once('config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
$useruploadids = mysql_query("SELECT member_id, firstname, lastname FROM members");
while ($row = mysql_fetch_assoc($useruploadids)) {
$userid = $row['member_id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
?>
<input type="checkbox" name="userid_<?php echo $userid ?>" value="y" /><?php echo $firstname ?><?php echo $lastname ?><br />
<?php } ?>
</td>
</tr>
<tr>
<th>Message For Client </th>
<td>
<textarea input name="otherdeets" type="textarea" class="textfield" id="otherdeets" style="width: 356px; height: 176px">
</textarea>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Update" /></td>
</tr>
</table>
</form>
</body>
</html>
你知道怎么了吗?知道我运气好,这很可能是个拼写错误
谢谢你嘿,你的查询错了,应该有带有适当列名的where条件,即“member\u id” 还有一件事 您正在获取
$id=$\u POST['userid']代码>这是一个错误,因为该键不存在值
而是在执行更新查询之前在if条件中执行,即。
$id=$POST['userid.$row['member\u id']
尝试在$result=mysql\u query($sql)
之后放置或死(mysql\u error())
,查看是否有错误。此外,您不应该使用函数,因为从PHP5.5开始,函数就不推荐使用。使用或。添加echo$sql;在$result=mysql\u查询之前,请检查sql语法。如果可以,可以在phpmyadmin中尝试,也可以直接在mysql bash中尝试。此外,mysql扩展已被弃用,您应该切换到mysqli或PDO。您还需要准备变量。。。使用mysql\u real\u escape\u string()
。如果您仍然使用mysql\uu
函数,那么在页面内建立数据库连接是一种糟糕的做法。您应该提取该部分并将其移动到自己的文件中。您不应该发布易受SQL注入攻击的答案。这将教会OP糟糕的编码……我建议编辑他们的答案,而不是告诉他们甚至不要发布答案。这更有建设性。
<?php
echo( "<pre>" );
print_r( $_POST );
echo( "</pre>" );
include ("config.php");
$tbl_name="members";
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//This gets all the other information from the form
$update = $_POST['otherdeets'];
$id = $_POST['userid'];
// Cycle through each member and check that it needs to be added to the db
$useruploadids = mysql_query( "SELECT member_id FROM members" );
while ($row = mysql_fetch_assoc($useruploadids))
{
// Check that the member was sent from the last form
if( isset( $_POST['userid_'.$row['member_id']] ) && $_POST['userid_'.$row['member_id']] == "y" )
{
// update data in mysql database
$sql="UPDATE $tbl_name SET otherdeets='$update' WHERE id='$id'";
$result=mysql_query($sql);
}
}
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='admin-welcome.php'>Admin Home</a>";
}
else {
echo "ERROR";
}
?>
Array
(
[userid_1] => y
[otherdeets] => Blah Blah
[Submit] => Update
)
ERROR