簡體   English   中英

PHP和Mysql中的新聞系統

[英]News system in PHP and Mysql

我一直在嘗試使該系統工作幾個小時,但仍然無法使其正常工作。 這是直接來自教程的復制/粘貼,而且顯然對許多其他人都有用,所以我不明白為什么它對我不起作用! 該表已經創建,所以這根本不是問題...系統將不會在mysql數據庫中輸入任何內容,我也不會收到任何錯誤!

可以在以下位置找到該教程: http : //www.codingforums.com/archive/index.php/t-46916.html

我知道這不是一個安全的系統,但是只要我能使它正常工作,我就會對其進行整理!

這是代碼:

config.php

<?php

$dbhost="localhost";
$dbusername="myusernam";
$dbpassword="mypass";
$dbname="dataname";

$connect = mysql_connect($dbhost, $dbusername, $dbpassword);
mysql_select_db($dbname,$connect) or die ("Could not select database");

?>

add_news.php

<?php

include("config.php");

if($submit)
{//begin of if($submit).

// Set global variables to easier names
$title = $_POST['title'];
$text1 = $_POST['text1'];
$text2 = $_POST['text2'];


//check if (title) field is empty then print error message.
if(!$title){ //this means If the title is really empty.
echo "Error: News title is a required field. Please fill it.";
exit(); //exit the script and don't do anything else.
}// end of if
//run the query which adds the data gathered from the form into the database
$result = mysql_query("INSERT INTO news (title, dtime, text1, text2)
VALUES ('$title',NOW(),'$text1','$text2')",$connect);
//print success message.
echo "<b>Thank you! News added Successfully!<br>You'll be redirected to Home Page after (4) Seconds";
echo "<meta http-equiv=Refresh content=4;url=index.php>";
}//end of if($submit).


// If the form has not been submitted, display it!
else
{//begin of else

?>
<br>
<h3>::Add News</h3>

<form method="post" action="<?php echo $PHP_SELF ?>">
Title: <input name="title" size="40" maxlength="255">
<br>
Text1: <textarea name="text1" rows="7" cols="30"></textarea>
<br>
Text2: <textarea name="text2" rows="7" cols="30"></textarea>
<br>
<input type="submit" name="submit" value="Add News">
</form>
<?
}//end of else

?>

news_view.php

<?php

// load the configuration file.

include("config.php");
//load all news from the database and then OREDER them by newsid
//you will notice that newlly added news will appeare first.
//also you can OREDER by (dtime) instaed of (news id)
$result = mysql_query("SELECT * FROM news ORDER BY newsid DESC",$connect);

//lets make a loop and get all news from the database
while($myrow = mysql_fetch_assoc($result))
{//begin of loop

//now print the results:
echo "<b>Title: ";
echo $myrow['title'];
echo "</b><br>On: <i>";
echo $myrow['dtime'];
echo "</i><hr align=left width=160>";
echo $myrow['text1'];

// Now print the options to (Read,Edit & Delete the news)
echo "<br><a href=\"read_more.php?newsid=$myrow[newsid]\">Read More...</a>
|| <a href=\"edit_news.php?newsid=$myrow[newsid]\">Edit</a>
|| <a href=\"delete_news.php?newsid=$myrow[newsid]\">Delete</a><br><hr>";

}//end of loop

?>

這應該更多是注釋,但是由於那里的格式化選項非常有限,並且我有很多注釋,因此請看:

誰寫的那個例子……都不是最好的工作。

根本沒有錯誤處理( mysql_query("INSERT... )。

請更換/擴展:

$result = mysql_query("INSERT INTO news (title, dtime, text1, text2)
VALUES ('$title',NOW(),'$text1','$text2')",$connect);
//print success message.
echo "<b>Thank you! News added Successfully!<br>You'll be redirected to Home Page after (4) Seconds";
echo "<meta http-equiv=Refresh content=4;url=index.php>";

$result = mysql_query("INSERT INTO news (title, dtime, text1, text2)
VALUES ('$title',NOW(),'$text1','$text2')",$connect);
if ( $result ){
//print success message.
echo "<b>Thank you! News added Successfully!<br>You'll be redirected to Home Page after (4) Seconds";
echo "<meta http-equiv=Refresh content=4;url=index.php>";
} else {
echo("Error: ".mysql_error() );
}
}//end of if($submit).

我相信插件沒有運行(出於任何原因,您現在都希望可以看到)

試試這個代碼:

config.php

<?php


$username = "myusernam";
$password = "mypass";

try {
    $connect = new PDO('mysql:host=localhost;dbname=dataname', $username, $password);
    $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

?>

addnews.php

<?php

include("config.php");

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

$title = $_POST['title'];
$text1 = $_POST['text1'];
$text2 = $_POST['text2'];


if(empty($title)){ 
echo "Error: News title is a required field. Please fill it.";
exit(); 
}

$sql = "INSERT INTO news (title, dtime, text1, text2) VALUES (:title, NOW(), :text1, :text2)";
         $stmt = $connect->prepare($sql);
         $stmt->bindParam(':title', $title, PDO::PARAM_STR);
         $stmt->bindParam(':text1', $text1, PDO::PARAM_STR);
         $stmt->bindParam(':text2', $text2, PDO::PARAM_STR);
         $stmt->execute();


echo "<b>Thank you! News added Successfully!<br>You'll be redirected to Home Page after (4) Seconds";
echo "<meta http-equiv=Refresh content=4;url=index.php>";


}
?>

<br>
<h3>::Add News</h3>

<form method="post" action="<?php echo $PHP_SELF ?>">
Title: <input name="title" size="40" maxlength="255">
<br>
Text1: <textarea name="text1" rows="7" cols="30"></textarea>
<br>
Text2: <textarea name="text2" rows="7" cols="30"></textarea>
<br>
<input type="submit" name="submit" value="Add News">
</form>

news_view.php

<?php



include("config.php");


$stmt = $connect->prepare('SELECT * FROM news ORDER BY newsid desc');
$stmt->execute();


while($sa = $stmt->fetch(PDO::FETCH_ASSOC)) {



echo "<b>Title: ";
echo $sa['title'];
echo "</b><br>On: <i>";
echo $sa['dtime'];
echo "</i><hr align=left width=160>";
echo $sa['text1'];


echo "<br><a href=\"read_more.php?newsid=$sa[newsid]\">Read More...</a>
|| <a href=\"edit_news.php?newsid=$sa[newsid]\">Edit</a>
|| <a href=\"delete_news.php?newsid=$sa[newsid]\">Delete</a><br><hr>";

}

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM