Advertisement
Skorpius

PDO: Insert records into DB

Jun 11th, 2022
1,108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.69 KB | None | 0 0
  1. <?php
  2.     //Connection script in other file
  3.     require_once("connect.php);
  4.    
  5.     //Data you would collect from form via POST
  6.     $name = 'elvis';
  7.     $quote = 'Who gives a rats ass';    
  8.  
  9.     $sql = "(INSERT INTO tableName(name,fav_quotes) VALUES(:name,:quote)");
  10.    $stmt = $pdo-prepare($sql);
  11.     $stmt->bindParam('name',$name);
  12.     $stmt->bindParam('quote',$quote);
  13.    if($stmt-execute()){
  14.         echo "Success";
  15.     }else{
  16.         echo "Failed";
  17.     }
  18.  
  19. /*
  20.    while($row = $stmt-fetch())
  21. {
  22.    //DB Fieldnames in square brackets within the single quotes
  23.    $name = $row['name'];
  24.    $quote = $row['fav_quote'];
  25.  
  26.    //Variables here must match above
  27.    echo "<b><i>$quote</i></b><br><p>$name</p>";
  28. }
  29. */
  30.  
  31. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement