Jump to content

Reports sucess of query, but nothing is inserted into database


pixy

Recommended Posts

Okay, so I'm writing my own, simple forum script. It tells me the query was sucessful, BUT nothing is inserted into the database...

Here's a screenshot of my posts table.
(if there is an easier was to do this kind of thing, PLEASE tell me...I'm a bit "in the dark")...
[img]http://img.photobucket.com/albums/v72/vanillachick/posts_table.gif[/img]

And this is my PHP code for new_thread.php...
(just FYI, lifeonmars is the admin username I'm using for testing the site. When it goes live i'll make a session for depicting the rank of the member.)

[code]
<?php

// Copyright "the Wizarding World" 2006-2007
// Date: July 14th, 2006
// Description: Create a new thread

session_start();
$user = $_SESSION['user'];

$page_title = 'Create a new thread';

// Includes header + database variables + Stuff
require_once('config.inc.php');
require_once('connect.php');
include('functions.php');
include('header.php');

if (isset($_GET['forum_id'])) {
if (is_numeric($_GET['forum_id'])) {
$forum = $_GET['forum_id'];
}
else {
echo 'Your forum ID is invalid.';
include("footer.php");
die();
}
}

if (isset($_POST['submitted'])) {
$errors = array();
if (empty($_POST['subject'])) {
$errors[] = 'You did not choose a subject for your topic.';
}
else {
$subject = escape_data(htmlspecialchars($_POST['subject']));
}
if (empty($_POST['body'])) {
$errors[] = 'You did not enter anything in the body of your topic.';
}
else {
$body = escape_data(htmlspecialchars($_POST['body']));
}
if (empty($_POST['forum'])) {
$errors[] = 'Forum ID was incorrect.';
}
else {
$forum = $_POST['forum'];
}
if (empty($_POST['special'])) {
$errors = 'You did not select the type of post to make.';
}
elseif ($_POST['special'] == 1) {
$special = 1;
}
elseif ($_POST['special'] == 2) {
$special = 2;
}
elseif ($_POST['special'] == 3) {
$special = 3;
}
elseif ($_POST['special'] == 4) {
$special = 4;
}
else {
$errors[] = 'You did not select a valid type.';
}
if (empty($errors)) {
// First, find out what the thread_id should be
$query = "SELECT ABS(thread_id) FROM posts";
$result = mysql_query($query);
if ($result) {
$row = mysql_fetch_array($result, MYSQL_NUM);
$thread_id = $row[0] + 1;
$query = "INSERT INTO posts (forum_id, thread_id, subject, body, time, special) VALUES ('$forum', '$thread_id', '$subject', '$body', NOW(), '$special')";
$result = mysql_query($query);
if ($query) {
echo '<div align="center">Your post has been created! You are being redirected to your thread.</div>';
redirect("view_thread.php?id=$thread_id", 2);
}
else {
echo mysql_error();
}
}
else {
echo mysql_error();
}
}
else {
foreach ($errors as $msg) {
echo '<Li> '.$msg.'</li>';
}
}
}
else {
echo '<form action="'.$file.'" method="post">
<table border="0" class="tablestyledark" align="center" width="70%" cellpadding="7px" cellspacing="7px">
<tr><td class="tablestylelight"><b>In Forum:</b></td><td class="tablestylelight">';
if (isset($_GET['forum_id'])) {
$query = "SELECT name FROM forums WHERE forum_id='$forum'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 1) {
$row = mysql_fetch_array($result, MYSQL_NUM);
echo '<i>'.$row[0].'</i>';
}
else {
die('Could not select forum.</td></tr></table><p><center><i>Since we could not select the form, you cannot make a post in it</center>');
}
}
else {
die('Could not select forum.</td></tr></table><p><center><i>Since we could not select the form, you cannot make a post in it</center>');
}
echo '<tr>
<td class="tablestylelight"><b>Subject:</b></td><td class="tablestylelight"><input type="text" name="subject" value="';
if (isset($_POST['subject'])) {
echo stripslashes($_POST['subject']);
}
echo '" size="51"></td></tr>
</td></tr>
<tr><Td colspan="2" class="tablestylelight"><b>Post Body:</b><br>
<textarea name="body" rows="10" cols="63">';
if (isset($_POST['body'])) {
echo stripslashes($_POST['body']);
}
echo '</textarea></td></tr>
<tr><td class="tablestylelight" colspan="2"><input type="radio" name="special" value="1" checked> Regular Post <br>';
if ($user == 'lifeonmars') {
echo '<input type="radio" name="special" value="2"> Sticky Post<br>
<input type="radio" name="special" value="3"> Lock Post<br>
<input type="radio" name="special" value="4"> Stickied and Locked Post<br>';
}
echo '</td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Submit Post"></td></tr></table>
<input type="hidden" name="forum" value="'.$forum.'">
<input type="hidden" name="submitted" value="TRUE"></form>';
}

mysql_close();
include("footer.php");
?>
[/code]
Link to comment
Share on other sites

if ($query) will always be true since you just defined $query.  I suspect you want to change that to if ($result).

What's useful for debugging is to change $result = mysql_query($query); to:

[code]$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);[/code]
Link to comment
Share on other sites

[quote author=AndyB link=topic=100692.msg397800#msg397800 date=1152986709]
if ($query) will always be true since you just defined $query.  I suspect you want to change that to if ($result).

What's useful for debugging is to change $result = mysql_query($query); to:

[code]$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);[/code]
[/quote]

Oops, that was totally a typo. XD Thanks so much for pointing it out.
And thanks to Drumminxx, because I've gotta fix that too--it would spit out an error at me if I didn't change it.

EDIT:
It works now. Thanks so much!<33
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.