[dx] Posted December 10, 2014 Share Posted December 10, 2014 Hi, I'm managing some script so I have problem inserting new data to dbase. Here's my connection: $config = array('host' => 'localhost','username' => 'xxxx','password' => 'xxxx','dbname' => 'xxxx','charset' => 'latin2');$db = new PDO('mysql:host=' . $config['host'] . ';dbname=' . $config['dbname'] .';charset=' . $config['charset'], $config['username'], $config['password']);$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); And this is piece of code which worked for me and I didn't changed anything it just stopped to work: for ($i = 0; $i < count($_POST['domacin']); $i++) {if (strlen($_POST['domacin'][$i]) > 0 and strlen($_POST['gost'][$i]) > 0):if ($_POST['domacin'][$i] != $_POST['gost'][$i]):$query = $db->prepare("INSERT INTO utakmice (domacin, gost, broj_kola) SELECT * FROM (SELECT ?, ?, ?) AS tmp WHERE NOT EXISTS (SELECT domacin, gost, broj_kola FROM utakmice WHERE domacin = ? AND gost = ? AND broj_kola = ?) LIMIT 1");$query->bindValue(1, $_POST['domacin'][$i]);$query->bindValue(2, $_POST['gost'][$i]);$query->bindValue(3, $_POST['kolo_broj']);$query->bindValue(4, $_POST['domacin'][$i]);$query->bindValue(5, $_POST['gost'][$i]);$query->bindValue(6, $_POST['kolo_broj']);$query->execute();endif;endif;} As you can see I'm getting $_POST['domacin'] and $_POST['gost'] with same number of items so it loops and store unique data. Can it be that dbase does not allow spam like? I'm really sure that it works becouse I used it before. This for adding sport matches, I added few rounds and it just stopped. Can you provide me a clue where I can search? Best regards. Link to comment Share on other sites More sharing options...
Ingolme Posted December 10, 2014 Share Posted December 10, 2014 What does "not working" mean? Are you getting any error messages? Maybe there is an error in the query. Use errorInfo() to find out. You should prepare the statement outside of the loop, it's much more efficient. Statements only need to be prepared once, they can be executed as many times as needed. Link to comment Share on other sites More sharing options...
[dx] Posted December 13, 2014 Author Share Posted December 13, 2014 It won't output anything if I use print_r($db->errorInfo()); just after $query->execute(); :S Link to comment Share on other sites More sharing options...
Ingolme Posted December 13, 2014 Share Posted December 13, 2014 You would need to call $query->errorInfo(), not $db->errorInfo(). But what exactly is the program doing and what did you expect it to do? Link to comment Share on other sites More sharing options...
[dx] Posted December 14, 2014 Author Share Posted December 14, 2014 Nothing outputed with $query->errorInfo() It should insert data in db, but it won't execute. Link to comment Share on other sites More sharing options...
Ingolme Posted December 14, 2014 Share Posted December 14, 2014 $query->errorInfo() has to output something. At the very least it should output an error code zero, meaning that everything is working fine. Link to comment Share on other sites More sharing options...
[dx] Posted December 17, 2014 Author Share Posted December 17, 2014 It outputs before calling execute(), but later nothing. Link to comment Share on other sites More sharing options...
Ingolme Posted December 17, 2014 Share Posted December 17, 2014 That sounds like the program is halting at the line where the query was executed. Make sure error messages are activated by putting this at the beginning of the script: ini_set('display_errors', 1);error_reporting(E_ALL); Could you show the most recent code you have? Link to comment Share on other sites More sharing options...
[dx] Posted December 25, 2014 Author Share Posted December 25, 2014 Hi, For someone with same problem. In my case error reporting don't work but I found error. Problem was in query: $query = $db->prepare("INSERT INTO utakmice (domacin, gost, broj_kola) SELECT * FROM (SELECT ?, ?, ?) AS tmp WHERE NOT EXISTS (SELECT domacin, gost, broj_kola FROM utakmice WHERE domacin = ? AND gost = ? AND broj_kola = ?) LIMIT 1"); It seems that it can proceed with duplicated columns names. Like (SELECT 20, 20, 10). If params have same value it stops. So I found other way. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now