dousenjue3214 2014-12-15 01:02
浏览 43

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

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>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用