将数据更新到数据库时出错[重复]

时间:2022-12-26 15:42:49

This question already has an answer here:

这个问题在这里已有答案:

I'm trying to create an admin page where the admin must approve a user registration before the user logs in.I'm trying to show in this page all the users which have been registered but still waiting for admin approval for log in.So I want to create a button close to each unconfirmed user with the value"Yes" to give the admin the possibility to approve his account.If "Yes" button is set by the admin the user should be approved so the approval field which is stored in my database should be updated to 1.This is the idea.When executing the code the buttons are displayed but no update is made for the field approval!

我正在尝试创建一个管理员页面,管理员必须在用户登录之前批准用户注册。我试图在此页面中显示所有已注册但仍在等待管理员批准登录的用户。所以我想创建一个靠近每个未确认用户的按钮,其值为“是”,以便管理员可以批准他的帐户。如果“是”按钮由管理员设置,则应批准用户,以便存储的批准字段在我的数据库中应该更新为1.这是一个想法。当执行代码时,按钮会显示但不会更新现场批准!

Also the buttons are displayed but are not functional.Can someone help me to make the "Yes" buttons functional? This is my code:

此外,按钮显示但不起作用。有人帮助我使“是”按钮功能正常吗?这是我的代码:

<html >
<head>
<title></title>
</head>

<body>
<?php
if( !($database=mysql_connect("localhost","root",""))||!(mysql_select_db("st_login",$database))  )
   print("Could not connect");
if(isset($_POST['yes'])){

     $id = intval($_POST['id']);
$update_query= "UPDATE login SET `approval`=1 WHERE `id` = '".$id."'";
mysql_query($update_query,$database);
 }

$query = "SELECT * FROM `login` WHERE `admin` =0";
 if(!($result=mysql_query($query,$database)))
{
    print("Could not execute query");
    die (mysql_error());//ose error
} else
  while($query_row=mysql_fetch_assoc($result))
  {

 $firstname=$query_row['firstname'];
$lastname=$query_row['lastname'];
$username=$query_row['username'];
$password=$query_row['password'];
$email=$query_row['email'];
$cv=$query_row['cv'];
$id=$query_row['id'];

if($query_row['approval']==0){
  echo "this user is waiting for you approval";
echo $firstname.'   '.$lastname.'  has this cv:'.$cv.'<br />
Do you want to approve his account?
<form action="admin.php" method="post">
<input  type="text" style="display: none;" value="$id" name="id">
<input  type="submit" value="yes" name="yes">
</form>
<br/>';
}
}
mysql_close($database);
?>
</body>
</html>

Anyone helping me with this updating?Thanks in advance!

有人帮我这个更新吗?提前谢谢!

2 个解决方案

#1


0  

Change the button to be

将按钮更改为

<form action="admin.php" method="post"><input type="submit" value="'.$id.'" name="id"></form><br />';

So that the whole echo will be

这样整个回声都会如此

echo $firstname.' '.$lastname.' has this cv:'.$cv.'<br /> Do you want to approve his account? <form action="admin.php" method="post"><input type="submit" value="'.$id.'" name="id"></form><br />';

The name of the form element is what you should expect to be the key in the $_POST array, and the value is what you should expect to be the value of $_POST['id']

表单元素的名称是你应该期望成为$ _POST数组中的键,并且值应该是你应该期望的$ _POST ['id']的值

Also, you should use $_POST not $_GET as you have specified the form method to be post

此外,您应该使用$ _POST而不是$ _GET,因为您已指定要发布的表单方法

#2


0  

You should change the last part of your code. Your form method is POST and yet you're trying to get id via GET. You can't do that, and you can't get the id from the input like this.

您应该更改代码的最后部分。你的表单方法是POST,但你试图通过GET获取id。你不能这样做,你不能像这样从输入中获取id。

This is what you should do:

这是你应该做的:

if($query_row['approval']==0){
  echo "this user is waiting for you approval";
echo $firstname.'   '.$lastname.'  has this cv:'.$cv.'<br />
Do you want to approve his account?
<form action="admin.php" method="post">
<input  type="hidden" value="$id" name="id">
<input  type="submit" value="yes" name="yes">
</form>
<br/>';
}

}
mysql_close($database);
?>

And on the line

就行了

$id = intval($_GET['id']);

It should turn into

它应该变成

 $id = intval($_POST['id']);

#1


0  

Change the button to be

将按钮更改为

<form action="admin.php" method="post"><input type="submit" value="'.$id.'" name="id"></form><br />';

So that the whole echo will be

这样整个回声都会如此

echo $firstname.' '.$lastname.' has this cv:'.$cv.'<br /> Do you want to approve his account? <form action="admin.php" method="post"><input type="submit" value="'.$id.'" name="id"></form><br />';

The name of the form element is what you should expect to be the key in the $_POST array, and the value is what you should expect to be the value of $_POST['id']

表单元素的名称是你应该期望成为$ _POST数组中的键,并且值应该是你应该期望的$ _POST ['id']的值

Also, you should use $_POST not $_GET as you have specified the form method to be post

此外,您应该使用$ _POST而不是$ _GET,因为您已指定要发布的表单方法

#2


0  

You should change the last part of your code. Your form method is POST and yet you're trying to get id via GET. You can't do that, and you can't get the id from the input like this.

您应该更改代码的最后部分。你的表单方法是POST,但你试图通过GET获取id。你不能这样做,你不能像这样从输入中获取id。

This is what you should do:

这是你应该做的:

if($query_row['approval']==0){
  echo "this user is waiting for you approval";
echo $firstname.'   '.$lastname.'  has this cv:'.$cv.'<br />
Do you want to approve his account?
<form action="admin.php" method="post">
<input  type="hidden" value="$id" name="id">
<input  type="submit" value="yes" name="yes">
</form>
<br/>';
}

}
mysql_close($database);
?>

And on the line

就行了

$id = intval($_GET['id']);

It should turn into

它应该变成

 $id = intval($_POST['id']);