Jump to content

Not adding to the database and doing what I expect :/


LooieENG

Recommended Posts

<?php

require('../config.php');

if (!$_SESSION['id']) {
header('location: ../login.php');
exit;
} else {
$result = mysql_query("SELECT class, username FROM users WHERE id = '$_SESSION[id]'");
$row = mysql_fetch_array($result);
if ($row['class'] != 1) {
	exit('You don\'t have permission to access this page.');
} else {
	if (!$_GET['cat']) {

?>

<h3>Select a Forum Category</h3>

<?php

		$result = mysql_query("SELECT id, title FROM categories ORDER BY id ASC");
		$queries++;
		for ($i = 0; $i < mysql_num_rows($result); $i++) {
			$row = mysql_fetch_array($result);
			echo '<a href="forums.php?cat=' . $row['id'] . '">' . $row['title'] . '</a><br />';
		}
		exit('<p>Page generated in ' . (microtime() - $stime) . ' seconds with ' . $queries . ' MySQL ' . ($queries == 1 ? 'Query' : 'Queries') . '</p>');
	} else {
		if ($_POST['forum'] && $_POST['desc'] && $_POST['submit']) {
			$forum = htmlentities($_POST['forum'], ENT_QUOTES);
			$desc = htmlentities($_POST['desc'], ENT_QUOTES);
			mysql_query("INSERT INTO forums (title, description) VALUES ('$forum', '$desc')");
			$queries++;
			echo '<p>Done.</p><hr />';
		}

?>

Welcome, <?php echo $row['username'] ?>.

<h3>Add a Forum</h3>

<p><form name="forum" method="post" action="forums.php">
<input type="text" name="forum" /><br />
<input type="text" name="desc" /><br />
<input name="submit" type="submit" value="Add Forum" />
</form></p>

<p>Page generated in <?php echo microtime() - $stime ?> seconds with <?php echo $queries . ' MySQL ' . ($queries == 1 ? 'Query' : 'Queries') ?></p>

<?php

	}
}
}

?>

 

It should add it to the database, then say

 

_______________________________________

 

Done.

 

(horizontal line)

<h3>Add a Forum</h3>

 

<p><form name="forum" method="post" action="forums.php">

<input type="text" name="forum" /><br />

<input type="text" name="desc" /><br />

<input name="submit" type="submit" value="Add Forum" />

</form></p>

 

<p>Page generated in <?php echo microtime() - $stime ?> seconds with <?php echo $queries . ' MySQL ' . ($queries == 1 ? 'Query' : 'Queries') ?></p>

 

_______________________________________

 

Thanks.

 

Edit: Here's what happens for adding categories (and should happen for this too), and here's what happens with forums.php (this page)

 

http://files.ehwtf.com/php.mp4

Take a look at your first query:

 

<?php

"SELECT class, username FROM users WHERE id = '$_SESSION[id]'"

?>

 

I have had the experience in which the script doesn't work when you pass a $_SESSION, $_GET, or $_POST variable directly into the query.  Assign the $_SESSION value to a variable:

 

<?php

$id = $_SESSION['id']

$result = mysql_query("SELECT class, username FROM users WHERE id = '$id'");

?>

Vermillion, that's because you probably weren't doing it right. >_<  Correct array variable interpolation goes something like:

 

<?php
$array['test'] = "nothing";
echo "This example is anything but {$array['test']}!";
?>

 

That's how it "should" be done.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.