1

Ok, i try to make an update query with PDO MYSQL so to learn how make it work and then make a function to make it more efficiently

The problem is doesn't work, i do all the debugg the Log files allow me, and the personal returns too, i alsy fix wherever the navigator tell & even with Firebug

Any ideas what can be?

Any suggestion, question, comment or any kind of answer than help to improve, clarify or answer the question would be much aprreciated

Thanks in advance

<?php
include('conexion.php');
session_start();


 $idf = $_POST['Id_Msg'];
 $ids = (int) $idf;

 var_dump($ids);
  echo '<br /><br />';
 var_dump($_FILES);

 echo '<br /><br />';
 var_dump($_POST);
 echo '<br /><br />';

  $wlq = -1;

 if($_POST['Title']) {
     $wlq++;
 }
 if($_POST['Prelude']) {
     $wlq++;
 }

 if($_POST['Text']) {
     $wlq++;
 }

 if($_POST['NOMBR1']) {
     $wlq++;
 }

 if($_POST['NOMBR2']) {
     $wlq++;
 }

 if($_POST['NOMBR3']) {
     $wlq++;
 }

 if($_POST['NOMBR4']) {
     $wlq++;
 }

 if($_POST['REGIN']) {
     $wlq++;
 }

 if($_POST['CTGRY']) {
     $wlq++;
 }

 if(!empty($_FILES['RutIm']['name'])) {
     $wlq++;
     echo 'me esta detectando visteh!';
 }

 echo "Asignado es: ".$wlq.'<br /><br />';

 $qls = "UPDATE `cmtmplo` SET";

 if($_POST['Title']) {
     if($wlq > 0){
    $qls .=" `Title` = :Title , ";
    $wlq--;
     } else {
    $qls .=" `Title` = :Title";
    }
 }

 if($_POST['Prelude']) {
     if($wlq > 0){
    $qls .=" `Prelude` = :Prelude , ";
    $wlq--;
     } else {
    $qls .=" `Prelude` = :Prelude";
    }
 }

 if($_POST['Text']) {
     if($wlq > 0){
    $qls .=" `Text` = :Text , ";
    $wlq--;
     } else {
    $qls .=" `Text` = :Text";
    }
 }

 if($_POST['NOMBR1']) {
     if($wlq > 0){
    $qls .=" `NOMBR1` = :NOMBR1 , ";
    $wlq--;
     } else {
    $qls .=" `NOMBR1` = :NOMBR1";
    }
 }

 if($_POST['NOMBR2']) {
     if($wlq > 0){
    $qls .=" `NOMBR2` = :NOMBR2 , ";
    $wlq--;
     } else {
    $qls .=" `NOMBR2` = :NOMBR2";
    }
 }

 if($_POST['NOMBR3']) {
     if($wlq > 0){
    $qls .=" `NOMBR3` = :NOMBR3 , ";
    $wlq--;
     } else {
    $qls .=" `NOMBR3` = :NOMBR3";
    }
 }

 if($_POST['NOMBR4']) {
     if($wlq > 0){
    $qls .=" `NOMBR4` = :NOMBR4 , ";
    $wlq--;
     } else {
    $qls .=" `NOMBR4` = :NOMBR4";
    }
 }

 if($_POST['REGIN']) {
     if($wlq > 0){
    $qls .=" `REGIN` = :REGIN , ";
    $wlq--;
     } else {
    $qls .=" `REGIN` = :REGIN";
    }
 }

 if($_POST['CTGRY']) {
     if($wlq > 0){
    $qls .=" `CTGRY` = :CTGRY , ";
    $wlq--;
     } else {
    $qls .=" `CTGRY` = :CTGRY";
    }
 }

 if(!empty($_FILES['RutIm']['name'])) {
     if($wlq > 0){
    $qls .=" `RutIm` = :RutIm , ";
    $wlq--;
     } else {

    }
 }

 $qls .= " WHERE 'Id_Msg' = :ids";
 echo $qls;
 $stmt = $con->prepare($qls);
 $stmt->bindValue(':ids', $ids);

 if(isset($_POST['Title']) && !empty($_POST['Title'])) {
    $stmt->bindValue('Title', $_POST['Title']);
 } 

 if(isset($_POST['Prelude']) && !empty($_POST['Prelude'])) {
    $stmt->bindValue('Prelude', $_POST['Prelude']);
 } 

 if(isset($_POST['Text']) && !empty($_POST['Text'])) {
    $stmt->bindValue('Text', $_POST['Text']);
 } 

 if(isset($_POST['NOMBR1']) && !empty($_POST['NOMBR1'])) {
    $stmt->bindValue('NOMBR1', $_POST['NOMBR1']);
 } 

 if(isset($_POST['NOMBR2']) && !empty($_POST['NOMBR2'])) {
    $stmt->bindValue('NOMBR2', $_POST['NOMBR2']);
 } 

 if(isset($_POST['NOMBR3']) && !empty($_POST['NOMBR3'])) {
    $stmt->bindValue('NOMBR3', $_POST['NOMBR3']);
 } 

 if(isset($_POST['NOMBR4']) && !empty($_POST['NOMBR4'])) {
    $stmt->bindValue('NOMBR4', $_POST['NOMBR4']);
 }

 if(isset($_POST['REGIN']) && !empty($_POST['REGIN'])) {
    $stmt->bindValue('REGIN', $_POST['REGIN']);
 }

 if(isset($_POST['CTGRY']) && !empty($_POST['CTGRY'])) {
    $stmt->bindValue('CTGRY', $_POST['CTGRY']);
 }

 if(!empty($_FILES['RutIm']['name'])) {
    $carpeta = "images/";
    opendir($carpeta);
    $destiny = $carpeta.$_FILES['RutIm']['name'].".jpg";
    copy($_FILES['RutIm']['tmp_name'],$destiny);
    $skr=$_FILES['RutIm']['name'];
    $stmt->bindValue('RutIm', $skr);    
 }
$stmt->execute(); 
?>

echo $qls output

UPDATE cmtmplo SET Title = :Title , REGIN = :REGIN , CTGRY = :CTGRY WHERE 'Id_Msg' = :ids

6
  • so what is your echo $qls; outputs? Commented Feb 24, 2015 at 18:03
  • it depends of what parts i add text and what others not, but this is what returns with the current tests UPDATE cmtmplo SET Title = :Title , REGIN = :REGIN , CTGRY = :CTGRY WHERE 'Id_Msg' = :ids Commented Feb 24, 2015 at 20:37
  • so show me the output with $qls please? Commented Feb 24, 2015 at 20:46
  • how can i get the output in this case of PDO Mysql? Commented Feb 24, 2015 at 21:11
  • echo $qls; before your $con->prepare! Commented Feb 24, 2015 at 21:17

2 Answers 2

2

PHP PDO provides a good way of finding out what might be wrong with the query. PDOStatement::execute() only returns true or false by default. It won't return any specific detail.

For detailed error information, you can use:

var_dump($con->errorInfo());

For more details, refer to the documentation page: http://php.net/manual/en/pdo.errorinfo.php

Sign up to request clarification or add additional context in comments.

4 Comments

Also $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); will enable exception-based error handling if that sort of thing turns your crank.
could you instance it in this code case?, i'm not so used to use this error handlers, thanks
@user57129 in your code specifically, you can do something like: if($stmt->execute()){ echo 'Successfully updated' } else { print_r($con->errorInfo()) }
Thanks, i'll add it but is returning 'Successfully updated' However the database remains unchanged
1

you need change all labels like 'Title' to ':Title':

 if(isset($_POST['Title']) && !empty($_POST['Title'])) {
    $stmt->bindValue(':Title', $_POST['Title']);
 } 

....

and so on... all parameters you bind must start with :

so now we can see that you should change

$qls .= " WHERE 'Id_Msg' = :ids";

to

$qls .= " WHERE `Id_Msg` = :ids";

apostrophs to ticks

4 Comments

Try, and you are all right, but it still doesn't update
the colon is not required
@user57129 so now we can see that you shod change $qls .= " WHERE 'Id_Msg' = :ids"; to $qls .= " WHERE `Id_Msg` = :ids"; spostrophs to ticks
Boy, what an eye you have, effectively, it was the $qls .= " WHERE 'Id_Msg' = :ids"; when i change it to $qls .= " WHERE Id_Msg` = :ids";` it works like a charm, thanks you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.