-3

我是 php 和 mysql 的新手,我想创建一个查询来执行更新或添加一个新表(如果它不在数据库中)

到目前为止,我有这个,为什么我会收到 Parse error: syntax error, unexpected T_STRING for

mysql_query(UPDATE ps_product SET id_tax_rules_group = 1 WHERE id_tax_rules_group = 0);

你能帮我让这段代码工作吗?当我只评论所有 mysql_quer 时,我得到空白页并回显“ID $trenutnired 在表中不存在并且:$countUpdated
”;

不管用?

我使用 PHP 5.2.17

谢谢

    <?php


     $mysql_db = "";
    $mysql_user = "";
    $mysql_pwd  = "";


    $con = mysql_connect("localhost", $mysql_user, $mysql_pwd);
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db($mysql_db, $con);

      $countUpdated=0;

        mysql_query(UPDATE ps_product_shop prod_shop INNER JOIN ps_product prod USING(id_product) SET prod_shop.id_category_default = prod.id_category_default WHERE prod_shop.id_product BETWEEN 1 AND 62226);

                mysql_query(UPDATE ps_product_supplier SET id_currency = 3 WHERE id_currency = 0);                      
                mysql_query(UPDATE ps_product SET id_tax_rules_group = 1 WHERE id_tax_rules_group = 0);
                mysql_query(UPDATE ps_product_shop SET id_tax_rules_group = 1 WHERE id_tax_rules_group = 0);


    $kveri = "SELECT id_product,id_supplier,supplier_reference, wholesale_price FROM ps_product";

    $ispis = mysql_query($kveri) or die(mysql_error());

               while ($row = mysql_fetch_array($ispis)){


               $trenutnired = $row['id_product'];
               $trenutnired1 = 0;
               $trenutnired2 = $row['id_supplier'];
               $trenutnired4 = $row['supplier_reference'];
               $trenutnired5 = $row['wholesale_price'];
               $trenutnired6 = 3;


               $drugatab = mysql_query("SELECT * FROM ps_product_supplier WHERE id_product = '$trenutnired'");

               $dalipostoji = mysql_num_rows($drugatab);

               if ($dalipostoji == 0) {

               echo "ID $trenutnired does not exist in table and:: $countUpdated<br />";   

                 mysql_query("INSERT INTO ps_product_supplier (id_product_supplier, id_product, id_product_attribute, id_supplier, product_supplier_reference, product_supplier_price_te, id_currency)  VALUES ('', '$trenutnired', '$trenutnired1',  '$trenutnired2', '$trenutnired4', '$trenutnired5', '$trenutnired6')");

                                   $countUpdated++;

               };
             }               
    ?>
4

3 回答 3

2

看来你忘了放双"mysql_query(

你有没有试过这个:

mysql_query("UPDATE ps_product SET id_tax_rules_group = 1 WHERE id_tax_rules_group = 0 ");

代替

mysql_query(UPDATE ps_product SET id_tax_rules_group = 1 WHERE id_tax_rules_group = 0);

检查下面的整个代码(修改)


<?php


     $mysql_db = "";
    $mysql_user = "";
    $mysql_pwd  = "";


    $con = mysql_connect("localhost", $mysql_user, $mysql_pwd);
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db($mysql_db, $con);

      $countUpdated=0;

      //popravak kategorija
        mysql_query("UPDATE ps_product_shop prod_shop INNER JOIN ps_product prod USING(id_product) SET prod_shop.id_category_default = prod.id_category_default WHERE prod_shop.id_product BETWEEN 1 AND 62226 ");


                // sve na kunu
                mysql_query("UPDATE ps_product_supplier SET id_currency = 3 WHERE id_currency = 0");


                //dodavanje poreza

                mysql_query("UPDATE ps_product SET id_tax_rules_group = 1 WHERE id_tax_rules_group = 0");
                mysql_query("UPDATE ps_product_shop SET id_tax_rules_group = 1 WHERE id_tax_rules_group = 0");



    //$kveri = "SELECT id_product_supplier, id_product, id_product_attribute, id_supplier,product_supplier_reference, product_supplier_price_te, id_currency FROM ps_product";
    $kveri = "SELECT id_product,id_supplier,supplier_reference, wholesale_price FROM ps_product";

    $ispis = mysql_query($kveri) or die(mysql_error());

               while ($row = mysql_fetch_array($ispis)){


               $trenutnired = $row['id_product'];
               $trenutnired1 = 0;
               $trenutnired2 = $row['id_supplier'];
               $trenutnired4 = $row['supplier_reference'];
               $trenutnired5 = $row['wholesale_price'];
               $trenutnired6 = 3;



                                                            //echo $trenutnired;

               $drugatab = mysql_query("SELECT * FROM ps_product_supplier WHERE id_product = '$trenutnired'");

               $dalipostoji = mysql_num_rows($drugatab);




               if ($dalipostoji == 0) {

               echo "ID $trenutnired ne postoji u drugoj tablici i trenutno: $countUpdated<br />";   

                 mysql_query("INSERT INTO ps_product_supplier (id_product_supplier, id_product, id_product_attribute, id_supplier, product_supplier_reference, product_supplier_price_te, id_currency)  VALUES ('', '$trenutnired', '$trenutnired1',  '$trenutnired2', '$trenutnired4', '$trenutnired5', '$trenutnired6')");

                                   $countUpdated++;



               };


             }

    ?>
于 2012-12-26T00:01:55.007 回答
2

That error indicates a problem in your PHP. Specifically, the mysql_query() function takes a string as its first argument. So you'll need to wrap your SQL statement in quotes:

mysql_query("UPDATE ps_product SET id_tax_rules_group = 1 WHERE id_tax_rules_group = 0");
于 2012-12-26T00:01:58.033 回答
0

You should always check if your query was successful before continuing script execution because on production servers error reporting is usually disabled (as it should be) so you won't be able to see errors in your code like in this case where you actually tried to fetch the number of records for an unsuccessful query.

I.e. you should have checked if your SELECT query actually returns a result like this.

$drugatab = "SELECT * FROM ps_product_supplier WHERE id_product = '$trenutnired'";
if($rezultati = mysql_query($drugatab) && mysql_num_rows($rezultati)){
    // Successful query...
    mysql_query("INSERT INTO ps_product_supplier (id_product_supplier, id_product, id_product_attribute, id_supplier, product_supplier_reference, product_supplier_price_te, id_currency)  VALUES ('', '$trenutnired', '$trenutnired1',  '$trenutnired2', '$trenutnired4', '$trenutnired5', '$trenutnired6')");
    $countUpdated++;
} else {
    echo "ID $trenutnired ne postoji u drugoj tablici i trenutno: $countUpdated<br />";
}
于 2012-12-26T00:10:55.453 回答