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 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比