Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP: Update everything but blank fields
    primarykey
    data
    text
    <p>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;</p> <pre><code>&lt;form action="edit_profile.php" method="post"&gt; &lt;fieldset&gt; &lt;legend&gt;Edit your profile&lt;/legend&gt; &lt;label for="title"&gt;Member Title&lt;/label&gt; &lt;input type="text" name="title" id="title" placeholder="Member Title"&gt; &lt;span class="help-block"&gt;This will be shown next to your posts. Only numbers, letters, and grammar symbols allowed&lt;/span&gt; &lt;hr class="soften"&gt; &lt;label for="about"&gt;About you&lt;/label&gt; &lt;textarea name="about" id="about" placeholder="Tell us about yourself"&gt;&lt;/textarea&gt; &lt;span class="help-block"&gt;This information will be visible to other registered members. Only numbers, letters, and grammar symbols allowed&lt;/span&gt; &lt;hr class="soften"&gt; &lt;label&gt;Your gender&lt;/label&gt; &lt;p&gt;I am...&lt;/p&gt; &lt;div class="btn-group" data-toggle="buttons-radio"&gt; &lt;button type="button" class="btn btn-primary" id="gender" name="gender" value="Not Saying"&gt;Not Saying&lt;/button&gt; &lt;button type="button" class="btn btn-primary" id="gender" name="gender" value="Male"&gt;Male&lt;/button&gt; &lt;button type="button" class="btn btn-primary" id="gender" name="gender" value="Female"&gt;Female&lt;/button&gt; &lt;/div&gt; &lt;span class="help-block"&gt;This is optional, leave as it is if you do not want to specify your gender.&lt;/span&gt; &lt;hr class="soften"&gt; &lt;h3 class="muted"&gt;Social&lt;/h3&gt; &lt;div class="row-fluid"&gt; &lt;div class="span4"&gt; &lt;label for="social_facebook"&gt;Facebook&lt;/label&gt; &lt;input type="url" name="social_facebook" id="social_facebook" placeholder="Link to your Facebook page..." style="width: 100%"&gt; &lt;/div&gt; &lt;div class="span4"&gt; &lt;label for="social_twitter"&gt;Twitter&lt;/label&gt; &lt;input type="url" name="social_twitter" id="social_twitter" placeholder="Link to your Twitter page..." style="width: 100%"&gt; &lt;/div&gt; &lt;div class="span4"&gt; &lt;label for="social_youtube"&gt;YouTube&lt;/label&gt; &lt;input type="url" name="social_youtube" id="social_youtube" placeholder="Link to your YouTube page..." style="width: 100%"&gt; &lt;/div&gt; &lt;/div&gt; &lt;span class="help-block"&gt;This information will be displayed on your profile if anything is entered&lt;/span&gt; &lt;hr class="soften"&gt; &lt;label class="checkbox"&gt; &lt;input type="hidden" name="view_profile" id="view_profile" value="0"&gt; &lt;input type="checkbox" name="view_profile" id="view_profile" value="1"&gt; View profile after saving? &lt;/label&gt; &lt;hr class="soften"&gt; &lt;button type="submit" class="btn btn-primary"&gt;Save Profile&lt;/button&gt; &lt;a href="./" class="btn btn-inverse"&gt;Cancel&lt;/a&gt; &lt;/fieldset&gt; </code></pre> <p></p> <p>and the PHP;</p> <pre><code>$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[] = "&lt;div class='alert alert-danger'&gt;Member Title has a character limit of 32 characters!&lt;/div&gt;"; } } # if(empty($gender)) { # $errors[] = "&lt;div class='alert alert-danger'&gt;Please select a gender!&lt;/div&gt;"; # } $title = addslashes(htmlspecialchars($title)); $about = addslashes(htmlspecialchars($about)); $facebook = stripslashes($facebook); $twitter = stripslashes($twitter); $youtube = stripslashes($youtube); if(count($errors) &gt; 0){ foreach($errors AS $error){ echo '&lt;div class="container"&gt;'; echo $error; echo '&lt;/div&gt;'; } } 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. &lt;a href='./profile.php?id=".$_SESSION['id']."'&gt;View it here&lt;/a&gt;"; } $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"); } </code></pre> <p>So I know I am going wrong on the "UPDATE <code>user_profiles</code> query, I just do not know how to insert inputs if they contain information :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload