是否可以使用一种形式更新所有非空白(仅用户输入信息的输入)输入?因此,我有一个edit_profile.php页面,并且有一个输入列表。它也是一种形式。我仍然是PHP的初学者,所以我不太确定该怎么做。我知道PHP出了什么问题,我只是不知道如何解决。这是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>




和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");
}


因此,我知道我在“ UPDATE user_profiles”查询中出了错,我只是不知道如何在包含信息的情况下插入输入:)

最佳答案

您必须检查每个项目以查看其是否为空,然后构建查询:

$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'";
}


我真的建议使用PDOmysqli并使用参数来防止sql注入

10-08 08:35
查看更多