PDA

View Full Version : Not adding to DB [PHP]



loserWILL
22-07-2008, 01:59 AM
When you press register, it doesn't add the contents to the database. All it does is refresh the page.


<?php

if($logged['in'] == 1){
header("Location: index.php");
}

if(row_exists('users','ip',$ip)){ ?>
Your IP ($ip) has already been registered with.
<?php }

else{
if($_POST['register']){
$username = clean($_POST['username']);
$email = clean($_POST['email']);
$password = clean($_POST['password']);
$cpassword = clean($_POST['cpassword']);

$errors = 0;
$error_message = '';

if(empty($username) || empty($email) || empty($password) || empty($cpassword)){
$errors++;
$error_message = 'All fields must be filled in.';
}

if(row_exists('users','username',$username)){
$errors++;
$error_message = 'That username is already taken.';
}

if(row_exists('users','email',$email) || !is_valid_email($email)){
$errors++;
$error_message = 'That email is alreayd in use or is incorrect.';
}

$password = enc($password);
$cpassword = enc($cpassword);

if($password != $cpassword){
$errors++;
$error_message = 'The two passwords you entered do not match.';
}

if($errors = 0){
$insert = mysql_query("INSERT INTO `users` (`username`,`password`,`email`,`ip`,`joindate`,`jo intime`) VALUES ('".$username."','".$password."','".$email."','".$ip."','".$date."','".$time."')");

$subject = "Welcome to willSystem";
$body = "Thanks for joining (test message from willSystem demo.)";
send_mail($email,$subject,$body);

?>

Thanks for joining, you may now <a href="login.php">login</a>.

<?php

exit();
} else{
echo $error_message;
}

}

?>
<form method="post">
Username<br />
<input type="text" name="username" value="<?=$username?>" maxlength="12"><br /><br />

Email<br />
<input type="text" name="email" value="<?=$email?>"><br /><br />

Password<br />
<input type="password" name="password"> Confirm <input type="password" name="cpassword"><br /><br />

<input type="submit" name="register" value="Register"><br /><br />

All fields are required.
</form>

<?php
}
?>+rep

Moved by Invent (Forum Moderator) from Designing & Development: Please post in the correct forum next time, thanks :).

Excellent1
22-07-2008, 02:02 AM
Nevermind, got an error.

loserWILL
22-07-2008, 02:04 AM
Here:


<?php

ob_start();


ob_start("ob_gzhandler");


session_start();

$database = array();

$database['host'] = 'localhost';
$database['name'] = 'x';
$database['user'] = 'x';
$database['pass'] = 'x';

$mysql_connection = mysql_pconnect($database['host'], $database['user'], $database['pass']);

mysql_select_db($database['name'], $mysql_connection);

$sess_id = (!$_SESSION['id']) ? 0 : $_SESSION['id'];
$sess_username = (!$_SESSION['username']) ? 'nonexistantuser_DIE' : $_SESSION['username'];
$sess_password = (!$_SESSION['password']) ? 'nonexistantpass_DIE' : $_SESSION['password'];

$query = mysql_query("SELECT * FROM `users` WHERE `id`='".$sess_id."' AND `username`='".$sess_username."' AND `password`='".$sess_password."' LIMIT 1");
$count = mysql_num_rows($query);

$logged = array();

if($count == 0){
$logged['username'] = 'Guest';
$logged['level'] = 3;
$logged['in'] = 0;
}else{
$logged = mysql_fetch_array($query);
$logged['in'] = 1;
}

$date = date("m.d.Y"); /* Date - Month.Day.Year */
$time = time(); /* Numerical Time */
$timestamp = date("g:i A"); /* Time - Hour:Minute AM/PM */
$ip = getenv('REMOTE_ADDR'); /* Ip Address */

?>

Hypertext
22-07-2008, 02:45 AM
Don't use ! in your ternarys, instead just reverse the statements.

You shouldn't be directly editing data taken from a database ie $logged['in']

You mispelt mysql_connect

is $_SESSION['id'] boolean?

You have a lot of pointless ternarys

I think your mistaking booleans and wether a string is null/not set

instead get an ip like $_SERVER['REMOTE_ADDR']

single quotes are faster than doubles

your mistaking your variable names (time types) (just a personal preference)

$logged['in'] should be set as true or false, not 1 or 0.

I personally wouldn't use a mysql connection resource in your database selector

I don't know where your going with the output buffering

a few personal preferences but some are needed.

please check your code in a debugger or an ide, coming on here asking about mysql_pconnect, it's to be honest wasting everybodys time when you could see that individual mistake in quarter of a second on an ide or a debugger.


ok your first page

ips are dynamic a lot of the time so don't use them


your database schema looks bad - ip - as a row?

++$error;
is faster than
$error++

Use echo equals like <?= $var ?>

where are some of your functions coming from?

such as is_valid_email




HOW THE HECK ARE YOU DOING ALL THIS WITHOUT A SINGLE ERROR TO PRODUCE FOR US?



Wow...

as for your html you didn't do much with <form> atleast add action


oh and get rid of $insert = ...why is that there in the first place!!??!

Dentafrice
22-07-2008, 12:48 PM
I suggest that everyone read this ;) Quite funny. heh.


Don't use ! in your ternarys, instead just reverse the statements.

You shouldn't be directly editing data taken from a database ie $logged['in']

You mispelt mysql_connect

is $_SESSION['id'] boolean?

You have a lot of pointless ternarys

I think your mistaking booleans and wether a string is null/not set

instead get an ip like $_SERVER['REMOTE_ADDR']

single quotes are faster than doubles

your mistaking your variable names (time types) (just a personal preference)

$logged['in'] should be set as true or false, not 1 or 0.

I personally wouldn't use a mysql connection resource in your database selector

I don't know where your going with the output buffering

a few personal preferences but some are needed.

please check your code in a debugger or an ide, coming on here asking about mysql_pconnect, it's to be honest wasting everybodys time when you could see that individual mistake in quarter of a second on an ide or a debugger.


ok your first page

ips are dynamic a lot of the time so don't use them


your database schema looks bad - ip - as a row?

++$error;
is faster than
$error++

Use echo equals like <?= $var ?>

where are some of your functions coming from?

such as is_valid_email




HOW THE HECK ARE YOU DOING ALL THIS WITHOUT A SINGLE ERROR TO PRODUCE FOR US?



Wow...

as for your html you didn't do much with <form> atleast add action


oh and get rid of $insert = ...why is that there in the first place!!??!

Okay, don't be stupid.. you don't know anything about coding.

After reading your post.. and seeing your stupid remarks about what you think, I have to laugh.. :eusa_clap good job.. it was all a joke right?

Sadly not with you.. you are actually being serious.. you think what you just said was correct.

No.. no it wasn't.

Lets start off with some of the things you just said, and address why they are the most idiotic remarks about PHP I think I have heard yet.

About as bad as someone asking what echo does.

Don't use ! in your ternarys, instead just reverse the statements.

Why do that, and increase the file size and coding.. that is not the preferred method.. use !=, but not !$statement.


You shouldn't be directly editing data taken from a database ie $logged['in']

Why not? It Isn't editing it on the database.. just the array it is stored on. It is his data.. it is checking the array, not the database..

Not the safest method, but it works for what he is doing.. duh.


is $_SESSION['id'] boolean?

Why would you have an id with letters?


instead get an ip like $_SERVER['REMOTE_ADDR']

getenv is the opposite of using a superglobal.. my god read php.net


single quotes are faster than doubles

No.. single quotes are for simple strings.. there is no use having a bunch of:



$string = 'hey ' . $there . ' my name is ' . $caleb . ' I hope ' . $you . ' ...';


That is stupid when you can just use a set of "" and be done with it, as PHP parses that string above as each '' being a different string, then joins them.

So use '' for simple things



$string = 'hello';


"" for variables and other things.. different times for different things..

don't start going about speeds... you have no clue.


$logged['in'] should be set as true or false, not 1 or 0.

Haha.. this is my favorite! My absolute favorite.

Mr. PHP MAN!

TRUE/FALSE is 1/0!!!!!!

I thought you knew all this? All true false is, is a boolean!

So you are telling him to be setting it to the same thing, are you stupid?

Don't give advice if you don't know what you are talking about.

You are trying too hard to be smart.


I personally wouldn't use a mysql connection resource in your database selector

Okay dude.. listen.. this isn't a normal mysql connect, it is a pconnect, you normally need a link for it to communicate right as it is persis.

You want to keep it on the same link! Not start another connection, then you run into problems with MySQL taking up 200MB of RAM.

Gah.


I don't know where your going with the output buffering

Maybe if you read?


ob_start("ob_gzhandler");

He is gzipping it! Congratulations for not reading 6th line.

He is probably using cookies.. you need ob_start(); :rolleyes:


a few personal preferences but some are needed.

please check your code in a debugger or an ide, coming on here asking about mysql_pconnect, it's to be honest wasting everybodys time when you could see that individual mistake in quarter of a second on an ide or a debugger.

You don't need an IDE, it makes it easier yes, but he isn't to the point now where he needs a professional development tool.

From what I have read, you are on about the same level as him, and neither do you.

It's to be honest you are wasting everyone's time when you come on here providing wrong advice, wrong coding tips, and stupid points.

mysql_pconnect is a function to connect to the database.. it isn't ******* mispelled.

You really need to use PHP.net..


ips are dynamic a lot of the time so don't use them

No **** sherlock, you just figured out the internet?

What other identifying variable do you suggest we use then..? There isn't one, which is why sites log IPs.

You could use hostname, which just transfers to an IP and vise versa, or include a shockwave movie and grab the MAC id but that is too much of a pain.

Which is why we use IPs. Gah.


your database schema looks bad - ip - as a row?

Yeah, he is storing the IP.. dip****, the ip isn't a row.. it is a column..

He is either storing the IP when registered, or constantly updating it.. why would you think it is a row?


++$error;
is faster than
$error++

It is the SAME thing.. just reversed.. you are telling it to do the same thing.. it is neither faster or slower.. before making a statement.. test it out.. time it.. god.


Use echo equals like <?= $var ?>

No.. just no.. that is an awful way to do it.. <? is a shortag, do it the right way if you are going to be telling him how to do it.

You are going to make him a bad as you.




Simple ;) Put it in your clipboard, or snippets, easy as heck.


where are some of your functions coming from?

Probably a file he hasn't posted, thought of that?



HOW THE HECK ARE YOU DOING ALL THIS WITHOUT A SINGLE ERROR TO PRODUCE FOR US?


All your statements are invalid, so there is no 'doing all this'.

If you haven't noticed.. it isn't about the PHP.. it is about inserting into the database.


as for your html you didn't do much with <form> atleast add action

Seriously kid.. he didn't ask what the hell you thought about his HTML, if you haven't noticed.. the title is Not adding to DB [PHP]

PHP! PHP! damn.


oh and get rid of $insert = ...why is that there in the first place!!??!



Because it is a query! You can get results from queries, server response, and do way more as well as fetch the array sent in an insert.

There is nothing wrong with that..

All in all.. you just got owned... quit providing advice.. noob.

----------------
HELP
----------------


change your $insert to this:

[PHP]
$insert = mysql_query( "INSERT INTO `users` (`username`,`password`,`email`,`ip`,`joindate`,`jo intime`) VALUES ('" . $username . "','" . $password . "','" . $email . "','" . $ip . "','" . $date . "','" . $time . "')" ) or die( mysql_error() );



The error probably is in MySQL, but I can't be arsed to check the code.

That should return a MySQL error for us to look through.

If that isn't it, try changing to mysql_connect and removing the $link from the mysql_select_db.

Excellent1
22-07-2008, 01:06 PM
Don't use ! in your ternarys, instead just reverse the statements.

You shouldn't be directly editing data taken from a database ie $logged['in']

You mispelt mysql_connect

is $_SESSION['id'] boolean?

You have a lot of pointless ternarys

I think your mistaking booleans and wether a string is null/not set

instead get an ip like $_SERVER['REMOTE_ADDR']

single quotes are faster than doubles

your mistaking your variable names (time types) (just a personal preference)

$logged['in'] should be set as true or false, not 1 or 0.

I personally wouldn't use a mysql connection resource in your database selector

I don't know where your going with the output buffering

a few personal preferences but some are needed.

please check your code in a debugger or an ide, coming on here asking about mysql_pconnect, it's to be honest wasting everybodys time when you could see that individual mistake in quarter of a second on an ide or a debugger.


ok your first page

ips are dynamic a lot of the time so don't use them


your database schema looks bad - ip - as a row?

++$error;
is faster than
$error++

Use echo equals like <?= $var ?>

where are some of your functions coming from?

such as is_valid_email




HOW THE HECK ARE YOU DOING ALL THIS WITHOUT A SINGLE ERROR TO PRODUCE FOR US?



Wow...

as for your html you didn't do much with <form> atleast add action


oh and get rid of $insert = ...why is that there in the first place!!??!
Mod, please delete this post. Will, if you get an error post it here.

Independent
22-07-2008, 04:08 PM
I won't bother quoting your post caleb, it's long, but I agree as your code is right etc.

Add to your form code; (Top of your form, replace the FORM tag)


<form action="" method="post">

Hypertext
23-07-2008, 12:13 AM
I suggest that everyone read this ;) Quite funny. heh.



Okay, don't be stupid.. you don't know anything about coding.

After reading your post.. and seeing your stupid remarks about what you think, I have to laugh.. :eusa_clap good job.. it was all a joke right?

Sadly not with you.. you are actually being serious.. you think what you just said was correct.

No.. no it wasn't.

Lets start off with some of the things you just said, and address why they are the most idiotic remarks about PHP I think I have heard yet.

About as bad as someone asking what echo does.

Don't use ! in your ternarys, instead just reverse the statements.

Why do that, and increase the file size and coding.. that is not the preferred method.. use !=, but not !$statement.



Why not? It Isn't editing it on the database.. just the array it is stored on. It is his data.. it is checking the array, not the database..

Not the safest method, but it works for what he is doing.. duh.



Why would you have an id with letters?



getenv is the opposite of using a superglobal.. my god read php.net



No.. single quotes are for simple strings.. there is no use having a bunch of:



$string = 'hey ' . $there . ' my name is ' . $caleb . ' I hope ' . $you . ' ...';
That is stupid when you can just use a set of "" and be done with it, as PHP parses that string above as each '' being a different string, then joins them.

So use '' for simple things



$string = 'hello';
"" for variables and other things.. different times for different things..

don't start going about speeds... you have no clue.



Haha.. this is my favorite! My absolute favorite.

Mr. PHP MAN!

TRUE/FALSE is 1/0!!!!!!

I thought you knew all this? All true false is, is a boolean!

So you are telling him to be setting it to the same thing, are you stupid?

Don't give advice if you don't know what you are talking about.

You are trying too hard to be smart.



Okay dude.. listen.. this isn't a normal mysql connect, it is a pconnect, you normally need a link for it to communicate right as it is persis.

You want to keep it on the same link! Not start another connection, then you run into problems with MySQL taking up 200MB of RAM.

Gah.



Maybe if you read?



He is gzipping it! Congratulations for not reading 6th line.

He is probably using cookies.. you need ob_start(); :rolleyes:



You don't need an IDE, it makes it easier yes, but he isn't to the point now where he needs a professional development tool.

From what I have read, you are on about the same level as him, and neither do you.

It's to be honest you are wasting everyone's time when you come on here providing wrong advice, wrong coding tips, and stupid points.

mysql_pconnect is a function to connect to the database.. it isn't ******* mispelled.

You really need to use PHP.net..



No **** sherlock, you just figured out the internet?

What other identifying variable do you suggest we use then..? There isn't one, which is why sites log IPs.

You could use hostname, which just transfers to an IP and vise versa, or include a shockwave movie and grab the MAC id but that is too much of a pain.

Which is why we use IPs. Gah.



Yeah, he is storing the IP.. dip****, the ip isn't a row.. it is a column..

He is either storing the IP when registered, or constantly updating it.. why would you think it is a row?



It is the SAME thing.. just reversed.. you are telling it to do the same thing.. it is neither faster or slower.. before making a statement.. test it out.. time it.. god.



No.. just no.. that is an awful way to do it.. <? is a shortag, do it the right way if you are going to be telling him how to do it.

You are going to make him a bad as you.




Simple ;) Put it in your clipboard, or snippets, easy as heck.


where are some of your functions coming from?

Probably a file he hasn't posted, thought of that?



All your statements are invalid, so there is no 'doing all this'.

If you haven't noticed.. it isn't about the PHP.. it is about inserting into the database.



Seriously kid.. he didn't ask what the hell you thought about his HTML, if you haven't noticed.. the title is Not adding to DB [php]

PHP! PHP! damn.



Because it is a query! You can get results from queries, server response, and do way more as well as fetch the array sent in an insert.

There is nothing wrong with that..

All in all.. you just got owned... quit providing advice.. noob.

----------------
HELP
----------------


change your $insert to this:

[php]
$insert = mysql_query( "INSERT INTO `users` (`username`,`password`,`email`,`ip`,`joindate`,`jo intime`) VALUES ('" . $username . "','" . $password . "','" . $email . "','" . $ip . "','" . $date . "','" . $time . "')" ) or die( mysql_error() );

The error probably is in MySQL, but I can't be arsed to check the code.

That should return a MySQL error for us to look through.

If that isn't it, try changing to mysql_connect and removing the $link from the mysql_select_db.
What the hell was that orgasm about?
Chill pill alert.
Seriously Caleb, have you even heard about something called benchmarks, the stuff I said is faster is faster because it is. And your thing about <?= ?> is just plain funny. Your the noob!, noob.

Source
23-07-2008, 12:17 AM
*removed*

Edited by SyrupyMonkey (Forum Super Mod.): Please don't be rude to other members.

Dentafrice
23-07-2008, 12:59 AM
What the hell was that orgasm about?
Chill pill alert.
Seriously Caleb, have you even heard about something called benchmarks, the stuff I said is faster is faster because it is. And your thing about <?= ?> is just plain funny. Your the noob!, noob.
Quit using terms in which you do not even know the definition of, you don't even know what benchmarks or benchmarking is, or how to even complete a succesful, accurate benchmarking test.

You really don't have any idea about PHP, how it works, and what it is based on?

I have been coding far longer then you, and I know far more then you, if I remember correctly.. you asked me what a class is.. and that was around May, and in almost two months you know what I have learned in years, no...

Quit pretending you know what you are talking about.. it is getting old.

I know what the **** a benchmark is, don't talk to me like I'm stupid.. when you don't even know what one is.

It is not "faster because it is", no.. you don't even know what PHP is doing when it interprets the code.. you don't know what memory it is using, the overhead from using different functions and different methods is all the same.

Quit pretending..

I would love to see how you think my statement about <?=$test; ?> is just "plain funny", *removed*

Edited by SyrupyMonkey (Forum Super Moderator): Please don't be rude, thanks.

loserWILL
23-07-2008, 02:39 AM
Caleb: what do you mena the $link in the mysql_select_db? I couldn't find that variable.

Dentafrice
23-07-2008, 02:55 AM
I meant $mysql_connection, my bad.

Baving
23-07-2008, 11:41 AM
You mispelt mysql_connect

mysql_pconnect is a perfectly legit function. I agree in the context of this script mysql_connect would be a better preference as there is no reason to have a persistent connection to MySQL. mysql_pconnect should only be used in certain circumstances and therefore is not required for most basic web scripts.


single quotes are faster than doubles

Yes, they may be faster, however for a simple small web application this doesn't need to be taken into account. It will make no difference at all. Saying they are faster really only applies for much larger sites such as FaceBook.


$logged['in'] should be set as true or false, not 1 or 0.

Same thing:



$i=0;
if($i) echo 'true'; else echo 'false';


Would echo false.



$i=1;
if($i) echo 'true'; else echo 'false';


Would echo true.


I personally wouldn't use a mysql connection resource in your database selector

You should. It is good practice especially later on when you develop further and wish to connect to several databases at the same time.


please check your code in a debugger or an ide, coming on here asking about mysql_pconnect, it's to be honest wasting everybodys time when you could see that individual mistake in quarter of a second on an ide or a debugger.

This forum is partly to help other users, therefore if a user posts asking for help you shouldn't turn them away. If the user's PHP level isn't as good as yours, (No offence aimed at you LoserWill) then you should be offering advice. Life is all about learning, not saying "go away".


ips are dynamic a lot of the time so don't use them

Not true all the time. There are ways of creating IP comparison scripts to help track users on dynamic IPs.


++$error;
is faster than
$error++

No it isn't, they are the same thing.


Use echo equals like <?= $var ?>

Saying that may be another form of "echo" however you need the short_open_tag option enabled in the PHP Configuration.


HOW THE HECK ARE YOU DOING ALL THIS WITHOUT A SINGLE ERROR TO PRODUCE FOR US?

I won't comment..


oh and get rid of $insert = ...why is that there in the first place!!??!

It is good practice to assign querys to a variable.


Seriously Caleb, have you even heard about something called benchmarks, the stuff I said is faster is faster because it is. And your thing about <?= ?> is just plain funny. Your the noob!, noob.

It may be faster in large scale websites however in this instance it will make no difference at all.

loserWILL
23-07-2008, 01:18 PM
No, that's not it Caleb. (thanks btw.)

loserWILL
23-07-2008, 04:30 PM
I've tried a lot of things and nothing is working. Anybody have any ideas?

Dentafrice
23-07-2008, 04:33 PM
Change the query to



$insert = mysql_query ( "INSERT INTO `users` (username, password, email, ip, joindate, jointime) VALUES('$username', '$password', '$email', '$ip', '$date', '$time')" ) or die ( mysql_error () );

loserWILL
23-07-2008, 04:48 PM
Nope, it still doesn't add it to the database, take a look: http://kolzy.com/ws/register.php

Dentafrice
23-07-2008, 05:14 PM
<?php

if($logged['in'] == 1){
header("Location: index.php");
}

if(row_exists('users','ip',$ip)){ ?>
Your IP ($ip) has already been registered with.
<?php }

else{
if($_GET["action"] == "register"){
$username = clean($_POST['username']);
$email = clean($_POST['email']);
$password = clean($_POST['password']);
$cpassword = clean($_POST['cpassword']);

$errors = 0;
$error_message = '';

if(empty($username) || empty($email) || empty($password) || empty($cpassword)){
$errors++;
$error_message = 'All fields must be filled in.';
}

if(row_exists('users','username',$username)){
$errors++;
$error_message = 'That username is already taken.';
}

if(row_exists('users','email',$email) || !is_valid_email($email)){
$errors++;
$error_message = 'That email is alreayd in use or is incorrect.';
}

$password = enc($password);
$cpassword = enc($cpassword);

if($password != $cpassword){
$errors++;
$error_message = 'The two passwords you entered do not match.';
}

if($errors = 0){
$insert = mysql_query("INSERT INTO `users` (`username`,`password`,`email`,`ip`,`joindate`,`jo intime`) VALUES ('".$username."','".$password."','".$email."','".$ip."','".$date."','".$time."')");

$subject = "Welcome to willSystem";
$body = "Thanks for joining (test message from willSystem demo.)";
send_mail($email,$subject,$body);

?>

Thanks for joining, you may now <a href="login.php">login</a>.

<?php

exit();
} else{
echo $error_message;
}

}

?>
<form method="post" action="?action=register">
Username<br />
<input type="text" name="username" value="<?=$username?>" maxlength="12"><br /><br />

Email<br />
<input type="text" name="email" value="<?=$email?>"><br /><br />

Password<br />
<input type="password" name="password"> Confirm <input type="password" name="cpassword"><br /><br />

<input type="submit" name="register" value="Register"><br /><br />

All fields are required.
</form>

<?php
}
?>

loserWILL
23-07-2008, 06:31 PM
I'll try it when I get back on my main pc. Thanks again for your help.

loserWILL
23-07-2008, 08:30 PM
I've tried adding an action to the form (like yours) and it didn't do anything. I've looked it through many times and fail to see what's wrong with it.

loserWILL
24-07-2008, 03:15 AM
I tried getting rid of the "$insert =" part so that it just leaves "mysql_query(..." but that didn't work either.

I don't get why it's now working, it should work perfectly fine.

Excellent1
24-07-2008, 12:00 PM
Try checking your database? Did you give the user full permissions?

loserWILL
24-07-2008, 04:15 PM
It has full permissions.

Want to hide these adverts? Register an account for free!