0

I've got this code (not full length of the code):

[Database]

"count_size"

cislonakupu: 123 , idproduktu = 1

cislonakupu: 123 , idproduktu = 2

       <?php
        include("config.php");
    $cislonakupus=$_GET['cislonakupu'];

        $su=mysql_query("SELECT * FROM `count_size` WHERE `cislonakupu`='$cislonakupus'");
        while ($row=mysql_fetch_array($su)) {
            $pid1=$row['idproduktu'];

        $b=mysql_query("INSERT INTO `objednavkyinfo`(cislonakupu,produkt) VALUES ('$cislonakupu','$pid1')") or die(mysql_error());
        }
?>

If I use this one, into database is writtnen each of them (from database "count_size") but twice.

What is wrong?

ssqhj
  • 1
  • 1
  • The only way this'd happen is your records are duplicated to start with, or the code is executed twice. – Marc B Oct 07 '14 at 20:57
  • Does the count increment by 1 each time? So the next time you run the code it does it 3 times? – Fraggy Oct 07 '14 at 20:58
  • @Fraggy it increment 1 time only. In count_size I've got 2 rows. But it writes 4 rows. – ssqhj Oct 07 '14 at 20:59
  • 2
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 07 '14 at 21:00

1 Answers1

-2

By default, you get back an array with both, numeric and associated keys.

Look here: http://php.net/manual/en/function.mysql-fetch-array.php

and try this:

while ($row=mysql_fetch_array($su,MYSQL_ASSOC)) { ... }

boulder_02
  • 301
  • 1
  • 6
  • That's the answer to his question. Do you know his php version? – boulder_02 Oct 07 '14 at 21:18
  • 1
    It doesn't matter since MySQLi became available in 4.1. – Jay Blanchard Oct 07 '14 at 21:20
  • 1
    While you are correct that `mysql_fetch_array returns both numerically and indexed keys, those key are still in a single array, not two different arrays, or two different rows in the result set, so that alone wouldn't multiply the number of inserts based on the code shown. – Mike Brant Oct 07 '14 at 21:21