Ricevo questo errore nel seguente codice che è collegato ad un'altro per i commenti se potreste darci un'occhiata e capire dove sbaglio perchè fino a che non metto il form per i commenti tutto funziona quando cerco di inviare un commento ad un determinato post mi da l'errore Error was: Undefined index: title line 9... Grazie pr l'aiuto.
ecco i due codici:
1)
per i commenti il seguente:
2)
ecco i due codici:
1)
PHP:
<?php
function Blog (Database $db, Request $request){
$author ="";
$title ="";
$article="";
$msg_to_user="";
if (isset($_POST['author'])) {
$author = $_POST['author'];
$title = $_POST['title']; //qui da l'errore quando premo send nel form dei commenti solo che nel form dei commenti non ho un titolo perchè non è bisogno
$article = $_POST['article'];
$sql = "SELECT * FROM articles WHERE art_title='$title'";
$result = $db->getData($sql);
$numRows =0;
foreach ($result as $row){
$numRows++;
}
if (!$author) {
$msg_to_user = "<br /><br /><h4><font color='FF0000'>Please insert author!</font></h4>";
}else if (!$title){
$msg_to_user = "<br /><br /><h4><font color='FF0000'>Please insert title!</font></h4>";
}else if (!$article) {
$msg_to_user = "<br /><br /><h4><font color='FF0000'>Please insert article!</font></h4>";
} else if ( $numRows > 0) {
$msg_to_user = "<br /><br /><h4><font color='FF0000'>'$title' exist please choose a different one!.</font></h4>";
} else {
$sql = "INSERT INTO articles (art_author, art_title, art_articles, art_date) VALUES('$author','$title', '$article', now())";
$db->query($sql);// !!!!!!IMPORTANT FOR RUN THE SQL!!!!!!!!!!!!!!!!
//echo mysql_error();
$msg_to_user = "<br /><br /><h4><font color='0066FF'>Thanks '$author', article posted.</font></ h4>";
$author = "";
$title = "";
$article = "";
}
}
$out = "<section class='blog'>";
$out .= "<h1>share here your opinions and your comments on our blog</h1>";
$out .="<fieldset class='blog_form'>";
$action = "index.php?page=blog&postState=new-article-posted";
$out .= "<form action='$action' method='post' class='bl_form'>";
$out .= "Author:<br>";
$out .= "<input name='author' type='text' size='30' value='$author'><br>";
$out .= "Title:<br>";
$out .= "<input name='title' type='text' size='30' value='$title'><br>";
$out .= "Article:<br>";
$out .= "<textarea name='article' cols='40' rows='10' value='$article'></textarea><br>";
$out .= "<input name='submit' type='submit' value='Send'>";
$out .= "$msg_to_user";
$out .= "</form>";
$out .= "</fieldset>";
$whichArticle = $request->get("show-article");
if ($whichArticle) {
$out .= "<div class='article_blg'>";
$out .= showArticle($db, $whichArticle);//One article
include_once 'views/comments_art.php'; //qui ho incluso i commenti senza questo tutto il resto funziona
$out .= Comments_art($db, $whichArticle);//comments included here
$out .= "</div>";
} else {
$out .= showAllTitles($db);//list of titles
}
return $out;
}
function showAllTitles(Database $db){
$out = "<div class='articles_blog'>";
$out .= "<h1>ARTICLES</h1>";
$out .= "<ul id='articles_bl'>";
//Get all Articles from db and shows all titles
$allArticle = getAllArticles($db);
foreach ($allArticle as $row) {
//Must take all rows and output list -> titles??
$id = $row->get("art_id");
$date = $row->get("art_date");
$article = $row->get("art_title");
$txt = $row->get("art_articles");
$date = preg_replace('/^(.{4})-(.{2})-(.{2})$/','$3-$2-$1', $date);
$href = "index.php?page=blog&show-article=$article";
$out .= "<li>";
$out .= "<a href='$href'>$article</a>  ";
$out .= "<em>$date</em>";
$out .= "<p class='prev_art'>";
$out .= preview($txt, 10, $href);
//$out .= substr($txt, 0, 30);
/*$preview = new Preview();
$preview->makePreview("$article");
$out .= "$preview";*/
$out .= "</p>";
$out .= "<a href='index.php?page=blog&show-article=$article'>Read more</a>";
$out .= "</li>";
}
$out .= "</ul>";
$out .= "</div>";
$out .= "</section>";
return $out;
}
//Get from DB all Articles
function getAllArticles(Database $db) {
$sql = "SELECT * FROM articles ORDER BY art_date DESC";
$db->query($sql);
//$sql = "SELECT art_title FROM articles";
$table = $db->getData($sql);
return $table;
}
function showArticle(Database $db, $whichArticle) {
$data = getArticle($db, $whichArticle);
$title = $data->get("art_title");
$author = $data->get("art_author");
$article = $data->get("art_articles");
//$out = "<div class='article_blg'>";
$out = "<h1>Title:</h1><p>$title</p>";
$out .= "<h2>Author:</h2><p>$author</p>";
$out .= "<h3>Article:</h3><p>$article<br></p>";
//$out .= "</div>";
return $out;
}
function getArticle(Database $db, $articles) {
$fname = $db->escapeString($articles);
$sql = "SELECT art_title, art_articles, art_author FROM articles WHERE art_title = '$fname'";
$db->query($sql);
$data = $db->getData($sql);
$row = $data->getAt(0);
return $row;
}
function preview($txt, $long, $final) {
return (count($words = explode(' ', $txt)) > $long) ? implode(' ', array_slice($words, 0, $long)) . $final : $txt;
}
2)
PHP:
function Comments_art (Database $db, $article){
$out = html("section")->attr("art_comments");
$formData = new Request("post");
$newCommentSubmitted = $formData->get("new-comment");
if ( $newCommentSubmitted ) {
insertComment( $db, $formData );
$formData->clear();
}
$out->append( showForm($article) );
$out->append( showComments( $db, $article) );
return $out->asHTML();
}
function showComments (Database $db, $article) {
$allComments = getComments( $db, $article );
$ul = html("ul");
foreach ( $allComments as $commentData ) {
$author = $commentData->get("com_author");
$comment = $commentData->get("com_text");
$date = $commentData->get("com_date");
$ul->append("<div id='comment_user'><p id=AuthorComment> $author</p><p id='comment_text'> $comment</p><p id='dateComment'> $date</p></div>");
}
return $ul;
}
function getComments( Database $db, $article ) {
$sql = "SELECT com_id, com_author, com_text, DATE_FORMAT (com_date,'%d %b %Y %H:%i') AS com_date FROM comments
WHERE com_art = '$article' ORDER BY com_id DESC";
return $db->getData($sql);
}
function showForm( $article ) {
$action = "index.php?page=blog&show-article=$article&com_art=$article";
//$out = "<div id='guestbookForm'>";
$form = html("form")->attr("id", "comment-form");
$form->attr("action", $action)->attr("method", "post");
$hidden = emptyTag("input")->attr("type", "hidden");
$hidden->attr("name", "id")->attr("value", "$article");
$form->append($hidden);
$form->append(
'<div class="commentForm">
<h1>Comment the article</h1>
<label class="com_author">Author:</label>
<input type="text" name="author" placeholder="author" class="com-author" />
<textarea class="com_txt" name="new-comment" placeholder="Your comment here" ></textarea>
<input type="submit" value="Comment" class="post_B"/>
</div>'
);
return $form;
}
function insertComment( Database $db, Request $formData ) {
$id = $formData->get( "com_id" );
$author = $formData->get( "com_author" );
$comment = $formData->get( "com_text" );
$sql = "INSERT INTO comments (com_author, com_text, com_art)
VALUES ( '$author', '$comment', '$id' )";
$db->query( $sql );
}