php - PDO says it updates the table, but it actually doesn't -
i'm trying use form update sql table first getting data (editrower.php) , setting values in form, using form update table (update_contactrequest.php) returns saying rower updated yet table not update.
editrower.php
<?php require('login.php'); ?> <?php include 'php/mysql_connect.php'; if(isset($_get['id'])){ $q = $db->prepare('select * rowercontacts id=:id limit 1'); $q->execute(array(':id'=>$_get['id'])); $row = $q->fetch(pdo::fetch_assoc); if($row){ echo ' <form method="post" action="php/update_contactrequest.php"><div class="col-xs-9 col-md-6 col-lg-6"> <div class="form-group"> <input type="hidden" name="id" id="id" value="'.$_get['id'].'"> <label for="firstname">first name</label> <input type"text" class="form-control" name="firstname" placeholder="first name" value="'.$row['firstname'].'" /> </div> <div class="form-group"> <label for="lastname">last name</label> <input type="text" class="form-control" name="lastname" placeholder="last name" value="'.$row['lastname'].'" /> </div> <br><br> <br><br> <input type="submit" class="btn btn-default" value="update" /> </div></form> '; } else{ echo 'no rower found'; } } else{ echo 'no rower found'; } ?>
update_contactrequest.php:
<?php session_start(); if($_session['loggedin'] == true){ $rower_id= $_post['id']; $firstname = $_post['firstname']; $lastname = $_post['lastname']; // connection database try { $bdd = new pdo('mysql:host=localhost;dbname=home','username','password'); } catch(exception $e) { exit('unable connect database.'); } // insert records $sql = "update rowercontacts set firstname=:firstname, lastname=:lastname id=:rower_id"; $q = $bdd->prepare($sql); if($q->execute(array(':firstname'=>$firstname, ':lastname'=>$lastname, ':rower_id'=>$id))){ echo '<script type="text/javascript">alert("rower updated.");location.href="../rowerlist.php";</script>'; } else{ echo '<script type="text/javascript">alert("something went wrong...");location.href="../rowerlist.php";</script>'; } } ?>
with $q->rowcount()
. prepared statements return number of affected rows.
if query error free , executes fine, need affected rows.
$q = $bdd->prepare($sql); if($q->execute(array(':firstname'=>$firstname, '...'))){ $updrows = $q->rowcount(); if($updrows==0){ echo '<script type="text/javascript">alert("affected rows = 0 !!!");location.href="../rowerlist.php";</script>'; } else{ echo '<script type="text/javascript">alert("rows affected : '.$updrows.'");location.href="../rowerlist.php";</script>'; } } else{ echo '<script type="text/javascript">alert("something went wrong...");location.href="../rowerlist.php";</script>'; }
- over 70% of update queries 0 affected rows due incorrect
where
rest comes attempt replacerecord
exactly same values
exist. - the first thing in such case, let query readable text display.
$q->debugdumpparams(); query array. where id = null
not expected.
to problem i'm sure can find wrong part yourself in following 3 lines . :-)
$rower_id= $_post['id']; .... $sql = "update rowercontacts ... id=:rower_id"; if($q->execute(array(':firstname'=>$firstname,...,':rower_id'=>$id)))
Comments
Post a Comment