doujiu8826 2013-07-16 18:53
浏览 29
已采纳

PHP:更新除空白字段外的所有内容

Is it possible to update all non-blank (only the inputs the user puts information in) inputs with one form? So, I have an edit_profile.php page, and I have a list of inputs. It's in one form too. I'm still a beginner at PHP so I'm not really sure how to do it. I know where I am going wrong with the PHP, I just don't know a way around it. Here is the html;

<form action="edit_profile.php" method="post">
<fieldset>
    <legend>Edit your profile</legend>
    <label for="title">Member Title</label>
    <input type="text" name="title" id="title" placeholder="Member Title">
    <span class="help-block">This will be shown next to your posts. Only numbers, letters, and grammar symbols allowed</span>
    <hr class="soften">
    <label for="about">About you</label>
    <textarea name="about" id="about" placeholder="Tell us about yourself"></textarea>
    <span class="help-block">This information will be visible to other registered members. Only numbers, letters, and grammar symbols allowed</span>
    <hr class="soften">
    <label>Your gender</label>
    <p>I am...</p>
    <div class="btn-group" data-toggle="buttons-radio">
        <button type="button" class="btn btn-primary" id="gender" name="gender" value="Not Saying">Not Saying</button>
        <button type="button" class="btn btn-primary" id="gender" name="gender" value="Male">Male</button>
        <button type="button" class="btn btn-primary" id="gender" name="gender" value="Female">Female</button>
    </div>
    <span class="help-block">This is optional, leave as it is if you do not want to specify your gender.</span>
    <hr class="soften">
    <h3 class="muted">Social</h3>
    <div class="row-fluid">
        <div class="span4"> 
            <label for="social_facebook">Facebook</label>
            <input type="url" name="social_facebook" id="social_facebook" placeholder="Link to your Facebook page..." style="width: 100%">
        </div>
        <div class="span4">
            <label for="social_twitter">Twitter</label>
            <input type="url" name="social_twitter" id="social_twitter" placeholder="Link to your Twitter page..." style="width: 100%">
        </div>
        <div class="span4">
            <label for="social_youtube">YouTube</label>
            <input type="url" name="social_youtube" id="social_youtube" placeholder="Link to your YouTube page..." style="width: 100%">
        </div>
    </div>
    <span class="help-block">This information will be displayed on your profile if anything is entered</span>
    <hr class="soften">
    <label class="checkbox">
        <input type="hidden" name="view_profile" id="view_profile" value="0">
        <input type="checkbox" name="view_profile" id="view_profile" value="1"> View profile after saving?
    </label>
    <hr class="soften">
    <button type="submit" class="btn btn-primary">Save Profile</button>
    <a href="./" class="btn btn-inverse">Cancel</a>
</fieldset>

and the PHP;

$gender = '';
$id = $_SESSION['id'];

$title      = $_POST['title'];
$about      = $_POST['about'];
$gender     = $_POST['gender'];
$check      = $_POST['view_profile'];
$facebook   = $_POST['social_facebook'];
$twitter    = $_POST['social_twitter'];
$youtube    = $_POST['social_youtube'];

$errors = array();
if($title){
    $range = range(0,32);
    if(!in_array(strlen($title),$range)){
        $errors[] = "<div class='alert alert-danger'>Member Title has a character limit of 32 characters!</div>";
    }
}
# if(empty($gender)) {
#   $errors[] = "<div class='alert alert-danger'>Please select a gender!</div>";
# }
$title = addslashes(htmlspecialchars($title));
$about = addslashes(htmlspecialchars($about));

$facebook   = stripslashes($facebook);
$twitter    = stripslashes($twitter);
$youtube    = stripslashes($youtube);

if(count($errors) > 0){
    foreach($errors AS $error){
        echo '<div class="container">';
        echo $error;
        echo '</div>';
    }
} else {
    if(!empty($check)) {
        $url = "./profile.php?id=".$_SESSION['id']."";
        $_SESSION['profile_updated'] = "Your profile was successfully updated";
    } 
    if(empty($check)) {
        $url = './edit_profile.php';
        $_SESSION['profile_updated_edit'] = "Your profile was successfully updated. <a href='./profile.php?id=".$_SESSION['id']."'>View it here</a>";
    }
    $sql4 = "UPDATE 
            `user_profiles` 
            SET 
            `title` = '$title',`about` = '$about', `gender` = '$gender', `facebook` = '$facebook', `twitter` = '$twitter', `youtube` = '$youtube' 
            WHERE 
            `id` = '$id'";
    $res4 = mysql_query($sql4) or die(mysql_error());
    header("Location: $url");
}

So I know I am going wrong on the "UPDATE user_profiles query, I just do not know how to insert inputs if they contain information :)

  • 写回答

2条回答 默认 最新

  • dongtangu6144 2013-07-16 19:02
    关注

    You have to check each item to see if it's empty, and then build your query:

    $qString = '';
    if(!empty($title))
        $qString .= "`title` = '$title',";
    if(!empty($about))
        $qString .= "`about` = '$about',";
    if(!empty($gender))
        $qString .= "`gender` = '$gender',";
    if(!empty($check))
        $qString .= "`check` = '$check',";
    if(!empty($facebook))
        $qString .= "`facebook` = '$facebook',";
    if(!empty($twitter))
        $qString .= "`twitter` = '$twitter',";
    if(!empty($youtube))
        $qString .= "`youtube` = '$youtube',";
    
    $qString = trim($qString,',');
    
    if(!empty($qString)){
        $sql4 = "UPDATE
        `user_profiles`
        SET
        $qString
        WHERE
        `id` = '$id'";
    }
    

    I'd really recommend using PDO or mysqli and use parameters to prevent sql injection

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀