mysql - Attempt to get a php prepare to work with two different tables -


i attempting first query send 2 different db tables. trying update 'group' in users , user_request table. getting id ajax call, using id find record trying update.

in users table id need find id field. in user_requests table id need associate user_id.

this line trying change make send 2 different db tables..

$stmt = $con->prepare("update users,user_reuqests set `group`=? id, user_id=?"); 

i'm getting error responce saying error user_id part.

$approved_id = $_post['id']; $change_group = $_post['update_group'];  $con = mysqli_connect("localhost","root","","db");     /* check connection */     if (mysqli_connect_errno()) {         printf("connect failed: %s\n", mysqli_connect_error());         exit();     }     $stmt = $con->prepare("update users,user_reuqests set `group`=? id, user_id=?");     if ( !$stmt || $con->error ) {      // check errors prepare         die('user group update prepare() failed: ' . htmlspecialchars($con->error));     }     if(!$stmt->bind_param('ii', $change_group, $approved_id)) {     // check errors binding parameters         die('user group update bind_param() failed: ' . htmlspecialchars($stmt->error));     }     if(!$stmt->execute()) {         die('user group update execute() failed: ' . htmlspecialchars($stmt->error));     }  

what doing wrong not work , conjoin?

update: after changed prepare part of this, i'm getting errors in bind_param part of prepared statement. how can change this?

$stmt = $con->prepare("update users,user_requests set users.group=?, user_requests.group=? users.id=? , user_requests.user_id=?"); 

first, clause in query doesn't specify id first constraint.

second, group ambiguous , cause errors when try update it.

your query should read:update users,user_reuqests set users.group=?, user_request.group=? users.id=? , user_request.user_id=?

now, since we've updated query more place holders, need bind these additional placeholders php variables. new query uses both $change_group , $approved_id twice - need bind each of them twice.

if(!$stmt->bind_param('iiii', $change_group, $change_group, $approved_id, $approved_id)) {     // check errors binding parameters         die('user group update bind_param() failed: ' . htmlspecialchars($stmt->error));     } 

when said , done, final code should this:

$approved_id = $_post['id']; $change_group = $_post['update_group'];  $con = mysqli_connect("localhost","root","","db");     /* check connection */     if (mysqli_connect_errno()) {         printf("connect failed: %s\n", mysqli_connect_error());         exit();     }     $stmt = $con->prepare("update users,user_reuqests set users.group=?, user_request.group=? users.id=? , user_request.user_id=?");     if ( !$stmt || $con->error ) {      // check errors prepare         die('user group update prepare() failed: ' . htmlspecialchars($con->error));     }     if(!$stmt->bind_param('iiii', $change_group, $change_group, $approved_id, $approved_id)) {     // check errors binding parameters         die('user group update bind_param() failed: ' . htmlspecialchars($stmt->error));     }     if(!$stmt->execute()) {         die('user group update execute() failed: ' . htmlspecialchars($stmt->error));     }  

more info on binding parameters mysqli_stmt here: http://php.net/manual/en/mysqli-stmt.bind-param.php


Comments