Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andrew McCombs
4,309 PointsCode issue - I dont know where I have messed up or what I have done wrong.
Lets start with the first issue, my sql query for inserting information into the database. For some reason it is only inserting some of the information into the data base.
This is what is in the database
id = 3
post_author = Its blank
post_author_id = 0 <- not the right id
otid = 0 <- this should be 3
date_time = 2014-09-07 21:59:57
type = a
view_count = 0 which is 0
section_title = Its blank
section_id = 0 <- should be 2
thread_title = asdfasdfasdffsda
post_body = afsdfdasdfasfdasdfsafdsasdf
closed = 0
So what I did was looked at the headers in the network tab to see what ajax was sending to my php.
post_author=Andrew&member_id=1&post_type=a&post_body=fdsdfsasdfadfdfasdfasfdaasdf&forum_section_id=2&forum_section_title=Welcome&post_title=dfasfasfdfdsfsdasfad
Which has the correct information. I dont know what the insert is only inserting certain items. Also you will see a line that also is updating the OTID (original topic id), but its not updating the db.
Issue 2:
Even-tho the query are not running correctly or I comment out the query to see if php is going to send the "post_success" back to my ajax, it doesn't. I am not that knowledgeable about ajax and it function so I problem made a mistake.
I hope that you guys can see what I did wrong. Im trying to make my own website, and using the skills that I learned from Tree House.
FYI - The get is used to pull the section id and the section title of where the new post is going to go.
<?php
include 'core/init.php';
include 'includes/overall/header.php';
if (isset($_GET['id']) && isset($_GET['title'])) {
$section_id = $_GET['id'];
$section_title = $_GET['title'];
} else {
echo 'this';
}
if(isset($_POST["post_type"])) {
$post_type = $_POST['post_type'];
$post_body = $_POST['post_body'];
$post_body = nl2br(htmlspecialchars($post_body));
$forum_section_id = (int)$_POST['fsID'];
$forum_section_title = htmlspecialchars($_POST['fsTitle']);
$member_id = (int)$_POST['uid'];
$post_author = $_POST['username'];
$post_title = preg_replace('#[^A-Za-z0-9 ?!.,]#i', '',$_POST['post_title']);
echo "heellloo";
exit();
if ($post_type == "a") {
if ($post_title == "") {
$errors[] = 'Your Title is missing';
}
if (strlen($post_title) < 10) {
$errors[] = 'The Title needs to be at least 10 characters';
}
if (strlen($post_body) < 2) {
$errors[] = 'Your body needs to be at lease 2 characters';
}
$sql = "INSERT INTO forum_post (post_author, post_author_id, date_time, type, section_title, section_id, thread_id, thread_title, post_body) VALUES ('$post_author', '$member_id', 'now()', 'a', 'forum_section_title', '$forum_section_id', '$post_title', $post_body')";
$query = mysqli_query($mysqli, $sql);
$this_id = mysqli_insert_id($mysqli);
$sql = "UPDATE forum_post SET otid = '$this_id' WHERE id = '$this_id'";
$query = mysqli_query($mysqli, $sql);
echo "post_success";
exit();
}
}
?>
<script type="text/javascript">
function emptyElement(x){
_(x).innerHTML = "";
}
function newPost(){
var post_author = _("username").value;
var member_id = _("uid").value;
var post_type = _("post_type").value;
var post_body = _("post_body").value;
var forum_section_id = _("fsID").value;
var forum_section_title = _("fsTitle").value;
var post_title = _("post_title").value;
var status = _("status");
if(post_author == "" || member_id == "" || post_type == "" || post_body == "" || forum_section_id == "" || forum_section_title == "" || post_title == ""){
status.innerHTML = "Please fields are required";
} else if(post_title.length < 10){
status.innerHTML = "Your title must be 10 characters";
} else {
_("createbtn").style.display = "none";
status.innerHTML = 'please wait ...';
var ajax = ajaxObj("POST", "new_topic.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
if(ajax.responseText == "post_success") {
status.innerHTML = ajax.responseText;
_("creatbtn").style.display = "block";
window.location = 'view_thread.php?id=' . $this_id;
} else {
}
}
}
ajax.send("post_author="+post_author+"&member_id="+member_id+"&post_type="+post_type+"&post_body="+post_body+"&forum_section_id="+forum_section_id+"&forum_section_title="+forum_section_title+"&post_title="+post_title);
}
}
</script>
<div class="container">
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">
<h2><?php echo $section_title; ?>
</h2>
</div>
<div class="panel-body">
<div id="status"></div>
<ol class="breadcrumb">
<li><a href="index.php">Home</a></li>
<li><a href="forums.php">Forum Home</a></li>
<li><a href="section.php?id=<?php echo $section_id; ?>"><?php echo $section_title; ?></a></li>
</ol>
<form id="postTread" onsubmit="return false" class="form-horizontal" role="form">
<div class="form-group">
<label for="topic_author" class="col-sm-2 control-label">Author</label>
<div class="col-sm-10">
<input type="text" onfocus="emptyElement('status')" name="topic_author" class="form-control" id="topic_author" value="<?php echo $log_username; ?>" autocomplete="off" placeholder="" disabled>
</div>
</div>
<div class="form-group">
<label for="post_title" class="col-sm-2 control-label">Thread Title</label>
<div class="col-sm-10">
<input type="text" onfocus="emptyElement('status')" name="post_title" class="form-control" id="post_title" value="" autocomplete="off" placeholder="Please type in the title for your thread">
</div>
</div>
<div class="form-group">
<label for="post_body" class="col-sm-2 control-label">Thread Body</label>
<div class="col-sm-10">
<textarea type="text" onfocus="emptyElement('status')" name="post_body" class="form-control" id="post_body" value="" autocomplete="off" placeholder="Please type in your body for your thread" rows="15"></textarea>
</div>
</div>
<input id="fsID" type="hidden" name="fsID" value="<?php echo $section_id ?>">
<input id="fsTitle" type="hidden" name="fsTitle" value="<?php echo $section_title; ?>">
<input id="uid" type="hidden" name="uid" value="<?php echo $_SESSION['userid']; ?>">
<input id="username" type="hidden" name="username" value="<?php echo $log_username; ?>">
<input id="post_type" type="hidden" name="post_type" value="a">
<button id="createbtn" onclick="newPost()" class="btn btn-primary col-sm-6">Create Thread </button>
</form>
</div>
</div>
</div>
<?php
include 'includes/overall/footer.php';
These are the functions that are being used but the ajax
function ajaxObj( meth, url ) {
var x = new XMLHttpRequest();
x.open( meth, url, true );
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x){
if(x.readyState == 4 && x.status == 200){
return true;
}
}
3 Answers
Shawn Flanigan
Courses Plus Student 15,815 PointsAndrew,
I didn't go through all of your code, so there may be other problems, but I noticed there are a few errors in your SQL query in your if ($post_type == 'a') block.
You have:
$sql = "INSERT INTO forum_post (post_author, post_author_id, date_time, type, section_title, section_id, thread_id, thread_title, post_body) VALUES ('$post_author', '$member_id', 'now()', 'a', 'forum_section_title', '$forum_section_id', '$post_title', $post_body')";
It looks like you're missing a $ for your forum_section_title variable. You also have no value corresponding to your thread_id which will throw off the rest of the query.
As for the OTID...I'm not sure that this is what you want, unless this is only for new topics:
$sql = "UPDATE forum_post SET otid = '$this_id' WHERE id = '$this_id'";
I didn't study your code too closely, but with this syntax it looks to me like you're just updating the OTID of the original post instead of the current post. I would think you should use something like this instead:
$sql = "UPDATE forum_post SET otid = '$this_id' WHERE id = '$current_post_id";
Hope some of this helps.
Philip Cox
14,818 PointsJust some advice.
Start simple and get it working. Create a test database with a single col table for say a name. Create a form with a single input for the name. Create the PHP and jQuery ajax to see it work. Then add on to it. One of the hardest things I find while learning is writing out a bunch of code and trying to solve many bugs at once. Strip it back to a simple solution and get it working and add on it.
Shawn Flanigan
Courses Plus Student 15,815 PointsGot it. And the other issues I pointed out?
Andrew McCombs
4,309 PointsThank you for that missing var. $......
I fixed the whole sql issue, it was because I didnt use the same var name as I did in the php, So I changed the var and it worked.
do you know anything about ajax.
Andrew McCombs
4,309 PointsAndrew McCombs
4,309 PointsWith the otid as you can see that $this_id is pulling the id of the inserted row. Now what would you call from the db to get that I'd so I could update the otid to match that. The otid is there so when reply come in to play it can ref that is to match the original topic.
The otid needs to match the same id that was created for the topic.