Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP PDO Update prepared statement problem
    primarykey
    data
    text
    <p>Hi everyone I've been trying my hand at PDO recently, and am currently trying to write a basic database class for a project i'm working on. However i have ran into problems trying to write a function for carrying out an update query using prepared statements.</p> <pre><code> function update($tabledata, $table, $where){ $fields = array_keys($tabledata); $data = array_values($tabledata); $fieldcount = count($fields); $wherefield = implode(array_keys($where)); $whereval = implode(array_values($where)); $this-&gt;query = "UPDATE $table SET "; $this-&gt;query .= '(' . implode($fields, ' = ?, ') . ' = ?)'; $this-&gt;query .= " WHERE $wherefield = '$whereval'"; $this-&gt;query = $this-&gt;_clean($this-&gt;query); $stmt = $this-&gt;conn-&gt;prepare($this-&gt;query) or die('Problem preparing query'); $stmt-&gt;execute($data)or die('Problem executing query'); } </code></pre> <p>An example of it's use would be:</p> <pre><code> $usertbl = 'users'; $date = date("Y-m-d"); $updatedata = array( 'Username' =&gt; 'test', 'Password' =&gt; 'unknown', 'Email' =&gt; 'email', ); $where = array( 'Username' =&gt; 'user' ); $Database-&gt;update($updatedata,$usertbl,$where); </code></pre> <p>This returns the following error:</p> <blockquote> <p>Warning: PDOStatement::execute() [pdostatement.execute]: 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 '(Username = 'test', Password = 'unknown', Email = 'email') WHERE Username = 'use' at line 1</p> </blockquote> <p>Any help would be much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload