用户注册到网站。管理员将登录&查看用户列表。
我正在尝试为管理员选择复选框并通过Dropdown submit更改用户状态。当我尝试下面的代码时,我可以选择“是/否/ A”,点击submit
按钮后显示消息“已成功添加”,但其未更新值< / strong>在数据库中。
表名:tbl_users,column:userStatus [enum],值:Y,N,A
形式
<form method="post" action="ajax1.php">
<select name="userStatus">
<option value="N">N</option>
<option value="Y">Y</option>
<option value="A">A</option>
</select>
<button type="submit" name="submit" >Submit</button>
</form>
ajax1.php
if(isset($_POST["submit"]))
{
$userStatus=$_POST["userStatus"];
$conn = new Database();
$stmt = $conn->dbConnection()->prepare("INSERT INTO tbl_users (userStatus) VALUES ('$userStatus')");
echo " Added Successfully ";
}
显示用户的代码复选框,ID,姓名,电子邮件:
$stmt = $user_home->runQuery("SELECT * FROM tbl_users");
$stmt->execute(array(":uid" => $_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->execute();
$status = array('Y'=>'Verified','N'=>'Not verified' , 'A'=>'Approved');
echo "<table>";
echo "<tr>
<td></td>
<td>id</td>
<td>name</td>
<td>email</td>
</tr>";
while($data = $stmt->fetch())
{
echo "<tr>
<td> <input type='checkbox' > </td>
<td>" . $data['userID'] . "</td>
<td>" . $data['name'] . "</td>
<td>" . $data['email'] . "</td>
</tr>";
}
echo "</table>";
我研究过&amp;在发布问题之前尝试了很多,在某些链接中,我看到我们需要使用Javascript,但在其他一些链接中,他们提到我们只能用php实现。 我是新编码,请帮帮我
修改
按照以下答案后,我将代码更新为$stmt = $conn->dbConnection()->prepare('UPDATE tbl_users SET userStatus = ? WHERE userID = ?');
答案 0 :(得分:-1)
您正在做的是使用以下行向表中插入新行:
"INSERT INTO tbl_users (userStatus) VALUES ('$userStatus')"
你应该做UPDATE
,而不是插入。您基本上想要做的是UPDATE
用户WHERE
用户userID
(或任何id列的名称)是所选用户的ID。
请参阅MySQL UPDATE。
答案 1 :(得分:-1)
<form method="post" action="ajax1.php">
<select name="userStatus" id="userStatus" onchange="UpdateStatus();">
<option value="N">N</option>
<option value="Y">Y</option>
<option value="A">A</option>
</select>
<button type="submit" name="submit" >Submit</button>
</form>
<script>
function UpdateStatus(){
var staus=$('#userStatus :selected').val();
allCheckedBoxes="";
$("input[id^='checkBoxId']:visible:checked").each(function(){ // this is to get checked checkbox vaues to update which user to update
allCheckedBoxes=allCheckedBoxes+$(this).val()+","; // comaseparated valuse
});
var dataString="allCheckedBoxes="+allCheckedBoxes+"&staus="+staus;
$.post("aupdate_status.php",'allCheckedBoxes='+allCheckedBoxes'&staus='+staus,function(result,status,xhr)
{
if( status.toLowerCase()=="error".toLowerCase() )
{ alert("An Error Occurred.."); }
else
{
alert(result);
}
})
.fail(function(){ alert("something went wrong. Please try again") });
}
</script>
update_status.php
<?php
$staus=$_POST['staus'];
$allCheckedBoxes=$_POST['allCheckedBoxes'];
$ArrallCheckedBoxes=explode(",",$allCheckedBoxes);
foreach($ArrallCheckedBoxes as $tempBoxes){
$sqlQueryToUpdate=" UPDATE tbl_users SET userStatus = '".$staus."' WHERE userID = '".$tempBoxes."' ;";
$conn = new Database();
$stmt = $conn->dbConnection()->prepare($sqlQueryToUpdate);
echo " ok success";
}
?>
Please try this . This is working in my case. This will work you too. Don't forget add jquery in your coding.