Jump to content


Photo

Reports sucess of query, but nothing is inserted into database


  • Please log in to reply
3 replies to this topic

#1 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 15 July 2006 - 05:24 PM

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")...
Posted Image

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.)

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

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 July 2006 - 06:05 PM

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:

$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);

Legend has it that reading the manual never killed anyone.
My site

#3 Drumminxx

Drumminxx
  • Members
  • PipPipPip
  • Advanced Member
  • 92 posts
  • LocationUSA

Posted 15 July 2006 - 06:13 PM

INSERT INTO posts (forum_id, thread_id, subject, body, time, special) VALUES ('$forum', '$thread_id', '$subject', '$body', NOW(), '$special')";

change the field "time" to "time_added"
:: gucci.com ::
:: blockbuster.com ::

--------------------

#4 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 15 July 2006 - 06:17 PM

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:

$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);


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

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users