I have put together php code that adds, deletes and updates records of book stocks in a database. The books are classified according to genre and when I select the genre (eg. horror) from a dropdown, details of all books from that genre will display. The code works as intended. Now I decided to use an if-else (or switch-case) to display records according to genre. And for now using just one genre (classics), to see if it works. The output is fine, but the actual code for deleting, editing and adding isn't running. Would really appreciate some advice on why this is so? Below is the code - the if statement is right there at the start after the dropdown:
<form action="sqltest.php" method="POST" name="theForm" id="theForm">
<select name="options">
<option value="classics">Classics</option>
<option value="horror">Horror</option>
<option value="recipes">Recipes</option>
<option value="historic">Historic</option>
<option value="entertainment">Entertainment</option>
<option value="educational">Educational</option>
</select>
<input type="submit" value="Load page" />
<?php $selection = $_POST['options'];
if ($selection == "classics"){
if (isset($_POST['delete']) && isset($_POST['isbndel']))
{
$isbn = get_post($conn, 'isbndel');
$query = "DELETE FROM classics WHERE isbn='$isbn'";
$result = $conn->query($query);
if (!$result) echo "DELETE failed: $query<br>" .
$conn->error . "<br><br>";
}
if (isset($_POST['author']) &&
isset($_POST['title']) &&
isset($_POST['category']) &&
isset($_POST['year']) &&
isset($_POST['isbn']) &&
isset($_POST['btn']))
{
$author = get_post($conn, 'author');
$title = get_post($conn, 'title');
$category = get_post($conn, 'category');
$year = get_post($conn, 'year');
$isbn = get_post($conn, 'isbn');
$filetmp = $_FILES["image"]["tmp_name"];
$filename = $_FILES["image"]["name"];
$filetype = $_FILES["image"]["type"];
$filepath = "images/".$filename;
move_uploaded_file($filetmp, $filepath);
$query = "INSERT INTO classics VALUES" .
"('$author', '$title', '$category', '$year', '$isbn', '$filename', '$filepath', '$filetype')";
$result = $conn->query($query);
if (!$result) echo "INSERT failed: $query<br>" .
$conn->error . "<br><br>";
}
//var_dump($_FILES['image2']);
if ($_FILES['image2'])
{
$ed_author = get_post($conn, 'ed_author');
$ed_title = get_post($conn, 'ed_title');
$ed_category = get_post($conn, 'ed_category');
$ed_year = get_post($conn, 'ed_year');
$hid_isbn = get_post($conn, 'hid_isbn');
if($_FILES['image2']['error'] != UPLOAD_ERR_NO_FILE){
$filetmp = $_FILES["image2"]["tmp_name"];
$filename = $_FILES["image2"]["name"];
$filetype = $_FILES["image2"]["type"];
$filepath = "images/".$filename;
move_uploaded_file($filetmp, $filepath);
$query = "UPDATE classics SET author='$ed_author', title='$ed_title', type='$ed_category', year='$ed_year', filename='$filename', filepath='$filepath', filetype='$filetype'WHERE isbn='$hid_isbn'";
$result = $conn->query($query);
if (!$result) echo "EDIT with image failed: $query<br>" .
$conn->error . "<br><br>";
$result = $conn->query($query);
}
else{
$query = "UPDATE classics SET author='$ed_author', title='$ed_title', type='$ed_category', year='$ed_year' WHERE isbn='$hid_isbn'";
$result = $conn->query($query);
if (!$result) echo "EDIT with image failed: $query<br>" . $conn->error . "<br><br>";
}
}
?>
<form action="sqltest.php" method="post" enctype="multipart/form-data"><pre>
Author <input type="text" name="author">
Title <input type="text" name="title">
Category <input type="text" name="category">
Year <input type="text" name="year">
ISBN <input type="text" name="isbn">
<input type="file" name="image">
<input type="submit" name="btn" value="Upload Image & ADD RECORD">
</pre></form>
<?php
$query = "SELECT * FROM classics";
$result = $conn->query($query);
if (!$result) die ("Database access failed: " . $conn->error);
$rows = $result->num_rows;
while ($row = $result->fetch_array(MYSQLI_BOTH))
{
?>
<div class="slab">
<div class="present">
<pre>
Author <?php echo $row[0]; ?>
Title <?php echo $row[1]; ?>
Category <?php echo $row[2]; ?>
Year <?php echo $row[3]; ?>
ISBN <?php echo $row[4]; ?>
Sleeve <br><?php echo "<img src='" . $row['filepath'] ." ' height='200' width='200'/>"; ?>
<form action="sqltest.php" method="post"><pre>
<input type="hidden" name="delete" value="yes">
<input type="hidden" name="isbndel" value="<?php echo $row[4]; ?>">
<input type="submit" value="DELETE RECORD">
</pre></form></div>
<div class="edit">
<pre>
<form action="sqltest.php" method="post" enctype="multipart/form-data"><pre>
Author <input type="text" name="ed_author" value="<?php echo $row[0]; ?>">
Title <input type="text" name="ed_title" value="<?php echo $row[1]; ?>">
Category <input type="text" name="ed_category" value="<?php echo $row[2]; ?>">
Year <input type="text" name="ed_year" value="<?php echo $row[3]; ?>">
ISBN <input type="text" name="hid_isbn" value="<?php echo $row[4]; ?>"readonly>
<input type="file" name="image2">
<input type="hidden" name="edit" value="yes">
<input type="submit" name="btn2" value="SUBMIT EDIT">
</pre></form>
</div>
</div>
<?php
}
$result->close();
$conn->close();
function get_post($conn, $var)
{
return $conn->real_escape_string($_POST[$var]);
}
}
?>
</html>
Aucun commentaire:
Enregistrer un commentaire