PHP MySql更新多行

时间:2012-04-22 15:27:19

标签: php mysql

无法使此代码正常工作。该语句直接在表上正常工作,而不是从PHP文件执行时:

    <strong>Multi Update</strong><br>

    <?php
    $host="localhost"; // Host name
    $username="user_name"; // Mysql username
    $password="Password"; // Mysql password
    $db_name="dbname"; // Database name
    $tbl_name="tablename"; // Table name

    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    $sql="SELECT 
      mik1vm_product.product_id,
      mik1vm_product.product_sku,
      mik1vm_product.product_name,
      mik1vm_product.product_in_stock,
      mik1vm_product.product_in_stock_cardiff
    FROM $tbl_name";
    $result=mysql_query($sql);

    // Count table rows
    $count=mysql_num_rows($result);
    ?>
    <table width="500" border="0" cellspacing="1" cellpadding="0">
    <form name="form1" method="post" action="">
    <tr>
    <td>
    <table width="500" border="0" cellspacing="1" cellpadding="0">


    <tr>
    <td align="center"><strong>Product Id</strong></td>
    <td align="center"><strong>SKU</strong></td>
    <td align="center"><strong>Name</strong></td>
    <td align="center"><strong>In Stock</strong></td>
    <td align="center"><strong>In Stock Cardiff</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="center"><? $product_id[]=$rows['product_id']; ?><? echo $rows['product_id']; ?></td>
    <td align="center"><input name="product_sku[]" type="text" id="product_sku" value="<? echo $rows['product_sku']; ?>"></td>
    <td align="center"><input name="product_name[]" type="text" id="product_name" value="<? echo $rows['product_name']; ?>"></td>
    <td align="center"><input name="product_in_stock[]" type="text" id="product_in_stock" value="<? echo $rows['product_in_stock']; ?>"></td>
    <td align="center"><input name="product_in_stock_cardiff[]" type="text" id="product_in_stock_cardiff" value="<? echo $rows['product_in_stock_cardiff']; ?>"></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
    </tr>
    </table>
    </td>
    </tr>
    </form>
    </table>
    <?php
    // Check if button name "Submit" is active, do this
    if($Submit){
    for($i=0;$i<$count;$i++){
    $sql1="UPDATE $tbl_name SET product_sku='$product_sku[$i]', product_name='$product_name[$i]', product_in_stock='$product_in_stock[$i]' , product_in_stock_cardiff='$product_in_stock_cardiff[$i]' WHERE product_id='$product_id[$i]'";
    $result1=mysql_query($sql1);
    }
    }

    if($result1){
    header("location:test.php");
    }
    mysql_close();
    ?>

页面加载正常,所有数据都正确加载。但只是不更新​​数据库。

提前感谢您的帮助。

更新

好的所以我想我在这里有点偏僻,显然我是PHP的新手所以请耐心等待。

我将代码修改为:

    <strong>Multi Update</strong><br>

    <?php
    $host=""; // Host name
    $username=""; // Mysql username
    $password=""; // Mysql password
    $db_name=""; // Database name
    $tbl_name=""; // Table name


    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    $sql="SELECT 
      mik1vm_product.product_id,
      mik1vm_product.product_sku,
      mik1vm_product.product_name,
      mik1vm_product.product_in_stock,
      mik1vm_product.product_in_stock_cardiff
    FROM $tbl_name";
    $result=mysql_query($sql);

    // Count table rows
    $count=mysql_num_rows($result);
    ?>
    <table width="500" border="0" cellspacing="1" cellpadding="0">
    <form name="form1" method="post" action="">
    <tr>
    <td>
    <table width="500" border="0" cellspacing="1" cellpadding="0">


    <tr>
    <td align="center"><strong>Product Id</strong></td>
    <td align="center"><strong>SKU</strong></td>
    <td align="center"><strong>Name</strong></td>
    <td align="center"><strong>In Stock</strong></td>
    <td align="center"><strong>In Stock Cardiff</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="center"><? $product_id[]=$rows['product_id']; ?><? echo $rows['product_id']; ?></td>
    <td align="center"><input name="product_sku[]" type="text" id="product_sku" value="<? echo $rows['product_sku']; ?>"></td>
    <td align="center"><input name="product_name[]" type="text" id="product_name" value="<? echo $rows['product_name']; ?>"></td>
    <td align="center"><input name="product_in_stock[]" type="text" id="product_in_stock" value="<? echo $rows['product_in_stock']; ?>"></td>
    <td align="center"><input name="product_in_stock_cardiff[]" type="text" id="product_in_stock_cardiff" value="<? echo $rows['product_in_stock_cardiff']; ?>"></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
    </tr>
    </table>
    </td>
    </tr>
    </form>
    </table>
    <?php
    // Check if button name "Submit" is active, do this

    if(isset($_POST['Submit']) && $_POST['Submit'] == 'Submit'){
        for($i=0;$i<$count;$i++){
            $sql1="UPDATE $tbl_name SET product_sku='$product_sku[$i]', product_name='$product_name[$i]', product_in_stock='$product_in_stock[$i]' , product_in_stock_cardiff='$product_in_stock_cardiff[$i]' WHERE product_id='$product_id[$i]'";
            $result1=mysql_query($sql1);
        }
    }


    if($result1){
    header("location:test.php");
    }
    mysql_close();
    ?>

现在出现错误:

在初始页面加载时:e:未定义的变量:第74行的test.php中的sql1

但是当提交页面时会有负载:

注意:未定义的变量:第70行的test.php中的product_sku

注意:未定义的变量:第70行的test.php中的product_name

注意:未定义的变量:第70行的test.php中的product_in_stock

注意:未定义的变量:第70行的test.php中的product_in_stock_cardiff

列表继续......

最后一行:

注意:未定义的变量:第70行的/test/test.php中的product_in_stock_cardiff 更新mik1vm_product SET product_sku ='',product_name ='',product_in_stock ='',product_in_stock_cardiff =''WHERE product_id ='74'

2 个答案:

答案 0 :(得分:0)

我没有看到你在任何地方都设置了 $ Submit 变量。尝试

if(isset($_POST['Submit']) && $_POST['Submit'] == 'Submit'){
    for($i=0;$i<$count;$i++){
        $sql1="UPDATE $tbl_name SET product_sku='$product_sku[$i]', product_name='$product_name[$i]', product_in_stock='$product_in_stock[$i]' , product_in_stock_cardiff='$product_in_stock_cardiff[$i]' WHERE product_id='$product_id[$i]'";
        $result1=mysql_query($sql1);
    }
}

将来你可以通过设置来避免这样的错误:

ini_set('error_reporting', E_ALL);

或者php.ini文件中的等效设置。

答案 1 :(得分:0)

<?php
    while($rows=mysql_fetch_array($result)){
    $currentProductId = $rows['product_id'];
    $product_id[] = $currentProductId;
    ?>
    <tr>
    <td align="center"><? echo $currentProductId; ?></td>
    <td align="center"><input name="product_sku[$currentProductId]" type="text" value="<? echo $rows['product_sku']; ?>"></td>
    <td align="center"><input name="product_name[$currentProductId]" type="text" value="<? echo $rows['product_name']; ?>"></td>
    <td align="center"><input name="product_in_stock[$currentProductId]" type="text" value="<? echo $rows['product_in_stock']; ?>"></td>
    <td align="center"><input name="product_in_stock_cardiff[$currentProductId]" type="text" value="<? echo $rows['product_in_stock_cardiff']; ?>"></td>
    </tr>
    <?php
    }

...

if(isset($_POST['Submit']) && $_POST['Submit'] == 'Submit'){
        for($i=0;$i<$count;$i++){
             $currentProductId = $product_id[$i];
            $sql1="UPDATE $tbl_name SET product_sku='$product_sku[$currentProductId]', product_name='$product_name[$currentProductId]', product_in_stock='$product_in_stock[$currentProductId]' , product_in_stock_cardiff='$product_in_stock_cardiff[$currentProductId]' WHERE product_id='$currentProductId'";
            $result1=mysql_query($sql1);
        }
    }