我正在尝试使用sql中的pdo在数据库中插入数据。我没有收到任何类型的错误。但是经过多次尝试后,数据仍未显示在数据库中。请帮我。查找是否有任何错误。
<?php $pdo = new PDO('mysql:host=localhost;dbname=fizday', 'root', '');?>
<?php
class Main {
//fetching posts from database
public
function posts() {
global $pdo;
$query = $pdo->prepare( "SELECT * FROM `posts`,`tbl_users` WHERE userID = user_id_p ORDER BY `post_id` DESC" );
$query->execute();
return $query->fetchAll();
}
//add new post if user post
public
function add_post( $user_id, $status ) {
global $pdo;
$query = $pdo->prepare( 'INSERT INTO posts (post_id, user_id_p, status, status_time) VALUES (NULL, ?, ?, CURRENT_TIMESTAMP)' );
$query->bindValue( 1, $user_id );
$query->bindValue( 2, $status );
$query->execute();
header( 'Location: index.php' );
}
//fetch user data by user id
public
function user_data( $user_id ) {
global $pdo;
$query = $pdo->prepare( 'SELECT * FROM tbl_users WHERE userID = ?' );
$query->bindvalue( 1, $user_id );
$query->execute();
return $query->fetch();
}
//timeAgo Function
public
function timeAgo( $time_ago ) {
$time_ago = strtotime( $time_ago );
$cur_time = time();
$time_elapsed = $cur_time - $time_ago;
$seconds = $time_elapsed;
$minutes = round( $time_elapsed / 60 );
$hours = round( $time_elapsed / 3600 );
$days = round( $time_elapsed / 86400 );
$weeks = round( $time_elapsed / 604800 );
$months = round( $time_elapsed / 2600640 );
$years = round( $time_elapsed / 31207680 );
// Seconds
if ( $seconds <= 60 ) {
return "just now";
}
//Minutes
else if ( $minutes <= 60 ) {
if ( $minutes == 1 ) {
return "one minute ago";
} else {
return "$minutes minutes ago";
}
}
//Hours
else if ( $hours <= 24 ) {
if ( $hours == 1 ) {
return "an hour ago";
} else {
return "$hours hrs ago";
}
}
//Days
else if ( $days <= 7 ) {
if ( $days == 1 ) {
return "yesterday";
} else {
return "$days days ago";
}
}
//Weeks
else if ( $weeks <= 4.3 ) {
if ( $weeks == 1 ) {
return "a week ago";
} else {
return "$weeks weeks ago";
}
}
//Months
else if ( $months <= 12 ) {
if ( $months == 1 ) {
return "a month ago";
} else {
return "$months months ago";
}
}
//Years
else {
if ( $years == 1 ) {
return "one year ago";
} else {
return "$years years ago";
}
}
}
}
?>
<?php
$get = new Main;
$send = new Main;
@$user_id = $_SESSION[ 'userID' ];
//fetching user data by user_id
$data = $get->user_data( $user_id );
// fetching posts from database
$post = $get->posts();
//check user submit data
if ( isset( $_POST[ 'submit' ] ) ) {
$status = $_POST[ 'status' ];
if ( !empty( $status ) === true ) {
$send->add_post( $user_id, $status );
}
}
?>
<div class="pt-4">
<div class="row">
<div class="column1">
<div class="sidebar">
<h4>Add Your Story</h4>
<form action="" method="post" enctype="multipart/form-data">
<textarea name="status"></textarea>
<div class="pt-2">
<button class="btn btn-gray" type="submit" name="submit">POST</button>
</div>
</form>
</div>
</div>
<div class="column2">
<?php foreach($post as $row){ $time_ago = $row['status_time']; ?>
<div class="card">
<div class="user-header">
<div class="user-pic">
<img src="images/user/default.png" class="user-picture" alt="matthewelsom's profile picture">
</div>
<div class="user-details">
<a href="#" class="user-name">
<?php echo $row['userName']?>
</a>
<div class="user-time">
<?php echo $get->timeAgo($time_ago) ?>
</div>
</div>
</div>
<div class="user-story">
<span class="more">
<?php echo $row['status'] ?>
</span>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
请帮助我克服这一点,因此我可以将我的数据插入数据库,也可以使用pdo从数据库中检索它。