Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: PHP classes for a newbie like me... Help plss T_T

  1. #1
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Question PHP classes for a newbie like me... Help plss T_T

    I'm having a problem in handling classes using php. Ok, this is my first time handling classes in php, so I made a basic php code.

    Database name: try
    Table name: example

    Here's my 3 files:

    insertUser.php

    Code:
    <?php
      
    if(isset($_GET['submit']))
    {
      include "dbconnect.php";
      include "execute.class.php";
      
      $execute = new try();
        
      $username = $_GET['user'];
      $password = $_GET['password'];
      
      $sqlstring = "insert into example values('$username','$password')";
      $execute->execute_query($sqlstring);
    }
    
    ?>
    
    <html>
    <head>
      <title> php class </title>
    </head>
    
    <body>   
    
      <form method="get" action="">
    
          <label for= "userid"> Username</label>
               <input type= "text"  name="user" id="userid" />
    
          <label for= "passid"> Password</label>
               <input type= "password" name= "password" id="passid"/>
    
          <input type="submit" name="submit" value="Submit" />
    
      </form>
    
    </body>
    
    </html>

    dbconnect.php

    Code:
    <?php
      $con = mysql_connect("localhost","root","") or die(mysql_error());
      mysql_select_db("try",$con) or die(mysql_error());  
    ?>

    execute.class.php

    Code:
    <?php
    
     class try
     {
        function execute_query($query)
        {
    	   mysql_query($query);
        }
     }
     
    ?>
    My problem was, when I executed this, I've got a blank page... I have tried putting include "dbconnect.php" in the execute.class.php but it's of no use. I tried putting the include "execute.class.php" and the creation of object outside the if statement, still, failed... what went wrong? And if you could kindly explain the rules, constraints, the do's, and dont's in using classes in php, it would be more appreciated...

  2. #2
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Smile

    Changed try to mtry because of try/catch/throw are words used in php, should not be used as class name.

    Added constructor method used in classes, you may go without this also.

    Please read about the PHP classes in wiki and in php mannual also.

    Now all should work fine as tested.

    A. insertuser.php

    <?php

    if(isset($_GET['submit']))
    {
    include "dbconnect.php";
    include "execute.class.php";

    $execute = new mtry();

    $username = $_GET['user'];
    $password = $_GET['password'];

    $sqlstring = "insert into example(username,password) values('$username','$password')";
    $execute->execute_query($sqlstring);
    }

    ?>

    <html>
    <head>
    <title> php class </title>
    </head>

    <body>

    <form method="get" action="">

    <label for= "userid"> Username</label>
    <input type= "text" name="user" id="userid" />

    <label for= "passid"> Password</label>
    <input type= "password" name= "password" id="passid"/>

    <input type="submit" name="submit" value="Submit" />

    </form>

    </body>

    </html>

    B. execute.class.php

    <?php
    class mtry
    {

    function mtry(){
    // this is the constructer method
    // you may read about it from php manual
    }

    function execute_query($query)
    {
    mysql_query($query);
    }
    }

    ?>

    C. db_connect.php

    <?php
    $con = mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("try",$con) or die(mysql_error());
    ?>

  3. The Following User Says Thank You to katierosy For This Useful Post:

    heavensgate15 (04-13-2010)

  4. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    $username = $_GET['user'];
    $password = $_GET['password'];

    $sqlstring = "insert into example(username,password) values('$username','$password')";
    You should never do this. Always validate user input, especially before inserting it into SQL. Read up on SQL injection.

  5. #4
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Smile

    To: Katie

    Tnx... I should have check for predefined tokens.. hehe, well, tnx again...


    To: Traq

    Yup, I'm aware of that... I just keep it that way coz I'm just testing how classes works in php...

  6. #5
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Hi guys.

    I'm trying to use a form to pass a form value (count) back to itself to update the counter file which is in an .xml document. I'm cutting all non counter code out for less distraction.

    In the body I have my form:
    <form action="<?php echo $PHP_SELF;?> method="post">
    <?php
    $doc = new DOMDocument();
    $doc->load('sidebarcounter.xml');

    $mycount = $doc->getElementsByTagName( "mycount" );
    $numcount = $mycount->item(0)->nodeValue;

    echo"<input type'text id='count' value=$numcount>'
    ?>

    so you see number 1 on the page. I want to update the file so after you hit the submit button they .xml document reads 2 so when the page loads again it give you the number 2.

    So I need code before the page is read to insert the new number. However I don't want to add 1 to the number if I just open the page so I need to check if $_POST["Quote"] has anything in it...if not do nothing...if so run script to add to the .xlm.


    I do not know how to check if the element is null
    what is wrong with this test code...that should do nothing if you just come in but if you submit the page to self it will write 2 at the top of the page...again this for testing not to update.
    <?php
    if ($_POST["Quote"])
    {
    $newcount = $_POST["Quote"] = 1;
    echo $newcount;
    }

    thanks for any help you can be.

  7. #6
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Quote Originally Posted by I am Abby View Post
    <?php
    if ($_POST["Quote"])
    {
    $newcount = $_POST["Quote"] = 1;
    echo $newcount;
    }

    thanks for any help you can be.
    oops!

    The quoted clip should be

    if ($_POST["count"])
    {
    $newcount = $_POST["count"] = 1;
    echo $newcount;
    }

    I always get back 2.
    I don't want this to happen unless the form was submited.

  8. #7
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Nevermind I figured it out. Go girl power!

  9. #8
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Ack! I was wrong...still not working right. HELP!

  10. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Why are you using: $a=$b=$c? I don't understand how that is useful. Don't you mean $a=$b+$c?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  11. #10
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Quote Originally Posted by I am Abby View Post
    Nevermind I figured it out. Go girl power!
    Quote Originally Posted by djr33 View Post
    Why are you using: $a=$b=$c? I don't understand how that is useful. Don't you mean $a=$b+$c?
    Sorry, typo. I should put my dog down when typing or I should cut and paste from my code.

    here's the part that must be wrong
    if ($_POST["count"])

    after hitting the submit the code should run...otherwise not.
    The first time you run this...it does not run. If you hit the submit and the page sends the data back to itself it works correctly. After that if you refresh the page I don't want it to work.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •