Web Analytics Made Easy -
StatCounter PDO insert wont run - CodingForum

Announcement

Collapse
No announcement yet.

PDO insert wont run

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PDO insert wont run

    Hi,

    I cannot get my insert query to work. I have a update query right above this in the file and it works great but i have two inserts one after another and neigher of them work and i cant figure out why... it looks proper to me...

    its not a connection issue... and i get no errors at all... I also checked the table to be sure the fields were set to be NULL or accept 0 as well. I also checked to be sure the user has insert permissions, yep

    Can you see anything that i dont see.

    I thought maybe it was dealing with a race condition since i had just creatated the db before this code.. But that would give an error. I did try sleep(10) just in case to give the server time to create the tables first.

    How are we suppose to see whats going on if we cant ouput the loaded query or get an error

    PHP Code:

    $sql 
    "INSERT INTO `admin_data` (`identify_id`, `name`, `email`, `password`, `level`, `email_verify`,
    `registered_ip`, `registered_stamp`, `activity_stamp`) VALUES(?,?,?,?,?,?,?,?,?)"
    ;

    $stmt $testcon->prepare($sql);
    $runsql $stmt->execute([
    '1' => $one,
    '2' => $admin_name,
    '3' => $admin_mail,
    '4' => $pwh,
    '5' => $three,
    '6' => $two,
    '7' => $cip,
    '8' => $thetime,
    '9' => $thetime
    ]); 
    Last edited by durangod; Sep 4, 2020, 06:56 PM.
    If a php file only has php code within it you do not need to use the closing php tag
    A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
    durangod is short for durango dave

  • #2
    Is exec or execute the correct command?

    Have you declared strict_types=1?

    Have you enabled or disabled error reporting and in_set display errors?

    Try adding the database validation script from this site:

    https://phpdelusions.net/

    Comment


    • #3
      All simple stuff has been checked and checked again, i also copied the insert directly from a file that works on another project of mine and just changed the data, so i know the code is right. Its been awhile since i did the binding method, im going to try to bind them to see if that works. Maybe i have to do inserts a different way one this project because of the way the file structure is, i dont know. But i will get it.. i always do..

      There is another way to skin this chicken if the bind does not work. I will create a generic record in the sql upload process and then just update it since i know the update works.
      Last edited by durangod; Sep 5, 2020, 01:25 AM.
      If a php file only has php code within it you do not need to use the closing php tag
      A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
      durangod is short for durango dave

      Comment


      • #4
        Try adapting this connection script and make sure the error $options ; are set because maybe the problem is not being able to insert into a unique column that is already allocated and duplicate data is not being reported.
        Code:
        $host = '127.0.0.1'; $db   = 'test';
        $user = 'root';
        $pass = '';
        $charset = 'utf8mb4';
        
        dsn = "mysql:host=$host;
        dbname=$db;
        charset=$charset";
        $options = [
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,     
          PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,     
          PDO::ATTR_EMULATE_PREPARES   => false,
        ];
        try {  $pdo = new PDO($dsn, $user, $pass, $options);
        } catch (\PDOException $e) {
            throw new \PDOException($e->getMessage(), (int)$e->getCode());
        }

        Comment


        • #5
          yep that was it, the bind version works and the original one using ??? does not...

          here is the bind version not sure why this works and the other one does not..

          PHP Code:

          $sql 
          '';
          $one 1;
          $two 2;
          $three 3;
          $thetime time();


          $sql "INSERT INTO `admin_data` (`identify_id`, `name`, `email`, `password`, `level`, `email_verify`,
          `registered_ip`, `registered_stamp`, `activity_stamp`) VALUES(:a,:b,:c,:d,:e,:f,:g,:h,:i)"
          ;


          $stmt $testcon->prepare($sql);

          $stmt->bindParam(':a'$onePDO::PARAM_INT);
          $stmt->bindParam(':b'$admin_name);
          $stmt->bindParam(':c'$admin_mail);
          $stmt->bindParam(':d'$pwh);
          $stmt->bindParam(':e'$threePDO::PARAM_INT);
          $stmt->bindParam(':f'$twoPDO::PARAM_INT);
          $stmt->bindParam(':g'$cip);
          $stmt->bindParam(':h'$thetimePDO::PARAM_INT);
          $stmt->bindParam(':i'$thetimePDO::PARAM_INT);

          $resultq $stmt->execute(); 

          If a php file only has php code within it you do not need to use the closing php tag
          A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
          durangod is short for durango dave

          Comment


          • #6
            also, this is the connection that i use, almost the same

            PHP Code:

            class Dbpdo {


            private 
            $servername;
            private 
            $username;
            private 
            $password;
            private 
            $dbname;
            private 
            $charset;
            private 
            $dsn;
            private 
            $pdo;


            public function 
            connect()
            {

            $this->servername INSTALL_SERVER;
            $this->username INSTALL_USER;
            $this->password INSTALL_PASS;
            $this->dbname INSTALL_DBNAME;
            $this->charset INSTALL_CHARSET;


            try {

            //pdo connection
            $dsn "mysql:host=".$this->servername.";dbname=".$this->dbname.";charset=".$this->charset;
            $pdo = new PDO($dsn,$this->username$this->password);
            $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
            $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODEPDO::FETCH_ASSOC);
            $pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
            return 
            $pdo;

            } catch(
            PDOException $e)
            {

            // log the error so it may be looked at later if necessary
            error_log($e->getMessage());

            echo 
            '<div class="generaltxt">
            <h3>Sorry but we ran into a problem connecting to our database.</h3>
            The problem has been logged. Please check your log file.
            Please try again later.
            </div>'
            ;

            exit;

            }
            //close catch

            }//close function connect 
            If a php file only has php code within it you do not need to use the closing php tag
            A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
            durangod is short for durango dave

            Comment


            • #7
              I think i found the problem all along. You cant use a general value variable more than once in a execution. So for example i had $null = 'NULL' and that will work the first time but the second time it sees that variable, it will just stop and move on, no warning, no error, it just says 'so long buddy-you figure it out" LMAO, and that was the issue.... i was using general value variables more than once in a execution.

              Now why didnt i just put 1 or NULL, i dont know im unique and goofy i guess. LOL

              The reason i did nto put it right in the bind is because now i get a cant pass data by reference so you cant just put 1 or 3 ot NULL
              Last edited by durangod; Sep 5, 2020, 05:41 AM.
              If a php file only has php code within it you do not need to use the closing php tag
              A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
              durangod is short for durango dave

              Comment


              • #8
                JOhn_Betong WOW John, whoever spent the time to type up the content for that website took alot of time to do it.... That came out nice...
                If a php file only has php code within it you do not need to use the closing php tag
                A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
                durangod is short for durango dave

                Comment


                • #9
                  Originally posted by durangod View Post
                  JOhn_Betong WOW John, whoever spent the time to type up the content for that website took alot of time to do it.... That came out nice...
                  Do you mean https://phpdelusions.net/... if not which site do you mean?

                  Comment


                  • #10
                    Yes i mean that site phpdelusions, tons of work went in into that... impressive
                    If a php file only has php code within it you do not need to use the closing php tag
                    A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
                    durangod is short for durango dave

                    Comment


                    • #11
                      Blind placeholders are 1) numerically indexed, not string, and 2) start at ZERO, not one.

                      Code:
                      $runsql = $stmt->execute([
                        $one,
                        $admin_name,
                        $admin_mail,
                        $pwh,
                        $three,
                        $two,
                        $cip,
                        $thetime,
                        $thetime
                      ]);
                      On your original code would have worked just fine, assuming it's PDO. You manually indexed for nothing. If you're using mysqli, it has no array pass equivalent on ->execute()
                      Bleed your death upon me, let your bloodline feed my youth.
                      https://cutcodedown.com
                      https://medium.com/@deathshadow

                      Comment


                      • #12
                        thanks i dont like using bind anyway, ill get the original one going...
                        If a php file only has php code within it you do not need to use the closing php tag
                        A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
                        durangod is short for durango dave

                        Comment


                        • #13
                          So i finally got around to changing this back from the bindparam method to the original method and making sure no vars was duplicated and it still wont insert anything and no error... what am i missing...

                          This format is an exact copy of the format i use in another domain on the same server, but that one works and this one will not... Anything you can see that is missing or incorrect?

                          Updates work just fine, INSERTS nothing happens...

                          Im using PDO

                          // i placed the data into vars because we cant put the raw data directly into the runsql array.

                          thanks

                          PHP Code:
                          //INSERT admin data

                          $testdb = new Dbpdoinstall;

                          $testcon $testdb->makeConnection();

                          $sql '';
                          $one 1;
                          $two 2;
                          $three 3;
                          $time time();
                          $newtime time();

                          $sql "INSERT INTO `admin_data` (`admin_identify_id`, `admin_name`, `admin_email`, `admin_password`, `admin_level`, `admin_email_verify`,
                          `admin_registered_ip`, `admin_registered_stamp`, `admin_activity_stamp`) VALUES(?,?,?,?,?,?,?,?,?)"
                          ;

                          $stmt $testcon->prepare($sql);

                          $runsql $stmt->execute([
                                                  
                          '1' => $one,
                                                   
                          '2' => $admin_name,
                                                   
                          '3' => $admin_mail,
                                                   
                          '4' => $pwh,
                                                    
                          '5' => $three,
                                                    
                          '6' => $two,
                                                    
                          '7' => $cip,
                                                     
                          '8' => $time,
                                                     
                          '9' => $newtime
                                                     
                          ]); 

                          I have even redone my db connection class

                          PHP Code:

                          class Dbpdoinstall {


                          private 
                          $servername;
                          private 
                          $username;
                          private 
                          $password;
                          private 
                          $dbname;
                          private 
                          $charset;
                          private 
                          $dsn;
                          private 
                          $pdo;


                          public function 
                          connect()
                          {

                          $this->servername cleanInput($_SESSION['thehost']);
                          $this->username cleanInput($_SESSION['thedbuser']);
                          $this->password cleanInput($_SESSION['thepw']);
                          $this->dbname cleanInput($_SESSION['thedbname']);
                          $this->charset cleanInput($_SESSION['charset']);


                          try {

                          //pdo connection
                          $dsn "mysql:host=".$this->servername.";dbname=".$this->dbname.";charset=".$this->charset;
                          $pdo = new PDO($dsn,$this->username$this->password);
                          $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
                          $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODEPDO::FETCH_ASSOC);
                          $pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
                          return 
                          $pdo;

                          } catch(
                          PDOException $e)
                          {

                          // log the error so it may be looked at later if necessary
                          error_log($e->getMessage());

                          echo 
                          '<div class="generaltxt">
                          <h3>Sorry but we ran into a problem connecting to your database.</h3>
                          The problem has been logged. Please check your log file.
                          You can use the browser back arrow to reload the previous page
                          and correct your input. </div>'
                          ;

                          exit;

                          }
                          //close catch

                          }//close function connect


                          public function makeConnection()
                          {

                          //init arrays
                          $newpdo = [];
                          $newcon = [];

                          //run connection
                          $newpdo = new Dbpdoinstall;
                          $newcon $newpdo->connect();

                          return 
                          $newcon;

                          }
                          //close function makeConnection

                          }//close class 
                          Still nothing
                          Last edited by durangod; Sep 22, 2020, 11:48 PM.
                          If a php file only has php code within it you do not need to use the closing php tag
                          A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
                          durangod is short for durango dave

                          Comment


                          • #14
                            You can start with getting rid of that numbering in the array and the unused variable.. You should only have the variables there.

                            PHP Code:
                            $stmt->execute([
                            $one,
                            $admin_name,
                            $admin_mail,
                            $pwh,
                            $three,
                            $two,
                            $cip,
                            $time,
                            $newtime
                            ]); 
                            To save time, lets just assume I am almost never wrong.

                            The XY Problem
                            The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

                            Make A Donation https://www.paypal.me/KevinRubio

                            Comment


                            • #15
                              Originally posted by benanamen View Post
                              You can start with getting rid of that numbering in the array and the unused variable.. You should only have the variables there.
                              Which is pretty much exactly what I posted...

                              Likewise all those "clean" function calls can sod right off, given he's using prepare/execute.

                              Your PDO object seems stupid since

                              1) why the blazes would you put the DB connection into the SESSION where it's global scope could bleed on elevation? That's as bad as putting them into DEFINE. Though to be fair, with PHP's "security, what's that?!?" attitude, there's no actual good way of doing it.

                              2) What's all that "cleaninput" trash?

                              3) what's this "connect" method for when you could / should have that on the constructor?

                              4) those connection attributes could be passed as an array to the new PDO, instead of messing around with three separate calls to setAttr.

                              5) It might be better if you just extended PDO instead of wrapping it.



                              Bleed your death upon me, let your bloodline feed my youth.
                              https://cutcodedown.com
                              https://medium.com/@deathshadow

                              Comment

                              Working...
                              X