php - Update Record with PDO keep getting an error -



php - Update Record with PDO keep getting an error -

i trying update row or record , maintain getting error:

parse error: syntax error, unexpected '$sql' (t_variable) in c:\wamp\www\systems\update_process.php on line 11

i'm not sure how go fixing tried many things.

<?php $db_host = "localhost"; $db_username = "root"; $db_pass = ""; $db_name = "systems_requests"; try{ $db = new pdo('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass); $db->setattribute(pdo::attr_errmode, pdo::errmode_warning) $sql = 'update requests set lanid= :lanid, name= :name, department= :department,manager= :manager,request= :request,request_description= :request_description, request_comments= :request_comments,status= :status,comments= :comments,compuser= :compuser, compdt= :comdt id= :id'; $stmt = $pdo->prepare($sql); $stmt->bindparam(':id', $_post['lanid'], pdo::param_int); $stmt->bindparam(':lanid', $_post['lanid'], pdo::param_str); $stmt->bindparam(':name', $_post['$name'], pdo::param_str); $stmt->bindparam(':department', $_post['department'], pdo::param_str); $stmt->bindparam(':manager', $_post['manager'], pdo::param_str); $stmt->bindparam(':request', $_post['request'], pdo::param_str); $stmt->bindparam(':request_description', $_post['request_description'], pdo::param_str); $stmt->bindparam(':request_comments', $_post['request_comments'], pdo::param_str); $stmt->bindparam(':status', $_post['status'], pdo::param_str); $stmt->bindparam(':comments', $_post['comments'], pdo::param_str); $stmt->bindparam(':compuser', $_post['compuser'], pdo::param_str); $stmt->bindparam(':comdt', $_post['comdt'], pdo::param_int); $stmt->execute(); header('location:index.php'); }catch(pdoexception $exception){ echo "error: " . $exception->getmessage(); } ?>

revised code still not working error message fatal error: phone call fellow member function prepare() on non-object in c:\wamp\www\systems\update_process.php on line 12

<?php $db_host = "localhost"; $db_username = "root"; $db_pass = ""; $db_name = "systems_requests"; try{ $db = new pdo('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass); $db->setattribute(pdo::attr_errmode, pdo::errmode_warning); $db = 'update requests set id=:id, lanid= :lanid, name= :name, department= :department,manager= :manager,request= :request,request_description= :request_description, request_comments= :request_comments,status= :status,comments= :comments,compuser= :compuser, compdt= :comdt id= :id'; $stmt = $db->prepare($sql); $stmt->bindparam(':id', $_post['id'], pdo::param_int); $stmt->bindparam(':lanid', $_post['lanid'], pdo::param_str); $stmt->bindparam(':name', $_post['$name'], pdo::param_str); $stmt->bindparam(':department', $_post['department'], pdo::param_str); $stmt->bindparam(':manager', $_post['manager'], pdo::param_str); $stmt->bindparam(':request', $_post['request'], pdo::param_str); $stmt->bindparam(':request_description', $_post['request_description'], pdo::param_str); $stmt->bindparam(':request_comments', $_post['request_comments'], pdo::param_str); $stmt->bindparam(':status', $_post['status'], pdo::param_str); $stmt->bindparam(':comments', $_post['comments'], pdo::param_str); $stmt->bindparam(':compuser', $_post['compuser'], pdo::param_str); $stmt->bindparam(':comdt', $_post['comdt'], pdo::param_int); $stmt->execute(); header('location:index.php'); }catch(pdoexception $exception){ echo "error: " . $exception->getmessage(); } ?>

correct code , working

class="snippet-code-html lang-html prettyprint-override"><?php $db_host = "localhost"; $db_username = "root"; $db_pass = ""; $db_name = "systems_requests"; try{ $db = new pdo('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass); $db->setattribute(pdo::attr_errmode, pdo::errmode_warning); $sql = 'update requests set id=:id, lanid= :lanid, name= :name, department= :department,manager= :manager,request= :request,request_description= :request_description, request_comments= :request_comments,status= :status,comments= :comments,compuser= :compuser, compdt= :comdt id= :id'; $stmt = $db->prepare($sql); $stmt->bindparam(':id', $_post['id'], pdo::param_int); $stmt->bindparam(':lanid', $_post['lanid'], pdo::param_str); $stmt->bindparam(':name', $_post['$name'], pdo::param_str); $stmt->bindparam(':department', $_post['department'], pdo::param_str); $stmt->bindparam(':manager', $_post['manager'], pdo::param_str); $stmt->bindparam(':request', $_post['request'], pdo::param_str); $stmt->bindparam(':request_description', $_post['request_description'], pdo::param_str); $stmt->bindparam(':request_comments', $_post['request_comments'], pdo::param_str); $stmt->bindparam(':status', $_post['status'], pdo::param_str); $stmt->bindparam(':comments', $_post['comments'], pdo::param_str); $stmt->bindparam(':compuser', $_post['compuser'], pdo::param_str); $stmt->bindparam(':comdt', $_post['comdt'], pdo::param_int); $stmt->execute(); }catch(pdoexception $exception){ echo "error: " . $exception->getmessage(); } ?>

$db->setattribute(pdo::attr_errmode, pdo::errmode_warning) $sql = 'update requests set lanid= :lanid, name= :name, department= :department,manager= :manager,request= :request,request_description= :request_description, request_comments= :request_comments,status= :status,comments= :comments,compuser= :compuser, compdt= :comdt id= :id'; $stmt = $pdo->prepare($sql);

lets review:

this line:

$db->setattribute(pdo::attr_errmode, pdo::errmode_warning)

is triggering error, php cannot process next line. solution:

$db->setattribute(pdo::attr_errmode, pdo::errmode_warning);

and in comment:

undefined variable: pdo in c:\wamp\www\systems\update_process.php on line 12 , phone call fellow member function prepare()

this because have assigned pdo class $db in line:

$db = new pdo('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);

so solution:

$stmt = $db->prepare($sql);

edit:

fatal error: phone call fellow member function prepare()

this because sql query returning false, can happen many many reasons. typos in of query.. trying phone call non-existant table/column names, table not exist. list goes on. debugging purposes. recommend changing code to:

if (!stmt){ print_r($db->errorinfo()); }

at end, seems try/catch isn't capturing exceptions beingness thrown. show error , assist in updating code query beingness executed correctly.

it seems have updated question posting new code. there collisions variables:

$db = new pdo('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass); $db->setattribute(pdo::attr_errmode, pdo::errmode_warning); $db = 'update requests set id=:id, lanid= :lanid, name= :name, department= :department,manager= :manager,request= :request,request_description= :request_description, request_comments= :request_comments,status= :status,comments= :comments,compuser= :compuser, compdt= :comdt id= :id'; $stmt = $db->prepare($sql);

what doing here setting variable $db accessor/constructor of pdo class, 3 lines downwards on writing variable string. there no database connection in $db. solution simple alter in variable names:

$db = new pdo('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass); $db->setattribute(pdo::attr_errmode, pdo::errmode_warning); $sql= 'update requests set id=:id, lanid= :lanid, name= :name, department= :department,manager= :manager,request= :request,request_description= :request_description, request_comments= :request_comments,status= :status,comments= :comments,compuser= :compuser, compdt= :comdt id= :id'; $stmt = $db->prepare($sql);

back original

php mysql pdo

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -