我尝试设置一个简单的函数来在重命名主文件夹时更新子文件夹的路径。
这是我的代码:
case 'rename':
$dirpath = rtrim(preg_replace('~/+~', '/', (str_replace('\\', '/', $_POST['old-dirpath']))), '/');
$new_dirpath = rtrim(preg_replace('~/+~', '/', (str_replace('\\', '/', $_POST['new-dirpath']))), '/');
$sql = "
UPDATE privatedir SET
dirpath = '{$new_dirpath}'
WHERE dirpath = '{$dirpath}'
";
$db->execute($sql);
$rs = $db->query("SELECT * FROM privatedir WHERE `dirpath` LIKE '%.$dirpath.%'");
$folders = $db->fetchAll($rs);
print_r ($folders); print_r($dirpath); die;
foreach ($folders as $folder){
echo $folder['dirpath']; die;
$folder_new_dirpath = str_replace($dirpath, $new_dirpath, $folder['dirpath']);
$sql = "
UPDATE privatedir SET
dirpath = '{$new_dirpath}'
WHERE dirpath = '{$folder['dirpath']}'
";
$db->execute($sql);
}
我的问题与行$rs = $db->query("SELECT * FROM privatedir WHERE
dirpath LIKE '%.$dirpath.%'");
有关,即使我的数据库中有匹配的结果也不会给我任何回报,如果我print_r $folders
它只是打印我一个空数组。
但是,如果我直接将.$dirpath.
替换为字符串(例如C:/wamp/www/gg/ftp/repository/folder1
),它将返回匹配结果,print_r $folders
将打印出相应的数组。
我做错了什么?
答案 0 :(得分:2)
将变量放在引号中,如thi
$rs = $db->query("SELECT * FROM privatedir WHERE `dirpath` LIKE '%'".$dirpath."'%'");
答案 1 :(得分:1)
事情就是你不需要周围的点,因为你没有连接字符串。
这应该有效:
$rs = $db->query("SELECT * FROM privatedir WHERE `dirpath` LIKE '%$dirpath%'");
您也可以
$rs = $db->query("SELECT * FROM privatedir WHERE `dirpath` LIKE '%".$dirpath."%'");
如果你更喜欢它。