Skip to content

Commit

Permalink
Merge pull request #3 from AlphaRomeoMike/feature/updatingData
Browse files Browse the repository at this point in the history
Data is being updated
  • Loading branch information
AlphaRomeoMike committed Jan 17, 2021
2 parents 2fb3869 + 344cdf8 commit 7adbd52
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
32 changes: 26 additions & 6 deletions database.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,40 @@ public function select($table_name) {


//* FETCH SINGLE DATA
public function select_where($table_name, $where_condition) {
public function select_where($table_name, $where) {
$condition = '';
$array = array();
foreach($where_condition as $key => $values) {
$condition .= $key . " = '". $values ."' AND";
foreach($where as $key => $value) {
$condition .= $key . " = '".$value."' AND ";
}
$condition = substr($condition, 0, -5);

$query = "SELECT * FROM ".$table_name." WHERE " . $condition;
$query = "SELECT * FROM $table_name WHERE $condition";
$result = mysqli_query($this->con, $query);
while($row = mysqli_fetch_assoc($result)) {

while($row = mysqli_fetch_array($result)) {
$array[] = $row;
}
return $array;
}

//* UPDATE SINGLE DATA
public function update($table_name, $fields, $where) {
$query = '';
$condition = '';

foreach($fields as $key => $value) {
$query .= $key. "='".$value."', ";
}
$query = substr($query, 0, -2);
foreach($where as $key => $value) {
$condition .= $key. "='".$value."' AND ";
}
$condition = substr($condition, 0, -5);
$query = "UPDATE $table_name SET $query WHERE $condition";

if(mysqli_query($this->con, $query)) {
return true;
}
}
}
?>
33 changes: 29 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@
$success = 'Data inserted';
}
}

if (isset($_POST['edit'])) {
$title = validate($_POST['post_title']);
$desc = validate($_POST['post_description']);

$update = array(
'post_title' => mysqli_real_escape_string($data->con, $title),
'post_desc' => mysqli_real_escape_string($data->con, $desc)
);

$where = array(
'post_id' => $_POST['post_id']
);

if ($data->update('tbl_posts', $update, $where)) {
header("location:index.php?updated=1");
}
}

?>

<!DOCTYPE html>
Expand Down Expand Up @@ -45,6 +64,7 @@
);
$single_data = $data->select_where("tbl_posts", $where);
foreach ($single_data as $post) {
//? UPDATE FORM DATA
?>
<div class="form-group">
<label for="post_title"><strong>Title</strong></label>
Expand All @@ -53,15 +73,20 @@
</div>
<div class="form-group">
<label for="post_description">Description</label>
<textarea name="post_description" id="post_description" class="form-control" placeholder="Enter post title"><?php echo $post['post_title']; ?></textarea>
<textarea name="post_description" id="post_description" class="form-control" placeholder="Enter post title"><?php echo $post['post_desc']; ?></textarea>
<small id="help_post" class="text-muted">Eg. Any message you would like to say</small>
</div>
<input type="hidden" name="post_id" value="<?php echo $post['post_id']; ?>">
<input type="submit" value="Submit" name="edit" class="btn btn-outline-secondary col-md-3" />
<input type="submit" value="Edit" name="edit" class="btn btn-outline-secondary col-md-3" />
<span><?php if (isset($_GET['updated'])) {
$success = 'Post Updated';
} ?></span>

<?php
}
}
} else {
//? INSERT FORM DATA
?>
<div class="form-group">
<label for="post_title"><strong>Title</strong></label>
Expand Down Expand Up @@ -101,13 +126,13 @@
$counter = 1;
$post_data = $data->select("tbl_posts");
foreach ($post_data as $post) {

?>
<tr>
<td width="5%"><?php echo $counter++; ?></td>
<td width="15%"><?php echo $post["post_title"]; ?></td>
<td width="50%"><?php echo substr($post["post_desc"], 0, 200); ?></td>
<td width="20%"><span><a href="" class="btn btn-outline-primary">View</a></span>
<td width="15%"><span><a href="" class="btn btn-outline-primary">View</a></span>
<span><a href="index.php?edit=1&post_id=<?php echo $post["post_id"]; ?>" class="btn btn-outline-info">Edit</a></span>
<span><a href="#" id="<?php echo $post["post_id"]; ?>" class="btn btn-outline-danger">Delete</a></span>
</td>
Expand Down

0 comments on commit 7adbd52

Please sign in to comment.