php - PDO insert validation -
i've got code insert correctly, failing give me success message. returns failed add device database, go check database , in fact successfull. ideas?
<?php // start or resume session session_start(); //check ensure user authorized view page if (isset($_session['loggedin']) && $_session['loggedin'] == true) { // include header include("includes/header.php"); echo " <div class='form_description'> <h2>french lick resort</h2> <p>status - add ingenico device inventory</p> </div> <form id='update' class='fieldset' method='post' action=''>"; $serial=$_post['serial']; $model=$_post['model']; $devicecondition=$_post['devicecondition']; $sealcondition=$_post['sealcondition']; $location=$_post['location']; $deploydate=$_post['deploydate']; $weight=$_post['weight']; $notes=$_post['notes']; //new pdo connection try{ $conn = new pdo("mysql:host=$sql_server;dbname=$sql_db", $sql_user, $sql_pass); $sql = "insert web01dev4s2.ingenicoinfo (serial, model, devicecondition, sealcondition, location, deploydate, weight, notes) values ('".$serial."', '".$model."', '".$devicecondition."', '".$sealcondition."', '".$location."', '".$deploydate."', '".$weight."', '".$notes."')"; $q = $conn->prepare($sql); $result_1=mysql_query($sql); $q->execute(); } catch (pdoeexception $pe) { die("could not connect database" . $pe->getmessage()); } //end pdo connection // display "go" or "no go" if($result_1){ echo "device added database."; header( "refresh:2;url=devicelist.php" ); } else { echo "failed add device database. please ensure device not in database , fields filled out. notes should na if there no notes add. also, ensure name not containt special characters such quotes.<br />"; echo "<a href=create.php>back</a>" ; } } else { header('location:login.php'); } echo " </form> </div> </body> </html>"; ?>
you mixing use of pdo , mysql extension. don't that.
if going use pdo, use prepare statements correctly, well. should not put variables raw sql string, instead use '?' expect value inserted. pass array of variables statement's execute. pdo way, , prevent sql injections against code.
$sql = "insert web01dev4s2.ingenicoinfo (serial, model, devicecondition, sealcondition, location, deploydate, weight, notes) values (?, ?, ?, ?, ?, ?, ?, ?)"; $q = $conn->prepare($sql); // line should fix problem $result_1 = $q->execute(array($serial, $model, $devicecondition, $sealcondition, $location, $deploydate, $weight, $notes));
Comments
Post a Comment