08. 02. 2014. | #1 |
profesionalac
Professional
Datum učlanjenja: 25.10.2012
Poruke: 278
Hvala: 16
32 "Hvala" u 9 poruka
|
Php pdo insert
Zao mi je sto moram da non-stop nesto pitam ali ovo pokusavam bukvalno eo dan da resim, dobijao sam milion gresaka svakakvog tipa...
Znaci imam formu i pokusavam AJAX-om da dodam u mysql koristeci php PDO funcije: JS: Kôd:
$("#newData").click(function() { //in here we can do the ajax after validating the field isn't empty. if($("#name").val()!="") { $.ajax({ url: "insert.php", type: "POST", async: true, data: { name:$("#name").val(), gender:$("#gender").val(), age:$("#age").val(), Donuts_eaten:$("#donuts_eaten").val()}, dataType: "html", success: function(data) { $('#output').html(data); $('#myModal').hide(); drawVisualization(); }, }); } else { $("#newData").html('Greska'); } }); Kôd:
<?php $host = "localhost"; $dbname = "gmaestro_agro"; $user = "gmaestro_agro"; $pass = "pass"; $opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ); $dsn = "mysql:host=$host;dbname=$dbname"; if ($_SERVER['REQUEST_METHOD'] == "POST") { if (!$_POST['name'] || !$_POST['gender']) { echo "<p>Popunite sva polja</p>"; exit; } else { try { $DBH = new PDO($dsn, $user, $pass, $opt); $STH = $DBH->prepare("INSERT INTO stat (Name,Gender,Age,Donuts_eaten) VALUES (:name,:gender,:age,:donuts_eaten)"); $STH->bindParam(':name', $_POST['name']); $STH->bindParam(':gender', $_POST['gender']); $STH->bindParam(':age', $_POST['age']); $STH->bindParam(':donuts_eaten', $_POST['Donuts_eaten']); $STH->execute(); } catch (PDOException $e) { echo $e->getMessage(); } echo "<p>Uspesno dodavanje</p>"; } $DBH = null; ?> sadrzati vrednost NULL iako ne prosledjujem null vrednost ... UPDATE (RADI): Izvinjavam se jer su problem bila mala/velika slova za Donuts_eaten Poslednja izmena od mslavko : 08. 02. 2014. u 22:49. |
|
|