在PHP和mysql上显示if语句错误后,更新数据不起作用

时间:2018-09-02 14:38:52

标签: mysql mysqli

请帮助! 我想更新数据,但这不起作用。我有一个包含2个文件的代码,分别是:form.php和update.php

当我编辑数据并有意删除现有数据时,错误语句起作用,然后我正确更新,更新后的数据不起作用。

但是当我直接编辑而没有尝试错误时,数据更改立即生效

form.php

<?php
	
	$user_id = isset($_GET['user_id'])? $_GET['user_id']: false;
	$notif = isset($_GET['notif'])? $_GET['notif']: false;
	$nama_lengkap = isset($_GET['nama_lengkap'])? $_GET['nama_lengkap']: false;
	$username = isset($_GET['username'])? $_GET['username']: false;
	$position = isset($_GET['position'])? $_GET['position']: false;
	$status = isset($_GET['status'])? $_GET['status']: false;
	
	if($user_id){
		$query_user = mysqli_query($connect, "SELECT * FROM user WHERE user_id='$user_id' ");
		$row = mysqli_fetch_array($query_user);
		
		$nama_lengkap = $row['nama_lengkap'];
		$username = $row['username'];
		$position = $row['position'];
		$status = $row['status'];
	}else{
		if($notif == "nama_lengkap"){
			echo "empty nama lengkap";
		}elseif($notif == "username"){
			echo "empty username ";
		}elseif($notif == "position"){
			echo "empty username ";
		}elseif($notif == "status"){
			echo "empty status ";
		}
	}
?>
	

<form action="<?php echo URL."menu/user/update_user.php?user_id=$user_id";?>" method="post">
	<div class="element-form">
		<label>Nama Lengkap</label>
		<input type="text" name="nama_lengkap" value="<?php echo $nama_lengkap; ?>" />
	</div>
	
	<div class="element-form">
		<label>Username</label>
		<input type="text" name="username" readonly value="<?php echo $username; ?>" />
	</div>
	
	<div class="element-form">
		<label>Position</label>
		<input type="radio" name="position" value="superadmin" <?php if($position == "superadmin"){ echo "checked"; } ?>  />Superadmin
		<input type="radio" name="position" value="member" <?php if($position == "member"){ echo "checked"; } ?>  />Member
	</div>
	
	<div class="element-form">
		<label>Status</label>
		<input type="radio" name="status" value="on" <?php if($status == "on"){ echo "checked"; } ?> />On
		<input type="radio" name="status" value="off" <?php if($status == "off"){ echo "checked"; } ?> />Off
	</div>
	
	<div>
		<input type="submit" name="update" value="update" />
	</div>
	
	
</form>

update.php

<?php
	
	include_once '../../db.php';
	include_once '../../helper.php';

	if($_SERVER['REQUEST_METHOD'] == "POST"){
		$user_id = $_GET['user_id'];

		$nama_lengkap = $_POST['nama_lengkap'];
		$username = $_POST['username'];
		$position = $_POST['position'];
		$status = $_POST['status'];
		$dataform = http_build_query($_POST);
		

		if(empty($nama_lengkap)){
			header("location: ".URL."index.php?page=dashboard&menu=user&action=form_update&notif=nama_lengkap&$dataform");
		}elseif(empty($username)){
			header("location: ".URL."index.php?page=dashboard&menu=user&action=form_update&notif=username&$dataform");
		}elseif(empty($position)){
			header("location: ".URL."index.php?page=dashboard&menu=user&action=form_update&notif=position&$dataform");
		}elseif(empty($status)){
			header("location: ".URL."index.php?page=dashboard&menu=user&action=form_update&notif=status&$dataform");
		}else{

			mysqli_query($connect, "UPDATE user SET nama_lengkap='$nama_lengkap', username='$username', position='$position', status='$status' WHERE user_id='$user_id' ")or die("data failed");
												
			header("location: ".URL."index.php?page=dashboard&menu=user&action=interface_user");
			
		}
	}
		
?>

0 个答案:

没有答案