Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

php - Database Tables Get Mixed Up When Adding Data

I want to add crud features to my project, but I run in to a problem. I can add a post through the website, but inside the database the tables hold information that should be in a different row, see the image. I've managed to make the author and the title show up in the correct row, but I cant do the same thing for the post_date and last_update. as you can see, the image part of my post (I've just typed in image to test it) is stored inside the column dedicated to the last_update data. The content of my post is nowhere to be found for whatever reason.

I've also added the code that let me create posts below. I need this to work in order to add the rest of the crud features, but I have no idea how to fix this.

<?php

$msg = '';
// Check if POST data is not empty
if (!empty($_POST)) {
// Post data not empty insert a new record
// Set-up the variables that are going to be inserted, we must check if the POST variables exist if 
not we can default them to blank
$id = isset($_POST['id']) && !empty($_POST['id']) && $_POST['id'] != 'auto' ? $_POST['id'] : NULL;
// Check if POST variable "name" exists, if not default the value to blank, basically the same for 
all variables
$author = isset($_POST['author']) ? $_POST['author'] : '';
$title = isset($_POST['title']) ? $_POST['title'] : '';
$content = isset($_POST['content']) ? $_POST['content'] : '';
$post_date = isset($_POST['post_date']) ? $_POST['post_date'] : date('Y-m-d H:i:s');
$post_update = isset($_POST['post_update']) ? $_POST['post_update'] : date('Y-m-d H:i:s');
$image = isset($_POST['image']) ? $_POST['image'] : '';
// Insert new record into the pages table
$stmt = $conn->prepare('INSERT INTO pages VALUES (?, ?, ?, ?, ?, ?, ?)');
$stmt->execute([$id, $author, $title, $content, $post_date, $post_update, $image]);
// Output message
$msg = 'Created Successfully!';
}
?>


<div class="content update">
<h2>Create Post</h2>
<form action="create.php" method="post">
    <label for="id">ID</label>
    <input type="text" name="id" placeholder="26" value="auto" id="id">
    <label for="name">Content</label>
    <input type="text" name="auhtor" placeholder="Content" id="author">
    <label for='content'>Author</label>
    <input type='text' name='content' placeholder='username' id='content'>
    <label for='content'>Title</Title></label>
    <input type='text' name='title' placeholder='Title' id='title'>
    <label for="created">Post Update</label>
    <input type="datetime-local" name="post_update" value="<?=date('Y-m-dTH:i')?>" id="post_update">
    <label for="created">Post Date</label>
    <input type="datetime-local" name="post_date" value="<?=date('Y-m-dTH:i')?>" id="post_date">
    <label for="title">Image</label>
    <input type="text" name="image" placeholder="Image" id="image">
    <input type="submit" value="Create">
</form>
<?php if ($msg): ?>
<p><?=$msg?></p>
<?php endif; ?>
</div>
question from:https://stackoverflow.com/questions/65884664/database-tables-get-mixed-up-when-adding-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...