2015-02-09 54 views
0

我是新來的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很抱歉,如果我這樣做有問題的任何錯誤,並提前謝謝你。

+0

你確定你的根文件夾中包含'includes'文件夾嗎?你的'connect.php'在裏面嗎? – 2015-02-09 05:26:29

+0

'echo $ query'。打印SQL並在phpmyadmin中打開它並檢查它是否有效? – Sky 2015-02-09 05:27:38

+0

'print_r($ _ POST)'和@ CWC15一樣 – Bender 2015-02-09 05:29:04

回答

1

注:

  • 確保你有一個與此相應的列名命名post表:post_titlepost_datepost_authorpost_imagepost_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 */ 

?> 
+0

非常感謝你,現在工作......再次感謝 – Desai 2015-02-09 08:52:46