form for inserting using php and pdo -


i losing mind on , use direction. trying learn pdo along php , failing understand logic. keep trying find online shows example of flow test attempt , i'm having real hard time.

could someone, if have flame heck out of me (although anyone, i'd prefer not), give me direction on i'm doing horribly wrong? i'm building start understanding. there's plenty of info on using mysqli not pdo , it's driving me nuts.

thanks in advance. here's code:

<?php  # connection info db $host = "--shadowed--"; $dbname = "--shadowed--"; $user = "--shadowed--"; $pass = "--shadowed--";  # pdo options/attributes $opt = array( pdo::attr_errmode => pdo::errmode_exception ); // not getting errors # data source name $dsn = "mysql:host=$host;dbname=$dbname";   # basic pdo connection (with added option error handling) if (isset($_post['submit'])) {     try {         $dbh = new pdo($dsn, $user, $pass, $opt);         $sth = $dbh->prepare("insert data (name,email,phone,detail,cost) values (:name,:email,:phone,:detail,:cost)");          $sth->bindparam(':name', $name);         $sth->bindparam(':email', $email);         $sth->bindparam(':phone', $phone);         $sth->bindparam(':detail', $detail);         $sth->bindparam(':cost', $cost);           $name = $_post['name'];         $email = $_post['email'];         $phone = $_post['phone'];         $detail = $_post['detail'];         $cost = $_post['cost'];           $sth->execute();         echo $sth; // attempted echo data, nothing happens       } catch (pdoexception $e) {         echo $e->getmessage(); // no errors     } }  echo '<form method="post" action="">'; echo '<p>enter below information if want live:</p>'; echo 'name: <input type="text" name="name"><br />'; echo 'e-mail: <input type="text" name="email"><br />'; echo 'phone: <input type="text" name="phone"><br />'; echo 'order generated randomly class (once built)<br />'; echo 'description: <input type="text" name="detail"><br />'; echo 'cost: <input type="text" name="cost"><br />';  echo '<input type="submit" value="do-it"></form>';  # close connection   $dbh = null; ?> 

------------- final code after resolution reached -------------

------------- final code after resolution reached -------------

(still newb can't answer own question currently)

so first off, didn't come this... it's mix of here really. appreciate everyone's , time while try learn missing links knowledge.

the main issues seems when used original attempt utilize if (isset($_post['submit'])), didn't or send anything. no errors... no database issues... bunch of nothing. removed find holding ( ty @fred ). although didn't change how code works, became more efficient using @hjpotter92 suggestion. looked how submit using single page. ended using mix of @fred's , @david strachan suggestions neither giving me right reaction, added if/else statement perform check , if passed, run try/catch.

it's no work of art, learned quite bit , appreciate help. also, think nice out there people can bump see full example. if of guys have additional suggestions, please let me know. along learning base knowledge, i'm reviewing how against sql injection (which may not covered in test).

    #------------------ working code ------------------#      # pdo options/attributes     $opt = array( pdo::attr_errmode => pdo::errmode_exception );     # data source name     $dsn = "mysql:host=$host;dbname=$dbname";       # basic pdo connection (with added option error handling)     if ($_server['request_method'] == "post") {          if (!$_post['name'] || !$_post['email'] || !$_post['phone'] || !$_post['detail'] || !$_post['cost']) {             echo "<p>please supply of data! may press button attempt again minion!</p>";             exit;         } else {              try {                         $dbh = new pdo($dsn, $user, $pass, $opt);                 $sth = $dbh->prepare("insert data (name,email,phone,detail,cost) values (:name,:email,:phone,:detail,:cost)");                  $sth->bindparam(':name', $_post['name']);                 $sth->bindparam(':email', $_post['email']);                 $sth->bindparam(':phone', $_post['phone']);                 $sth->bindparam(':detail', $_post['detail']);                 $sth->bindparam(':cost', $_post['cost']);                  $sth->execute();              } catch (pdoexception $e) {                 echo $e->getmessage();             }             echo "<p>data submitted successfully</p>";          }     }      echo '<form method="post" action="">';     echo '<p>enter below information if want live:</p>';     echo 'name: <input type="text" name="name"><br />';     echo 'e-mail: <input type="text" name="email"><br />';     echo 'phone: <input type="text" name="phone"><br />';     echo 'order generated randomly<br />';     echo 'description: <input type="text" name="detail"><br />';     echo 'cost: <input type="text" name="cost"><br />';      echo '<input type="submit" value="do-it"></form>';        # close connection       $dbh = null;     ?> 

to check if request post type use $_server['request_method'] documentation

// post variables $name = isset($_post['name']) ? $_post['name'] : '';  $name = isset($_post['email']) ? $_post['email'] : ''; $name = isset($_post['phone']) ? $_post['phone'] : ''; $name = isset($_post['detail']) ? $_post['detail'] : ''; $name = isset($_post['cost']) ? $_post['cost'] : ''; if($_server['request_method'] == "post") {           try{    remainder of code 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -