php - 将相同的值插入数据库两次

标签 php mysql

这是我的代码:

$text = $_REQUEST["enable"];
$description = 'Debited For Service';

foreach($text as $data)
{
    foreach($check as $data1)
    {
        $insert="INSERT INTO `wallet_transaction`(`wallet_id`, `debit_amount`, `description`) VALUES ('$esc_id', '$data1', '$data', '$description')";
        mysql_query($insert);
    }   
}

结果是这些查询已运行,​​但我只想要前两个查询:

INSERT INTO `wallet_transaction`(`wallet_id`, `debit_amount`, `description`) VALUES ('2', '200', 'Debited For Service')
INSERT INTO `wallet_transaction`(`wallet_id`, `debit_amount`, `description`) VALUES ('3', '100', 'Debited For Service')

INSERT INTO `wallet_transaction`(`wallet_id`, `debit_amount`, `description`) VALUES ('2', '200', 'Debited For Service')
INSERT INTO `wallet_transaction`(`wallet_id`, `debit_amount`, `description`) VALUES ('3', '100', 'Debited For Service')

最佳答案

只需使用一个循环,而不是嵌套循环,并一起访问两个数组的相应元素。

foreach ($text as $i => $data) {
    $data = mysql_real_escape_string($data);
    $data1 = mysql_real_escape_string($check[$i]);
    $insert="INSERT INTO `wallet_transaction`(`wallet_id`, `debit_amount`, `description`) VALUES ('$esc_id', '$data1', '$data', '$description')";
    mysql_query($insert);
}

还有另一个问题:您只列出了 3 列要插入的列,但有 4 个值。我猜这只是一个复制错误。

关于php - 将相同的值插入数据库两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27687405/

相关文章:

PHP从MYSQL中选择多个表

mysql - 使用 Ruby on Rails 连接到 MySQL

php - 直接使用另一个查询返回的值进行插入 (PHP)

PHP 警告包括 PHPUnit_Extensions_Story_TestCase.php 无法打开流

javascript - 使用 javascript 从共享 iCloud 获取数据

php - 如何屏蔽URL地址?

php - 比较两个表的值并为结果导出 JSON

mysql - 来自 SELECT 的多个 UPDATE,WHERE 子句中包含多行 [Mysql]

mysql - Regex MySQL - 匹配前两个字母,x 数字

mysql - 通过 CSV 将 JSON 字符串导入 MySQL。是好是坏?