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 八爪鱼爬数据为什么自己停了
    • ¥15 交替优化波束形成和ris反射角使保密速率最大化
    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率