gpt4 book ai didi

php - 数据未发布到数据库中

转载 作者:行者123 更新时间:2023-11-29 12:25:25 25 4
gpt4 key购买 nike

我是 php 新手,开始开发一个动态网站,我创建了几个静态页面和一个新闻页面。

对于新闻页面,我已在 phpmyadmin 中创建了数据库,但我无法在数据库中获取任何数据,但我在图像文件夹中获取图像,请查看我的代码。

要插入我创建的帖子:

<html>
<head>
<title>Insert New Post</title>
</head>
<body>
<form method="post" action="insert_post.php" enctype="multipart/form-data">

<table allign="center" border="10" width="600">
<tr>
<td align="center" colspan="5" bgcolor="yellow">
<h1>Insert New Post Here</h1></td>
</tr>

<tr>
<td align="right">Post Title:</td>
<td><input type="text" name="title" size="40"></td>
</tr>

<tr>
<td align="right">Post Author:</td>
<td><input type="text" name="author"></td>
</tr>

<tr>
<td align="right">Post image:</td>
<td><input type="file" name="image"></td>
</tr>

<tr>
<td align="right">Post content:</td>
<td><textarea name="content" cols="40" rows="20"></textarea></td>
</tr>

<tr>
<td align="center" colspan="6"><input type="submit" name="submit" value="Publish Now"></td>
</tr>

</table>

</form>
</body>
</html>

<?php
include('includes/connect.php');


if(isset($_POST['submit'])){

$title = $_POST['title'];
$date = DATE('y-m-d');
$author = $_POST['author'];
$content = $_POST['content'];
$image_name = $_FILES['image']['name'];
$image_type = $_FILES['image']['type'];
$image_size = $_FILES['image']['size'];
$image_tmp = $_FILES['image']['tmp_name'];

if($title == '' or $author =='' or $content ==''){
echo "<script>alert('Any filed is empty')</script>";
exit();
}
if($image_type=="image/jpeg" or $image_type=="image/png" or $image_type=="image/gif"){

if($image_size<=50000){
move_uploaded_file($image_tmp,"images/$image_name");
}
else {
echo "<script>alert('image is larger, only 50kb size is allowed')</script>";
}
}
else {
echo "<script>alert('image type is invalid')</script>";
}

$query = "insert into post (post_title,post_date,post_author,post_image,post_content) values ('$title','$date','$author','$image_name','$content')";

if(mysql_query($query)){
echo "<center><h1>Post has been Published</h1></center>";

}
}

?>

现在我已经创建了 connect.php 文件,其中包含以下代码:

<?php
mysql_connect("localhost","root","");
mysql_select_db("rect");

?>

我是 php 新手,如果我做错了什么,很抱歉,提前谢谢你。

最佳答案

注意:

  • 确保您有一个名为 post 的表,并具有相应的列名称:post_titlepost_datepost_author , post_image, post_content
  • 按照@spencer7593的建议,我会尝试将您的代码转换为mysqli_*,因为它们是您的连接问题,同时也是为了防止SQL injections .

如果您要将日期插入数据库,而不是:

$date=DATE("y-m-d"); /* YY-MM-DD */

你应该这样做:

$date=DATE("Y-m-d"); /* YYYY-MM-DD */

首先,我们修复您与数据库的连接 (connect.php):

<?php

$mysqli = new mysqli("localhost", "root", "", "rect");

/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

?>

然后像这个简单的例子一样更改您的插入查询:

$stmt = $mysqli->prepare("INSERT INTO post (post_title, post_date, post_author, post_image, post_content) VALUES (?,?,?,?,?)");

$stmt->bind_param('sssss',$title,$date,$author,$image_name,$content); /* BIND VARIABLES TO THE QUERY */

$stmt->execute(); /* EXECUTE QUERY */

?>

关于php - 数据未发布到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28403299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com