簡體   English   中英

將值插入mysql數據庫

[英]Insert Values into mysql database

我正在將值插入數據庫。 我需要插入產品。 用戶可以在前端添加多個產品。 為此,我有2個表到database.first是tbl_test和第二個tbl_test_months。 在tbl_test中,我要插入產品名稱和產品增長率。 產品價格和銷售將持續12個月。 因此,在第二張表中,我需要根據月份輸入產品ID,銷售,價格。 但是我的產品ID輸入錯誤。 我想在12個月后應該輸入第二個產品ID。這是我的代碼。 請幫我。

for($p=0; $p < $countProduct; $p++)
{
    $revInsert= "insert into tbl_test(id, user_id, product_name, price_growth_rate_year1, price_growth_rate_year2, sale_growth_rate_year1, sale_growth_rate_year2, scenario, currency_type, price_raise)
                values('','".$user_id."', '".$productName[$p]."', '".$growth_price_first[$p]."', '".$growth_price_second[$p]."', '".$growth_sale_first[$p]."', '".$growth_sale_second[$p]."', '".$scenario."', '".$pCurrency[$p]."', '".$priceRaiseStatus[$p]."')";
    $QueryRev= mysql_query($revInsert);
    $id= mysql_insert_id();
}
for($q=0; $q<$cnt; $q++)
{  
    $rev_month_insert= "insert into tbl_test_months(id, user_id, product_id, month, year, sale_volume, sale_price, scenario) 
    values('','".$user_id."', '".$id."','".$months1[$q % 12]."', '".$year."', '".$sale_volume1[$q]."', '".$price_currency_contract[$q]."', '".$scenario."')";
    $query= mysql_query($rev_month_insert);
}

您需要將id放入數組中並相應地插入它們。

$id_arr = array();
for($p=0; $p < $countProduct; $p++)
{
    $revInsert= "insert into tbl_test(id, user_id, product_name, price_growth_rate_year1, price_growth_rate_year2, sale_growth_rate_year1, sale_growth_rate_year2, scenario, currency_type, price_raise)
                values('','".$user_id."', '".$productName[$p]."', '".$growth_price_first[$p]."', '".$growth_price_second[$p]."', '".$growth_sale_first[$p]."', '".$growth_sale_second[$p]."', '".$scenario."', '".$pCurrency[$p]."', '".$priceRaiseStatus[$p]."')";
    $QueryRev= mysql_query($revInsert);
    $id_arr[] = mysql_insert_id();
}
for($q=0; $q<$cnt; $q++)
{  
    $rev_month_insert= "insert into tbl_test_months(id, user_id, product_id, month, year, sale_volume, sale_price, scenario) 
    values('','".$user_id."', '".$id_arr[$q]."','".$months1[$q % 12]."', '".$year."', '".$sale_volume1[$q]."', '".$price_currency_contract[$q]."', '".$scenario."')";
    $query= mysql_query($rev_month_insert);
}

並確保在tbl_test表中有一個auto increment字段,並且不要使用mysql_*函數,因為它們已被棄用,而應使用mysqli_*函數或PDO語句

從文檔( http://php.net/manual/zh/function.mysql-insert-id.php )。

返回值:成功時,前一個查詢為AUTO_INCREMENT列生成的ID;如果前一個查詢未生成AUTO_INCREMENT值,則為0;如果未建立MySQL連接,則為FALSE。

我通過使用mysql_insert_id假設您的id列是AUTO_INCREMENT列,在這種情況下,您不想在插入語句中指定id

即使列是自動遞增的,您仍然可以在插入查詢中提供id 但您的情況是您要提供'' 如果這是主鍵列,則由於重復的主鍵,您在第一個循環中的插入查詢應該在索引$p=1處失敗。

根據您提供的信息,我認為解決方案如下:

for($p=0; $p < $countProduct; $p++)
{
    $revInsert= "insert into tbl_test(user_id, product_name, price_growth_rate_year1, price_growth_rate_year2, sale_growth_rate_year1, sale_growth_rate_year2, scenario, currency_type, price_raise)
                values('".$user_id."', '".$productName[$p]."', '".$growth_price_first[$p]."', '".$growth_price_second[$p]."', '".$growth_sale_first[$p]."', '".$growth_sale_second[$p]."', '".$scenario."', '".$pCurrency[$p]."', '".$priceRaiseStatus[$p]."')";
    $QueryRev= mysql_query($revInsert);
    $product_id= mysql_insert_id();

    for($q=0; $q<$cnt; $q++)
    {  
      $rev_month_insert= "insert into tbl_test_months(user_id, product_id, month, year, sale_volume, sale_price, scenario) 
      values('".$user_id."', '".$product_id."','".$months1[$q % 12]."', '".$year."', '".$sale_volume1[$q]."', '".$price_currency_contract[$q]."', '".$scenario."')";
      $query= mysql_query($rev_month_insert);
    }

    // Update
    unset($product_id);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM