我有一个表单来更新用户配置文件,并使用一些PHP代码来检查字段是否为空,如果该字段不显示但是它会清除数据库中该字段中的信息。我怎么能阻止这种情况发生?
这是我的代码:
<?php
if (isset($_GET['success']) === true && empty($_GET['success']) === true) {
echo 'Your details have been updated!';
} else {
if (empty($_POST) === false && empty($errors) === true) {
$update_data = array(
'aboutme' => $_POST['aboutme'],
'facebook' => $_POST['facebook'],
'twitter' => $_POST['twitter'],
'google' => $_POST['google'],
'linked' => $_POST['linked'],
'pokerstars' => $_POST['pokerstars'],
'pokerstarsfr' => $_POST['pokerstarsfr'],
'888poker' => $_POST['888poker'],
'fullflush' => $_POST['fullflush'],
'partypoker' => $_POST['partypoker'],
);
update_user($session_user_id, $update_data);
header('Location: settings.php?success');
exit();
} else if (empty($errors) === false) {
echo output_errors($errors);
}
}
?>
这是表单代码:
<form class="margin-bottom-40" role="form" method="post" action="">
<div class="form-group">
<label>About Me</label><br>
<textarea class="form-control" rows="15" name="aboutme"><?php echo $user_data['aboutme']; ?></textarea>
</div>
<div class="form-group">
<label>Facebook</label>
<input type="text" class="form-control" name="facebook" placeholder="Please start with www." value="<?php echo $user_data['facebook']; ?>">
</div>
<div class="form-group">
<label>Twitter</label>
<input type="text" class="form-control" name="twitter" placeholder="Please start with www." value="<?php echo $user_data['twitter']; ?>">
</div>
<div class="form-group">
<label>Google+</label>
<input type="text" class="form-control" name="google" placeholder="Please start with www." value="<?php echo $user_data['google']; ?>">
</div>
<div class="form-group">
<label>LinkedIn</label>
<input type="text" class="form-control" name="linked" placeholder="Please start with www." value="<?php echo $user_data['linked']; ?>">
</div>
<input type="hidden" value="<?php echo $user_data['email']; ?>" name="email">
<h3>Poker ID's</h3>
Please note: You can only enter your poker ID once, after this it can NOT be changed.<br><br>
<div class="form-group">
<label>PokerStars ID</label>
<input type="text" class="form-control" value="<?php echo $user_data['pokerstars']; ?>" name="pokerstars" <?php if(!empty($user_data['pokerstars'])) { echo 'disabled'; } ?> >
</div>
<div class="form-group">
<label>PartyPoker ID</label>
<input type="text" class="form-control" value="<?php echo $user_data['partypoker']; ?>" name="partypoker" <?php if(!empty($user_data['partypoker'])) { echo 'disabled'; } ?> >
</div>
<div class="form-group">
<label>Full Flush Poker ID</label>
<input type="text" class="form-control" value="<?php echo $user_data['fullflush']; ?>" name="fullflush" <?php if(!empty($user_data['fullflush'])) { echo 'disabled'; } ?> >
</div>
<?php if (empty($user_data['888poker'])): ?>
<div class="form-group">
<label>888 Poker ID</label>
<input type="text" name="888poker" class="form-control" value="<?php echo $user_data['888poker']; ?>">
</div>
<?php endif; ?>
<?php if (empty($user_data['pokerstarsfr'])): ?>
<div class="form-group">
<label>PokerStars.fr ID</label>
<input type="text" name="pokerstarsfr" class="form-control">
</div>
<?php endif; ?>
<br><br>
<button type="submit" class="btn-u">Update!</button>
</form>
我已经尝试了几种方法,你可以从表单中的不同类型的PHP代码中看到,但还没有运气。
答案 0 :(得分:2)
将表单字段设置为已禁用时,它甚至不会发送到服务器。如果您只想阻止用户编辑字段,则应使用 readonly 。
此外,如果您不希望用户更改该值,您必须确保服务器端的值不会更改。它很容易从字段中删除readonly
属性。
答案 1 :(得分:1)
将代码的$ update_data部分更改为以下内容:
$update_data = array();
$update_array = array(
'aboutme',
'facebook',
'twitter',
'google',
'linked',
'pokerstars',
'pokerstarsfr',
'888poker',
'fullflush',
'partypoker')
);
foreach($update_array as $social) {
if(!empty($_POST[$social])) {
$update_data[$social] = $_POST[$social];
}
}