写段PHP代码,判断数据表vtmpTab是否有指标ID指标,如有则删除此记录,同时删除些记录丰关联的明细表VtmpD
时间: 2024-05-10 14:19:20 浏览: 50
以下是可能的实现方式:
```php
// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 设置要删除的指标ID
$indicatorID = 123;
// 在vtmpTab中查找指标ID
$sql = "SELECT * FROM vtmpTab WHERE indicatorID = $indicatorID";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// 如果找到了指标ID,则删除记录和关联的明细表
$row = $result->fetch_assoc();
$vtmpID = $row["vtmpID"];
$sql = "DELETE FROM vtmpD WHERE vtmpID = $vtmpID";
$conn->query($sql);
$sql = "DELETE FROM vtmpTab WHERE vtmpID = $vtmpID";
$conn->query($sql);
echo "删除成功";
} else {
echo "未找到指标ID";
}
$conn->close();
```
需要根据实际情况调整数据库连接参数和SQL语句。此外,为了防止SQL注入攻击,建议使用预处理语句。
阅读全文