I believe i am almost to the solution but in webdesign almost means next to nothing. If someone wouldn't mind helping me figure out where i am causing an error or need to do more that would be great. Basically i am trying to teach my self sessions and relearn php cause it has been many years. I want to give the user the option to select what field in their profile they would like to change and then submit a new value. when i hit submit i get this error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''lastname' = 'ZIPPI-DO-DAH' WHERE username = 'eric'' at line 1' in /Applications/MAMP/htdocs/TESTING/PHP/PHP-TEST/memberProfile.php:22 Stack trace: #0 /Applications/MAMP/htdocs/TESTING/PHP/PHP-TEST/memberProfile.php(22): PDOStatement->execute(Array) #1 {main} thrown in /Applications/MAMP/htdocs/TESTING/PHP/PHP-TEST/memberProfile.php on line 22
Here is my (NOW FIXED)code:
if(isset($_POST['submitUpdate']) && isset($_SESSION['username'])){
$selectField = $_POST['select'];
$newValue = $_POST['new_value'];
$sth = $conn->prepare("UPDATE Testing SET $selectField = :newValue WHERE username = :username");
$sth->execute(array( ':newValue'=> $newValue, ':username'=> $_SESSION['username'] ));
header("Locatioon: member.php");
echo 'Field has been updated, thank you.';
}
Here is my form:
<form id="updateProfile" method="POST">
<div class="inputField">
<label for="select">Select what field you want to change below.(firstname is default)</label>
<select name="select" id="selectUpdate">
<option value="firstname" selected>First name</option>
<option value="lastname">Last name</option>
<option value="email">Email</option>
<option value="message">Message</option>
<option value="username">Username</option>
<option value="password">Password</option>
</select>
</div>
<div class="inputField">
<label for="new_value">New value - place the new value here</label>
<input type="text" name="new_value" id="new_value" placeholder="New Value" tabindex="2">
</div>
<div class="inputField">
<button class="button" name="submitUpdate" id="submitUpdate" tabindex="4">Submit Update</button>
</div>
</form>