PHP和Mysql中的新闻系统

PHP和Mysql中的新闻系统,php,mysql,Php,Mysql,我已经试着让这个系统工作了几个小时,但仍然无法让它工作。这是一个直接复制/粘贴的教程,显然它适用于许多其他人,所以我不明白为什么它不适用于我!该表已创建,因此根本不是问题。。。系统不会在mysql数据库中输入任何内容,我也不会收到任何错误 教程可在以下位置找到: 我知道这是一个不安全的系统,但我会解决,只要我能让它工作 代码如下: config.php <?php $dbhost="localhost"; $dbusername="myusernam"; $dbpassword="myp

我已经试着让这个系统工作了几个小时,但仍然无法让它工作。这是一个直接复制/粘贴的教程,显然它适用于许多其他人,所以我不明白为什么它不适用于我!该表已创建,因此根本不是问题。。。系统不会在mysql数据库中输入任何内容,我也不会收到任何错误

教程可在以下位置找到:

我知道这是一个不安全的系统,但我会解决,只要我能让它工作

代码如下:

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");

?>
<?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

?>
<?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

?>

add_news.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");

?>
<?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

?>
<?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\u查询(“插入…”

请替换/扩展:

$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\u查询(“插入新闻(标题、数据时间、文本1、文本2))
值('$title',NOW(),'$text1','$text2'),$connect);
//打印成功消息。
echo“谢谢!新闻添加成功!
您将在(4)秒后重定向到主页”; 回声“;

$result=mysql\u查询(“插入新闻(标题、数据时间、文本1、文本2))
值('$title',NOW(),'$text1','$text2'),$connect);
如果($结果){
//打印成功消息。
echo“谢谢!新闻添加成功!
您将在(4)秒后重定向到主页”; 回声“; }否则{ echo(“错误:.mysql_Error()); } }//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>

什么是
$submit
add_news.php
中你必须定义
$submit=$\u POST['submit'];
在add_news.php中你必须
尝试检查主机上的错误报告是否设置为'true'。你可以在脚本的开头写:error_reporting(E_ALL);您是否确实使用与连接相同的详细信息设置了数据库?使用相同的结构等。如果这是教程的复制+粘贴,那么您需要找到更好的教程-这使用过时的PHP函数和糟糕的编码技术,极易被黑客攻击。感谢您的评论。我确实尝试了您的sug我得到了这个错误:解析错误:语法错误,在第31行和第31行的/add_news.php中出现意外的T_ELSE是这样的:ELSE{//begin of ELSE有什么建议吗?这很可能是因为你吃了一个
}
你需要一个来作为我添加的额外if/ELSE(如果插入失败,在插入后会出现错误)最后的大括号已经在那里了。
//if($submit)的结尾。
我把它编辑成了我的答案再次感谢。我试过了,但仍然没有出错,但系统也不工作!:(谢谢你的建议。那也不起作用!!我不明白发生了什么!!!