尝试使用pdo php动态创建查询

时间:2014-12-15 01:02:12

标签: php sql pdo

我相信我几乎是解决方案,但在网页设计中几乎意味着什么都没有。如果有人不介意帮我弄清楚我在哪里导致错误或需要做更多的事情会很棒。基本上我正在尝试教我的自我会话和重新学习PHP因为它已经很多年了。我想让用户选择他们想要更改的个人资料中的哪个字段,然后提交新值。当我点击提交时,我收到此错误:

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

这是我的(NOW FIXED)代码:

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.';
    }

这是我的表格:

<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>

0 个答案:

没有答案