Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


problem with create.php
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
deleted user 111213
Grandmaster Cheater
Reputation: 0

Joined: 09 Nov 2007
Posts: 714

PostPosted: Tue Dec 02, 2008 10:14 am    Post subject: problem with create.php Reply with quote

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
View user's profile Send private message
sloppy
Expert Cheater
Reputation: 0

Joined: 17 Aug 2008
Posts: 123

PostPosted: Tue Dec 02, 2008 7:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
deleted user 111213
Grandmaster Cheater
Reputation: 0

Joined: 09 Nov 2007
Posts: 714

PostPosted: Wed Dec 03, 2008 10:08 am    Post subject: Reply with quote

still didn't work! i have no idea what is wrong!
Back to top
View user's profile Send private message
ElectroFusion
Grandmaster Cheater
Reputation: 0

Joined: 17 Dec 2006
Posts: 786

PostPosted: Wed Dec 03, 2008 10:33 am    Post subject: Reply with quote

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
View user's profile Send private message
Reak
I post too much
Reputation: 0

Joined: 15 May 2007
Posts: 3496

PostPosted: Wed Dec 03, 2008 10:38 am    Post subject: Reply with quote

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
View user's profile Send private message
deleted user 111213
Grandmaster Cheater
Reputation: 0

Joined: 09 Nov 2007
Posts: 714

PostPosted: Wed Dec 03, 2008 11:17 am    Post subject: Reply with quote

tells me nothing. should i post my SQL? from the DB?
Back to top
View user's profile Send private message
ElectroFusion
Grandmaster Cheater
Reputation: 0

Joined: 17 Dec 2006
Posts: 786

PostPosted: Wed Dec 03, 2008 12:49 pm    Post subject: Reply with quote

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
View user's profile Send private message
deleted user 111213
Grandmaster Cheater
Reputation: 0

Joined: 09 Nov 2007
Posts: 714

PostPosted: Thu Dec 04, 2008 10:36 am    Post subject: Reply with quote

that still didn't work.
Back to top
View user's profile Send private message
Reak
I post too much
Reputation: 0

Joined: 15 May 2007
Posts: 3496

PostPosted: Thu Dec 04, 2008 11:04 am    Post subject: Reply with quote

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
View user's profile Send private message
deleted user 111213
Grandmaster Cheater
Reputation: 0

Joined: 09 Nov 2007
Posts: 714

PostPosted: Thu Dec 04, 2008 11:14 am    Post subject: Reply with quote

is there anyway i could rewrite the code so it does work?
Back to top
View user's profile Send private message
Reak
I post too much
Reputation: 0

Joined: 15 May 2007
Posts: 3496

PostPosted: Thu Dec 04, 2008 12:55 pm    Post subject: Reply with quote

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
View user's profile Send private message
deleted user 111213
Grandmaster Cheater
Reputation: 0

Joined: 09 Nov 2007
Posts: 714

PostPosted: Thu Dec 04, 2008 1:16 pm    Post subject: Reply with quote

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
View user's profile Send private message
sloppy
Expert Cheater
Reputation: 0

Joined: 17 Aug 2008
Posts: 123

PostPosted: Thu Dec 04, 2008 2:12 pm    Post subject: Reply with quote

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
View user's profile Send private message
deleted user 111213
Grandmaster Cheater
Reputation: 0

Joined: 09 Nov 2007
Posts: 714

PostPosted: Thu Dec 04, 2008 2:20 pm    Post subject: Reply with quote

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
View user's profile Send private message
~NJ
Grandmaster Cheater Supreme
Ban
Reputation: 0

Joined: 09 Mar 2007
Posts: 1417
Location: sitting outside of the forum

PostPosted: Fri Dec 05, 2008 1:37 am    Post subject: Reply with quote

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.

_________________
I failed to properly read http://forum.cheatengine.org/viewtopic.php?t=247716 last time I updated my trainer


Last edited by ~NJ on Fri Dec 05, 2008 1:44 am; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites