Okay Leute, im Grunde, was ich versuche zu fragen, ist dies: Ich habe eine Profilseite auf meiner Website. Jetzt gibt der Benutzer den Vor- und Nachnamen ein. Später wollen sie zum Beispiel ihren Vornamen aktualisieren. Nun, Update-Syntax in einer einzigen Abfrage würde das gesamte Feld für diese ID richtig?Was ist die empfohlene Vorgehensweise zum teilweisen Aktualisieren von SQL-Tabellen und Speichern von unbearbeiteten Werten?
Ich versuche, den ursprünglichen Wert des letzten Namens beizubehalten, selbst wenn die Tabelle für den Vornamen aktualisiert wird. Ich dachte, so etwas würde funktionieren.
$database = new Database;
if(isset($_POST['submit'])) {
$id = isset($_POST['user_id'])?$_POST['user_id']:null;
$fname = isset($_POST['fname'])?$_POST['fname']:null;
$lname = isset($_POST['lname'])?$_POST['lname']:null;
$profession = isset($_POST['profession'])?$_POST['profession']:null;
$phone = isset($_POST['phone'])?$_POST['phone']:null;
$fax = isset($_POST['fax'])?$_POST['fax']:null;
$filtered_email = isset($_POST['email'])?$_POST['email']:null;
$workbio = isset($_POST['workbio'])?$_POST['workbio']:null;
$employers = isset($_POST['employers'])?$_POST['employers']:null;
$years = isset($_POST['years_in_industry'])? $_POST['years_in_industry']:null;
$database->query('UPDATE users
SET firstname = COALESCE(:fname,firstname),
lastname = COALESCE(:lname,lastname),
profession = COALESCE(:profession,profession),
phone = COALESCE(:phone,phone),
fax = COALESCE(:fax,fax),
email = COALESCE(:email,email),
projects = COALESCE(:workbio,projects),
companies = COALESCE(:employers,companies),
exp_years = COALESCE(:years_in_industry,exp_years)
WHERE user_id = :id');
$database->bind(':id', $id);
$database->bind(':fname', $fname);
$database->bind(':lname', $lname);
$database->bind(':profession', $profession);
$database->bind(':phone', $phone);
$database->bind(':fax', $fax);
$database->bind(':email', $filtered_email);
$database->bind(':workbio', $workbio);
$database->bind(':employers', $employers);
$database->bind(':years_in_industry', $years);
$database->execute();
}
das sind meine Datenbank-Feldnamen: user_id, Vorname, Nachname, Beruf, Telefon, Fax, E-Mail, Projekte, Unternehmen, exp_years, acct_date
So, hier ist mein HTML-Formular:
<div class="content">
<!-- You only need this form and the form-basic.css -->
<form class="form-basic" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<div class="form-title-row">
<h1>Profile:</h1>
</div>
<div class="form-row">
<label>
<span>id:</span>
<input type="hidden" name="id">
</label>
</div>
<div class="form-row">
<label>
<span>Profession:</span>
<input type="text" name="profession">
</label>
</div>
<div class="form-row">
<label>
<span>First name</span>
<input type="text" name="fname">
</label>
</div>
<div class="form-row">
<label>
<span>Last name</span>
<input type="text" name="lname">
</label>
</div>
<div class="form-row">
<label>
<span>Email:</span>
<input type="email" name="email">
</label>
</div>
<div class="form-row">
<label>
<span>Phone:</span>
<input type="tel" name="tel">
</label>
</div>
<div class="form-row">
<label>
<span>Fax:</span>
<input type="tel" name="fax">
</label>
</div>
<div class="form-row">
<label>
<span>Companies I have worked with:</span>
<textarea name="employers"></textarea>
</label>
</div>
<div class="form-row">
<label>
<span>Projects I've worked on:</span>
<textarea name="workbio"></textarea>
</label>
</div>
<div class="form-row">
<label>
<span>Years I've been in my industry:</span>
<select name="years_in_industry">
<option>1 year</option>
<option>2+ years</option>
<option>5+ years</option>
<option>10+ years</option>
</select>
</label>
</div>
<div class="form-row">
<label><span>Radio</span></label>
<div class="form-radio-buttons">
<div>
<label>
<input type="radio" name="radio">
<span>Radio option 1</span>
</label>
</div>
<div>
<label>
<input type="radio" name="radio">
<span>Radio option 2</span>
</label>
</div>
<div>
<label>
<input type="radio" name="radio">
<span>Radio option 3</span>
</label>
</div>
</div>
</div>
<div class="form-row">
<button type="submit">Submit Form</button>
</div>
</form>
Danke.
Ich verstehe das Problem nicht. – Strawberry
@Strawberry im Grunde versuche ich zu verstehen, wie Update-Funktionalität funktionieren würde? Wird es alle Felder in der Datenbank aktualisieren, auch wenn Sie nur ein Formularfeld aktualisieren? –
Normalerweise würden Sie die Abfrage dynamisch erstellen und nur die Schlüssel mit einem Wert übergeben. – Strawberry