Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Whenever I click edit it shows these errors below



Notice: Undefined variable: _Get in C:\xampp\htdocs\OOP1\blog\admin\edit_post.php on line 9

Fatal error: Call to a member function fetch_array() on a non-object in C:\xampp\htdocs\OOP1\blog\admin\edit_post.php on line 13

What I have tried:

PHP
          <?php 
include "../libs/config.php";
include "../libs/db.php";
 

//Creating database object
$db = new database();
//getting the id
$id = $_Get['id'];

$query = "SELECT * FROM posts WHERE id = '$id'";
$posts = $db ->select($query);
$single = $posts ->fetch_array();

//Inserting Posts
if(isset($_POST['submit'])){
  //creating variables for texts
  $title = $_POST['title'];
  $content = $_POST['content'];
  $cat = $_POST['cat'];
  $author = $_POST['author'];
  $tags = $_POST['tags'];
  //creating varibles for image
  $image = $_FILES['image']['name'];
  $image_tmp = $_FILES['image']['tmp_image'];
  

move_uploaded_file($image_tmp,"../images/$image");
  $query = "INSERT INTO posts (category_id,title,content,author,image,tags) VALUES ('$cat','$title','$content','$auhor','$image','$tags')";
 $run = $db->insert($query);
}

?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../../../favicon.ico">

    <title>Admin Panel</title>

    <!-- Bootstrap core CSS -->
    <link href="../styles/bootstrap.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="../styles/custom.css" rel="stylesheet">
  </head>

  <body>

    <div class="blog-masthead">
      <div class="container">
        <nav class="nav">
          <a class="nav-link active" href="index.php">Dashboard</a>
          <a class="nav-link" href="add_post.php">Add New Post</a>
          <a class="nav-link" href="add_category.php">Add New Category</a>
          <a class="nav-link pull-right" href="../index.php">View Blog</a>
          <a class="nav-link pull-right" href="logout.php">Logout</a>
        </nav>
      </div>
    </div>

    <div class="blog-header">
      <div class="container">
        <h1 class="blog-title"></h1>
        <p class="lead blog-description"></p>
      </div>
    </div>

    <div class="container">

      <div class="row">

        <div class="col-sm-12 blog-main">
        <br/>
              <form action="add_post.php" method="post" enctype="multipart/form-data">
  <div class="form-group">
    <label>Post title:</label>
    <input type="text" name = "title" class="form-control"  placeholder="Enter a title" value="<?php echo $single['title'];?>"/>
    
  </div>
  <div class="form-group">
  <label>Post Content:</label>
   <textarea class="form-control" rows="3" name="content" placeholder = "Enter a content"></textarea>
   </div>
   <select name="cat" class="form-control">
    <option>Select a Category</option>
    <?php while ($row1 = $cats->fetch_array()) :?> 
    <option value="<?php echo $row['id'];?>"><?php echo $row1['title'];?></option>
 

 <?php endwhile ;?>
 </select>
  <div class="form-group">
    <label>Author Name:</label>
    <input type="text" name="author" class="form-control"  placeholder="Enter author name">
  </div>
  <div class="form-group">
 <label>Post Image:</label>
 <input type="file" name="image">
 </div> 
  <div class="form-group">
    <label>Tags:</label>
    <input type="text" name="tags" class="form-control"  placeholder="Enter text">
  </div>
  <button type="submit" name="submit" class="btn btn-success">Submit</button>
  <a href="index.php" class="btn btn-danger">Cancel</a>

</form>
          </div><!-- /.blog-main -->

          <?php include "includes/footer.php";

          ?>
Posted
Updated 15-Oct-17 13:01pm

change
$id = $_Get['id'];
as
$id = $_GET['id'];
Reason: $_GET is a the correct syntax to access get data in php.
 
Share this answer
 
v3
PHP
$query = "SELECT * FROM posts WHERE id = '$id'";

PHP
$query = "INSERT INTO posts (category_id,title,content,author,image,tags) VALUES ('$cat','$title','$content','$auhor','$image','$tags')";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900