html - How to add variables into a mysql table from php -


okay objective have people able select there schedule. code far

<?php  //if form has been submitted process if(isset($_post['submit'])){      $servername = "localhost";     $username = "username";     $password = "password";     $dbname = "jesuitschedule";      try {         $conn = new pdo("mysql:host=$servername;dbname=$dbname", $username, $password);         // set pdo error mode exception         $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception);         $sql = 'insert schedule (saturdaymorning, saturdayafternoon, sundaymorning, sundayafternoon, weekday) values (:saturdaymorning, :saturdayafternoon, :sundaymorning, :sundayafternoon, :weekday)';         // use exec() because no results returned         $conn->exec($sql);         echo "new record created successfully";         }     catch(pdoexception $e)         {         echo $sql . "<br>" . $e->getmessage();         }      $conn = null; }  //define page title $title = 'schedule';  //include header template require('layout/header.php'); ?><!doctype html> <html lang="en"> <head>     <meta charset="utf-8">     <title>schedule</title>     <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet">     <link rel="stylesheet" href="style/main.css"> </head> <body>  <div class="container">      <div class="row">          <div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">             <form role="form" method="post" action="" autocomplete="off">                 <h2>please select schedule</h2>                 <hr>                   <div class="form-group">                     <input type="checkbox" name="saturdamorning" id="satmor" class="form-control input-lg" placeholder="user name" value="yes" tabindex="1">saturday morning <br>                 </div>                 <div class="form-group">                     <input type="checkbox" name="saturdayafternoon" id="sataft" class="form-control input-lg" placeholder="s" value="yes" tabindex="2">saturday afternoon <br>                 </div>                 <div class="form-group">                     <input type="checkbox" name="sundaymorning" id="sunmor" class="form-control input-lg" placeholder="s" value="yes" tabindex="3">sunday afternoon <br>                 </div>                 <div class="form-group">                     <input type="checkbox" name="sundayafternoon" id="sataft" class="form-control input-lg" placeholder="s" value="yes" tabindex="4">sunday morning <br>                 </div>                 <div class="form-group">                     <input type="checkbox" name="weekday" id="email" class="form-control input-lg" placeholder="s" value="yes" tabindex="5">weekday <br>                 </div>                   <div class="row">                     <div class="col-xs-6 col-md-6"><input type="submit" name="submit" value="register" class="btn btn-primary btn-block btn-lg" tabindex="6"></div>                 </div>             </form>         </div>     </div>  </div>  </body> </html> 

when run code works no errors when check table blank rows. code adds new sets of rows doesn't add data them. trying add either yes, or keep blank if not select it. great thanks.

as pointed out fred -ii-, you've not bound statement. here's code using prepared statement. i've commented code explain position

if(isset($_post['submit'])){      $servername = "localhost";     $username = "username";     $password = "password";     $dbname = "jesuitschedule";      try {         $conn = new pdo("mysql:host=$servername;dbname=$dbname", $username, $password);         // set pdo error mode exception         $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception);         // have sql statement, attempting insert non-existant values. you'll either         // wind error, values given in statement inserted table,         // or empty values.         //$sql = 'insert schedule (saturdaymorning, saturdayafternoon, sundaymorning, sundayafternoon, weekday) values (:saturdaymorning, :saturdayafternoon, :sundaymorning, :sundayafternoon, :weekday)';          // create prepared statement, let's bind parameters         $stmt = $con->prepare(             'insert schedule (                 saturdaymorning, saturdayafternoon, sundaymorning, sundayafternoon, weekday                 ) values (                 :saturdaymorning, :saturdayafternoon, :sundaymorning, :sundayafternoon, :weekday                 )';         );          // use exec() because no results returned         //$conn->exec($sql); // you're executing statement no bound parameters          // can use bindparam, find method tad easier         // take stmt created above, , bind values parameters given         // in statement, but, execute. :)         $stmt->execute(array(             ':saturdaymorning' => 'value',             ':saturdayafternoon' => 'value',             ':sundaymorning' => 'value',             ':sundayafternoon' => 'value',             ':weekday' => 'value'         ));         echo "new record created successfully";         }     catch(pdoexception $e)         {         echo $sql . "<br>" . $e->getmessage();         }      $conn = null; } 

if you'd more info on this, take @ pdo page php site, pulled fix: php: pdo - manual


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -