如何更新数据库表而不必填写每个输入? [英] How to update database table without having to fill every input?

查看:83
本文介绍了如何更新数据库表而不必填写每个输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





目前我有一个个人资料页面,显示个人资料数据库表中的信息,这个工作正常。



我还有一个编辑个人资料页面,如果用户提供关于我,位置和个人资料图像,数据库中的个人资料表会更新,然后显示在他们的个人资料页面上。



我遇到的问题是,如果用户只想编辑他们关于我并将他们的位置和个人资料图像保持不变,则会将配置文件表设置为空白对于未被用户填写的输入(配置文件图像,然后在这种情况下的位置)。



这是我的代码:



Hi,

Currently I have a profile page that displays the information from the profile database table and this works fine.

I also have an edit profile page where, if the user provides an about me, location and a profile image, the profile table in the database is updated and is then displayed on their profile page.

The problem I am having is, if the user wanted to only edit, say, their about me and leave their location and profile image the same, it sets the profile table to blank for the inputs that were not filled in by the user (the profile image and then location in this case).

this is my code:

$db = Database::getConnection();
		$query = "SELECT * FROM profile WHERE username=:username";
		$output = $db->prepare($query);
		$output->bindParam(':username', $s_username);
		$success = $output->execute();
		if ($success){
			$row = $output->fetch();
				//this part is to get the current info for each column
                $db_about = $row['about'];
				$db_location = $row['location'];
				$db_filePath = $row['image'];
				$rest = substr("".$db_filePath."", 8);  // removes "uploads/"
		}}
		
		$uploadDir = 'uploads/'; //Image Upload Folder
		if(isset($_POST['submit']))
		{
			$about    = $_POST['about'];
			$location = $_POST['location'];
			$fileName = $_FILES['Photo']['name'];
			$tmpName  = $_FILES['Photo']['tmp_name'];
			$fileSize = $_FILES['Photo']['size'];
			$fileType = $_FILES['Photo']['type'];		
			$filePath = $uploadDir . $fileName;
			$result = move_uploaded_file($tmpName, $filePath);

			if (!$result) {
				echo' An error occurred';
				exit();
			}
			if(!get_magic_quotes_gpc())
			{

				$fileName = addslashes($fileName);
				$filePath = addslashes($filePath);
			}
			$db = Database::getConnection();
			$query = "UPDATE profile SET image='$filePath',about='$about',location='$location' WHERE username=:username";
			$output = $db->prepare($query);
			$output->bindParam(':username', $s_username);
			$success = $output->execute();
				if($success){
					echo 'Upload complete<br><br>';
				} else {
					echo 'error occurred';
				}
			}
		
		?>

		<form name="editProfile" enctype="multipart/form-data" action="editProfile.php" method="POST">
			<textarea id="editProfile" name="about" placeholder="About Me"></textarea><br/>
			<input type="text" id="editProfile" name="location" placeholder="Location"><br/>
			<div id="profileUpload">
				<input type="file" name="Photo" size="2000000" accept="image/gif, image/jpeg, image/x-ms-bmp, image/x-png" size="26"><br/>
			</div>
			<!-- password to confirm -->
			<INPUT type="submit" class="button" name="submit" value="Submit"> 
		</form>





我希望只在更改其中一个输入时更新配置文件表。未更改的空白输入应该是:



1.不在SQL中更新,

2.或从中获取当前信息配置文件表以及在editprofile中未更新的每个输入,将变量设置为更新前表中的变量,这意味着它正在使用相同的信息进行更新。



I want to be able to update the profile table when only changing one of the inputs. the blank inputs that havent been changed should either:

1. Not update in the SQL,
2. Or get the current info from the profile table and for each input that wasnt updated in editprofile, set the variable as what was in the table before the update, meaning that it is being updated with the same information.

推荐答案

db = Database :: getConnection();
db = Database::getConnection();


query = SELECT * FROM profile WHERE username =:username;
query = "SELECT * FROM profile WHERE username=:username";


output =
output =


这篇关于如何更新数据库表而不必填写每个输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆