Log in

View Full Version : PHP classes for a newbie like me... Help plss T_T



heavensgate15
04-09-2010, 01:20 PM
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


<?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


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


execute.class.php


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

katierosy
04-12-2010, 03:44 PM
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());
?>

traq
04-12-2010, 07:20 PM
$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 (http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php).

heavensgate15
04-13-2010, 02:50 PM
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...

I am Abby
04-13-2010, 03:18 PM
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.

I am Abby
04-13-2010, 04:24 PM
<?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.

I am Abby
04-13-2010, 04:30 PM
Nevermind I figured it out. Go girl power!

I am Abby
04-13-2010, 04:55 PM
Ack! I was wrong...still not working right. HELP!

djr33
04-13-2010, 05:45 PM
Why are you using: $a=$b=$c? I don't understand how that is useful. Don't you mean $a=$b+$c?

I am Abby
04-13-2010, 06:42 PM
Nevermind I figured it out. Go girl power!


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.

djr33
04-14-2010, 01:39 AM
if ($var) is a very messy way to check a value.
Here's why:
if (X) will return true unless X is (1) 0, (2) FALSE, (3) '' (empty string), (4) null; (5) not set*, and possibly other cases that I'm not remembering

*However, this will also give a warning about using a variable that doesn't exist.

There are better ways to do this, and one is:
if (isset($var))


The best way to debug this and to determine what sort of if statement you need is to just use echo $_POST["count"]; (immediately before the if statement).
Then you can decide what to do.

traq
04-14-2010, 04:56 AM
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.

That's the problem: if you click Refresh, the browser usually resubmits the POST information. You can avoid this by automatically redirecting the page (you could even simply redirect it to itself) - then if the user refreshes, he'll be refreshing the new page, not the one that just submitted the form. You have to do this before you output anything, so you'll have to pass the information along somehow.

(not tested)

<?php

// if the form was submitted, process the input
if (isset($_POST['Quote']))
{
$newcount = $_POST['Quote'] = 1;
// instead of echoing the value, redirect ("refresh") the page
// and pass the value as a query string
header('Location: '.$_SERVER['PHP_SELF'].'?newcount='.$newcount);
}

// if the form was not just submitted (no $_POST['Quote'])
elseif(isset($_GET['newcount']))
{
// if the query string is present, echo the value
echo $_GET['newcount'];
}

// you could also use sessions.

?>

I am Abby
04-14-2010, 08:06 PM
Thanks for the help guys...
It seems to be working now...and better than that, I'm starting to understand why. :)

However it the line header('Location: '.$_SERVER['PHP_SELF'].'?newcount='.$newcount); kept giving me an error when submitting, so I'm not sure if I'm going to have problems when people refresh the page or not.

traq
04-15-2010, 01:42 AM
What is your error? If it says "Headers already sent", then it's because you've already output something to the browser. header() doesn't work unless the server hasn't sent anything to the browser.

heavensgate15
04-15-2010, 04:24 AM
Ahw, tnx for the replies guyz.... hahahhhaha (sarcastic)... To abby, if you get error in using header, maybe there's a space b4 your <?php .... or something that you output b4 the <?php

If you use the header function:

<space><?php ---> this will generate an error

<?php ---> this will not generate an error

heavensgate15
04-15-2010, 04:39 AM
In summary:

This is wrong when using header function:

<html>
<?php

header("location: blahblahblah.php");

?>
</html>

wrong because there's a tag <html> before the <?php
Just remember that there should be no html tags, spaces, even comments, or what so ever b4 the <?php

I am Abby
04-15-2010, 04:51 PM
Ahw, tnx for the replies guyz.... hahahhhaha (sarcastic)... To abby, if you get error in using header, maybe there's a space b4 your <?php .... or something that you output b4 the <?php

If you use the header function:

<space><?php ---> this will generate an error

<?php ---> this will not generate an error


You guys are so smart. I had a dreamweaver created line above <?php after getting rid of it traq's line worked perfectly.

And heavensgate15 (your name makes me smile) I didn't really mean to take over your thread. Your question was so close to what I was needing.

Again thanks boys.

djr33
04-15-2010, 05:27 PM
Watch out for that in dreamweaver. It tends to do annoying things with spacing that in general keep the code clean and organized (so arguably it's helpful), but sometimes do weird things, especially with the issue of the header() function. So don't be surprised if you have this same problem again, but now you know how to fix it.

traq
04-15-2010, 07:52 PM
of course, ANY html editor is going to have issues like that. FrontPage, for example, is far worse.

I use notepad++ for my coding. :D

djr33
04-15-2010, 09:59 PM
Correct. But to clarify, DW specifically likes to add a return before <?php sometimes.

heavensgate15
04-16-2010, 11:49 AM
To abby: ahhahha, well, it's ok... I'm not mad ya know, as long as I made you smile hahahhha.... :)