Jump to content

Problem using PDO


[dx]

Recommended Posts

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...