| View previous topic :: View next topic |
| Author |
Message |
deleted user 111213 Grandmaster Cheater
Reputation: 0
Joined: 09 Nov 2007 Posts: 714
|
Posted: Tue Dec 02, 2008 10:14 am Post subject: problem with create.php |
|
|
| Code: | <?php
$id = $_GET['id'];
if($id){
$sql = "SELECT * FROM forum_sub_cats WHERE id='".$id."'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0){
echo "The forum you are trying to create a topic on doesn't exist!\n";
}else{
$row1 = mysql_fetch_assoc($res);
if($row1['admin'] == 1 && $admin_user_level == 0){
echo "You are not an admin, you ,cannot post here!\n";
}else{
echo "<table border='0' cellspacing='3' cellpadding='3'>\n";
echo "<form method='post' action='forum-index.php?act=create'>\n";
echo "<tr><td>Forum Sub Category</td><td><select name='cat'>\n";
$sql2 = "SELECT * FROM forum_cats WHERE admin < ".$admin_user_level."+1";
$res2 = mysql_query($sql2) or die mysql_error());
while ($row = mysql_fetch_assoc($res2)){
$sql3 = "SELECT * FROM forum_sub_cats WHERE id='".$row['id']."'";
$res3 = mysql_query($sql3) or die(mysql_error());
echo "<option value='0'>".$row['name']."</option>\n";
while ($row2 = mysql_fetch_assoc($res3)){
$selected = ($row2['id'] == $id) ? " SELECTED " : "";
echo "<option value=\"".$row2['id']."\"".$selected."> ".$row2['name']."</option>\n";
}
}
echo "</select></td></tr>\n";
}
}
}
?> |
basically i am trying to get a drop down menu but it won't appear.
|
|
| Back to top |
|
 |
sloppy Expert Cheater
Reputation: 0
Joined: 17 Aug 2008 Posts: 123
|
Posted: Tue Dec 02, 2008 7:09 pm Post subject: |
|
|
Would it kill you to use some spacing and indentation?.. You can't use single quotes in your html tags, and you are missing the closing tags for your table & form.
| Code: | <?php
$id = $_GET['id'];
if ($id)
{
$sql = "SELECT * FROM forum_sub_cats WHERE id='".$id."'";
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res) == 0)
{
echo "The forum you are trying to create a topic on doesn't exist!\n";
}
else
{
$row1 = mysql_fetch_assoc($res);
if ($row1['admin'] == 1 && $admin_user_level == 0)
{
echo "You are not an admin, you ,cannot post here!\n";
}
else
{
echo "<form method=\"post\" action=\"forum-index.php?act=create\">\n";
echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
echo "<tr><td>Forum Sub Category</td><td><select name=\"cat\">\n";
$sql2 = "SELECT * FROM forum_cats WHERE admin < ".$admin_user_level."+1";
$res2 = mysql_query($sql2) or die mysql_error());
while ($row = mysql_fetch_assoc($res2))
{
$sql3 = "SELECT * FROM forum_sub_cats WHERE id='".$row['id']."'";
$res3 = mysql_query($sql3) or die(mysql_error());
echo "<option value=\"0\">$row[name]</option>\n";
while ($row2 = mysql_fetch_assoc($res3))
{
$selected = ($row2['id'] == $id) ? " selected" : "";
echo "<option value=\"$row2[id]\"$selected>$row2[name]</option>\n";
}
}
echo "</select></td></tr>\n";
echo "</table></form>\n";
}
}
}
?> |
|
|
| Back to top |
|
 |
deleted user 111213 Grandmaster Cheater
Reputation: 0
Joined: 09 Nov 2007 Posts: 714
|
Posted: Wed Dec 03, 2008 10:08 am Post subject: |
|
|
| still didn't work! i have no idea what is wrong!
|
|
| Back to top |
|
 |
ElectroFusion Grandmaster Cheater
Reputation: 0
Joined: 17 Dec 2006 Posts: 786
|
Posted: Wed Dec 03, 2008 10:33 am Post subject: |
|
|
I think you should try some books first.
Get PHP & MYSQL novice to professional
I got it last year, awesome book.
_________________
| qwerty147 wrote: |
| ghostonline wrote: |
what world are you in?
|
bera
but i live in NZ
|
|
|
| Back to top |
|
 |
Reak I post too much
Reputation: 0
Joined: 15 May 2007 Posts: 3496
|
Posted: Wed Dec 03, 2008 10:38 am Post subject: |
|
|
sloppy's code should work.
The error is probably in your database-querying routine code.
Set:
| Code: | | ERROR_REPORTING(E_ALL); |
at the second line of your code (after <?php) and see if it tells you something.
|
|
| Back to top |
|
 |
deleted user 111213 Grandmaster Cheater
Reputation: 0
Joined: 09 Nov 2007 Posts: 714
|
Posted: Wed Dec 03, 2008 11:17 am Post subject: |
|
|
| tells me nothing. should i post my SQL? from the DB?
|
|
| Back to top |
|
 |
ElectroFusion Grandmaster Cheater
Reputation: 0
Joined: 17 Dec 2006 Posts: 786
|
Posted: Wed Dec 03, 2008 12:49 pm Post subject: |
|
|
| Code: | | $row1 = mysql_fetch_assoc($res); | should be in a while event.
| Code: | | while($row1 == mysql_fetch_assoc($res)){ |
_________________
| qwerty147 wrote: |
| ghostonline wrote: |
what world are you in?
|
bera
but i live in NZ
|
|
|
| Back to top |
|
 |
deleted user 111213 Grandmaster Cheater
Reputation: 0
Joined: 09 Nov 2007 Posts: 714
|
Posted: Thu Dec 04, 2008 10:36 am Post subject: |
|
|
| that still didn't work.
|
|
| Back to top |
|
 |
Reak I post too much
Reputation: 0
Joined: 15 May 2007 Posts: 3496
|
Posted: Thu Dec 04, 2008 11:04 am Post subject: |
|
|
| ElectroFusion wrote: | | Code: | | $row1 = mysql_fetch_assoc($res); | should be in a while event.
| Code: | | while($row1 == mysql_fetch_assoc($res)){ |
|
Only if there are more than one results.
|
|
| Back to top |
|
 |
deleted user 111213 Grandmaster Cheater
Reputation: 0
Joined: 09 Nov 2007 Posts: 714
|
Posted: Thu Dec 04, 2008 11:14 am Post subject: |
|
|
| is there anyway i could rewrite the code so it does work?
|
|
| Back to top |
|
 |
Reak I post too much
Reputation: 0
Joined: 15 May 2007 Posts: 3496
|
Posted: Thu Dec 04, 2008 12:55 pm Post subject: |
|
|
Tell us what it is doing. Saying "it doesn't work" doesn't really tell us much.
You could also put some echo()'s into certain code sections to see if it passes through certain checks like if(mysql_num_rows($..) == ... or not.
You need to learn how to fix errors by yourself.
|
|
| Back to top |
|
 |
deleted user 111213 Grandmaster Cheater
Reputation: 0
Joined: 09 Nov 2007 Posts: 714
|
Posted: Thu Dec 04, 2008 1:16 pm Post subject: |
|
|
| the thing is i don't have any experiance of fixing errors. i have fixed some by myself before but i don't see anything wrong with my code for a start off.
|
|
| Back to top |
|
 |
sloppy Expert Cheater
Reputation: 0
Joined: 17 Aug 2008 Posts: 123
|
Posted: Thu Dec 04, 2008 2:12 pm Post subject: |
|
|
As Reak mentioned, scatter some echos throughout to test the path of your code, echo your variables & sql results to see if they are as they should be. If you have a blank page and can't view errors, comment everything out then add it back in line by line, trial and error.
Use this function to check the results of your sql queries,
| Code: | function SQLResultsTable($res)
{
$html = "<table border=\"1\" cellpadding=\"3\" style=\"border-collapse: collapse;\">\n";
$html.= "<tr style=\"background-color: firebrick; color: white;\">\n";
$fNum = mysql_num_fields($res);
for ($i=0; $i < $fNum; $i++)
{
$html.= "<th>" . mysql_field_name($res, $i) . "</th>";
}
$html.= "</tr>\n";
$rNum = 0;
while ($row = mysql_fetch_assoc($res))
{
$html.= ($rNum++ % 2 == 0)
? "<tr style=\"background-color: whitesmoke;\">\n"
: "<tr style=\"background-color: white;\">\n";
foreach($row as $field => $value)
{
$html.= "<td>$value</td>";
}
$html.= "</tr>\n";
}
return $html.= "</table>\n";
} |
| Code: | $sql = "SELECT * FROM my_table WHERE id = $id";
$res = mysql_query($sql) or die(mysql_error());
echo SQLResultsTable($res);
|
|
|
| Back to top |
|
 |
deleted user 111213 Grandmaster Cheater
Reputation: 0
Joined: 09 Nov 2007 Posts: 714
|
Posted: Thu Dec 04, 2008 2:20 pm Post subject: |
|
|
ok so this is my code now
| Code: | <?php
$id = mss($_GET['id']);
if ($id){
$sql = "SELECT * FROM `forum_sub_cats` WHERE `id`='" . $id . "'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0){
echo "The forum you are trying to create a topic on does not exist!\n";
}else{
$row1 = mysql_fetch_assoc($res);
if($row1['admin'] == 1 && $admin_user_level == 0)}{
echo "You are not an administrator, you have no right to post on this forum!\n";
}else{
if(!$_POST['submit']){
echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
echo "<forum method=\"post\" action=\"./forum-index.php?act=create\">\n";
echo "<tr><td>Forum Sub Category</td<td><select name=\"cat\">\n";
$sql2 = "SELECT * FROM `forum_cats` WHERE `admin` < " . $admin_user_level . "+1";
$res2 = mysql_query($sql2) or die(mysql_error());
while ($row = mysql_fetch_assoc($res2)){
$sql3 = "SELECT * FROM `forum_sub_cats` WHERE `cid`='" . $row['id'] . "'";
$res3 = mysql_query($sql3) or die(mysql_error());
echo "<option value=\"0\">" . $row['name'] . "</option>\n";
while($row2 = mysql_fetch_assoc($res3)){
$selected = ($row2['id'] == $id) ? " SELECTED" : "";
echo "<option value=\"" . $row2['id'] . "\"" . $selected . "> " . $row2['name'] . "</option>\n";
}
}
echo "</select></td></tr>\n";
echo "<tr><td>Topic Title</td><td><input type=\"text\" name=\"title\"></td></tr>\n";
echo "<tr><td>Message</td><td><textarea name=\"message\" style=\"width:300px;height:100px;\"></textarea></td></tr>\n";
echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Create Topic\"></td></tr>\n";
echo "</form></table>\n";
}
}
}
}
?> |
anyone see anything wrong with it?
|
|
| Back to top |
|
 |
~NJ Grandmaster Cheater Supreme
Reputation: 0
Joined: 09 Mar 2007 Posts: 1417 Location: sitting outside of the forum
|
Posted: Fri Dec 05, 2008 1:37 am Post subject: |
|
|
Try:
| Code: | <?php
$id = $_GET['id'];
if(isset($id)){
$sql = "SELECT * FROM `forum_sub_cats` WHERE `id`='$id'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0){
echo "The forum you are trying to create a topic on does not exist!\n";
}else{
$row1 = mysql_fetch_assoc($res);
if($row1['admin'] == 1 && $admin_user_level == 0){
echo "You are not an administrator, you have no right to post on this forum!\n";
}else{
if(!$_POST['submit']){
echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
echo "<forum method=\"post\" action=\"./forum-index.php?act=create\">\n";
echo "<tr><td>Forum Sub Category</td<td><select name=\"cat\">\n";
$sql2 = "SELECT * FROM `forum_cats` WHERE `admin` < " . $admin_user_level . "+1";
$res2 = mysql_query($sql2) or die(mysql_error());
while ($row = mysql_fetch_assoc($res2)){
$rowid = $row['id'];
$sql3 = "SELECT * FROM `forum_sub_cats` WHERE `cid`='$rowid'";
$res3 = mysql_query($sql3) or die(mysql_error());
echo "<option value=\"0\">" . $row['name'] . "</option>\n";
while($row2 = mysql_fetch_assoc($res3)){
if($row2['id'] == $id){
$selected = " SELECTED";
}
echo "<option value=\"" . $row2['id'] . "\"" . $selected . "> " . $row2['name'] . "</option>\n";
}
}
echo "</select></td></tr>\n";
echo "<tr><td>Topic Title</td><td><input type=\"text\" name=\"title\"></td></tr>\n";
echo "<tr><td>Message</td><td><textarea name=\"message\" style=\"width:300px;height:100px;\"></textarea></td></tr>\n";
echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Create Topic\"></td></tr>\n";
echo "</form></table>\n";
}
}
}
}
?> |
Changes:
-Used isset to test if the $_GET variable is set
-Removed the concatenation you had inside your SQL queries
-You had some mixed up braces with the shitload of if else statements you got happening ;P
-Replaced the inline if else statement with the proper syntax
Future suggestions:
-Look into MySQL joins to lessen the amount of mysql queries you have. You current code wouldn't be appropriate for a production site
-You can use single quotes when setting strings containing HTML so you don't have to backslash every single double quotes.
Eg: $string = '<table width="100">'; rather than $string = "<table width=\"100\">';
-Sanitize your MySQL queries. Especially considering you taking input from a _GET variable...
-Optimize your MySQL queries. EG: Only select the fields you require rather than *, use LIMIT and ORDER BY to further reduce server load.
_________________
Last edited by ~NJ on Fri Dec 05, 2008 1:44 am; edited 1 time in total |
|
| Back to top |
|
 |
|