我有一點問題。PHP如何使用另一個if語句中的變量?
我正在製作提交簡單的文章到數據庫,然後顯示在admin.php頁面上的表單。
一切工作正常,除了圖像。 首先我將它上傳到我的服務器,然後嘗試將字符串(此圖像的路徑)添加到數據庫。
但是我不能在第二個if語句中使用變量$ filepath。 我可以在圖片上傳後回顯,但不能在其他情況下使用。
你能幫助我嗎? 在此先感謝。
這裏是代碼:
<?php session_start();
if (!isset($_SESSION['logged-in']))
{
header('Location: index.php');
exit();
}
?>
<?php
include('db_connect.php');
if(isset($_POST['btn_upload'])) {
$filetmp = $_FILES["file_img"]["tmp_name"];
$filename = $_FILES["file_img"]["name"];
$filetype = $_FILES["file_img"]["type"];
$filepath = "photo/".$filename;
move_uploaded_file($filetmp,$filepath);
$result = mysqli_query($mysqli, "INSERT INTO upload_img (img_name,img_path,img_type) VALUES ('$filename','$filepath','$filetype')");
echo '<img src="' . $filepath . '" alt="">';
echo $filepath;
}
if (isset($_POST['add'])) {
$title = strip_tags($_POST['title']);
$content = strip_tags($_POST['content']);
$image = strip_tags($filepath);
$statement = $mysqli->prepare("INSERT bikes (title,image,content) VALUES (?,?,?)");
$statement->bind_param("sss",$title,$image,$content);
$statement->execute();
$statement->close();
header('Location: admin.php');
}
?>
<form method="post" action="" class="ui form">
<div class="required field">
<label>Title</label>
<input type="text" name="title" id="title">
</div>
<div class="required field">
<label>Content</label>
<textarea name="content" id="content" cols="30" rows="10"></textarea>
</div>
<div class="required field">
<label>Image</label>
<input type="text" name="image" id="image">
</div>
<input type="submit" class="ui primary button" id="add" name="add" value="Add article"></input>
</form>
<form action="addbike.php" method="post" enctype="multipart/form-data">
<input type="file" name="file_img" />
<input type="submit" name="btn_upload" value="Upload">
</form>
謝謝你的男人。 我試了一下,它的工作原理。 我永遠不會認爲它會如此簡單。 –