php如何循环添加数据,php – 循环通过$_POST并添加到数据库

我想要做的是循环文本输入,用户输入博客文章的标签.我想将每个标记添加到数据库(如果它尚不存在).

当我在数据库中测试时,下面的实际查询字符串有效.

但是我认为我的循环语法可能不太正确,因为我没有添加任何内容.

任何人都可以在循环中发现错误导致我的“添加到数据库”失败吗?

在此先感谢您的帮助!

foreach ($_POST['__tags'] as $key=>$ls_value) {

$value = strtolower(mysql_real_escape_string($ls_value));

mysql_query("INSERT INTO `table` (`field`)

SELECT * FROM (SELECT '$value') as tmp

WHERE NOT EXISTS (

SELECT `field` FROM `table` WHERE `field` = '$value')

LIMIT 1") or trigger_error(mysql_error(), E_USER_ERROR);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<?php // 连接数据库 $conn = mysqli_connect("localhost", "tms", "123456", "nut"); // 检查连接是否成功 if (!$conn) { die("数据库连接失败: " . mysqli_connect_error()); } // 处理表单提交 if ($_SERVER["REQUEST_METHOD"] == "POST") { // 获取表单数据 $aoiSteps = $_POST['aoi_step']; $defectTypes = $_POST['defect_type']; $layerCodes = $_POST['layer_code']; $types = $_POST['type']; $dpets = $_POST['dpet']; $subcodes = $_POST['subcode']; $codeDescriptions = $_POST['code_description']; $determinationRules = $_POST['determination_rule']; $imagePaths = []; // 存储图片路径的数组 // 处理上传的图片 for ($i = 0; $i < count($aoiSteps); $i++) { $imagePaths = []; // 存储图片路径的数组 // 处理每个字段对应的图片 for ($j = 1; $j <= 5; $j++) { $imageField = "image" . $j; $targetDir = "D:/phpstudy_pro/WWW/192.168.1.16/images"; // 设置上传目录的路径 $fileName = uniqid() . '_' . $_FILES[$imageField]["name"][$i]; // 生成唯一文件名 $targetFile = $targetDir . '/' . basename($fileName); // 将反斜杠替换为正斜杠 $targetFile = str_replace('\\', '/', $targetFile); if (isset($_FILES[$imageField]["tmp_name"][$i]) && $_FILES[$imageField]["error"][$i] == UPLOAD_ERR_OK && move_uploaded_file($_FILES[$imageField]["tmp_name"][$i], $targetFile)) { $imagePath = $targetFile; } else { $imagePath = ""; } $imagePaths[] = $imagePath; } // 绑定参数 $stmt->bind_param("sssssssssssss", $aoiSteps[$i], $defectTypes[$i], $layerCodes[$i], $types[$i], $dpets[$i], $subcodes[$i], $codeDescriptions[$i], $imagePaths[0], $imagePaths[1], $imagePaths[2], $imagePaths[3], $imagePaths[4], $determinationRules[$i]); if ($stmt->execute()) { echo "数据插入成功"; } else { echo "数据插入失败: " . $stmt->error; } } // 关闭数据库连接 mysqli_close($conn); } ?>无法正常上传
最新发布
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值