php - How to ensure data inserted only once at a time? -
this how json like:
$return='{"sub":{"2":""},"postcode":"56000","slider1":"100","action":"test"}'; $data = json_decode($return, true);
this how value,
$postcode=$data['postcode']; $budget = $data['slider1']; $subject =$data['sub']; var_dump($key =array_keys($data['sub']));// prints 0=>2 foreach($key $key1=>$key2) { $key1;//prints 0 $key2;//prints 2 } $key2;//prints 2
i'm inserting data this:
try { //$pdo = new pdo('mysql:host=localhost;dbname=users', $username, $password); //$pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception); include($_server['document_root'].'/config.php'); $stmt = $pdo->prepare('insert test (postcode,budget,subject) values(:postcode,:budget,:subject)'); $stmt->execute(array( ':postcode' => $postcode, ':budget'=> $budget, ':subject'=> $key2 )); # affected rows? $stmt->rowcount(); // 1 } catch(pdoexception $e) { 'error: ' . $e->getmessage(); }
the above query inserts data twice database. checked network tab in chrome dev tool, shows request file sent twice.
is there way insert data once @ time? mean there way place restriction on query?
edited
ajax
$("document").ready(function(){ $(".form").submit(function(){ var data = { "action": "test" }; data = $(this).serialize(); $.ajax({ type: "post", datatype: "json", url: "writedb.php", //relative or absolute path writedb.php file data: data, success: function(data) { //..... }
Comments
Post a Comment