[PHP]What am i doing wrong?

Experienced Elementalist
Joined
Apr 15, 2008
Messages
256
Reaction score
0
I know there is something wrong with $msg+="blah blah...";
PHP:
<?php 
session_start(); 
include("includes/header.php"); 
if(isset($_POST['submit'])) {
 $selectUsers="SELECT * FROM `users` "; //Get data for existing users
 $queryUsers=mysql_query($selectUsers) or die('Connection Error.. Please try again.<br>If the problem continues, please contact the web-site administrator.'); //Create a query or display error msg.
 while($userRow=mysql_fetch_array($queryUsers)) { //Loop containing all user info
  $sql_username=$userRow['username'];
  $sql_email=$userRow['email'];
  $sql_blogurl=$userRow['blogurl'];
  $username=sql($_POST['username']);
  $email=sql($_POST['email']);
  $blogurl=sql($_POST['blogurl']);
  $password=sql($_POST['password']);
  if($username == $sql_username) { //2x user checker
   $error+=1;

$msg+='*Username is already in use.<br>'; }

  if($email== $sql_email) { //2x email checker
   $error+=1;

   $msg +='*Email is already in use.<br>'; }

  if($blogurl== $sql_blogurl) { //2x blogurl checker
   $error+=1;

   $msg +='*Blog URL is already in use.<br>'; }
 }
 if(strlen($password)<3) { //password length checker (too short)
  $error+=1;

  $msg +='*Password is too short.<br>'; }

 if(strlen($username)<1) { //username length checker (too short)
  $error+=1;

  $msg +='*Username is too short.<br>'; }

 if(!preg_match('/^.+@.+\..+$/',$email)){ //email format checker
  $error+=1;

  $msg +='*Invaild Email address.<br>'; } 
  if(strlen($blogurl)< 0){
  //too short
  $error+=1;

  $msg +='*Blogurl is too short max 50 min 2<br>';
  }
  if(strlen($blogurl)> 50){
  //too long
  $error+=1;

  $msg +='*Blogurl is too long max 50 min 2<br>';
  }
 //.. Down the page where data is sent
 if(strlen($error) < 1) {
  $insert="INSERT INTO `users` (`username` ,`password` ,`userid` ,`userlevel` ,`email` ,`timestamp` ,`userimage` ,`blogurl`)VALUES ('$username', '$password' , '', '0', '$email', '0', 'images/box.jpg', '$blogurl')"; 
  mysql_query($insert) or die('Could not insert your information. Please try again.<br>If this problem continues, please contact the Web-Site Administrator.');
  echo('Information was sent successfully!');
 } 

}  ?>
<script language="JavaScript" type="text/javascript">
<!--
function formValidator() {
	// check username 
	var name=document.register.blogurl.value;
	if ((name.length < 2) || (name.length > 50)) {
	//too long or too short
		alert("Please enter a blogurl that is between 2 to 50 characters long.");
		document.register.blogurl.focus();
		return false;
	}
	if ((name=='')) {
		alert("Please enter a blogurl.");
		document.register.blogurl.focus();
		return false;
	}
	if ((name.search(/[^a-z0-9_]/gi)>-1)) {
		alert("Please choose a different blog url, only letters, numbers and '_' allowed. No spaces or punctuation.");
		document.register.blogurl.focus();
		return false;
	}
	if ((name.search(/[a-z]/gi)<0)) {
		//alert("Check your MySpace user name. Names must contain at least one letter ('a' thru 'z').");
		alert("Check your  blog url. Blog url's must contain at least one letter ('a' thru 'z').");
		document.register.blogurl.focus();
		return false;
	}
	return true;
}
function smiley(num){
		//start the smiley invasion!
		var q=document.register.username;
		q.value +=num;
		}
//-->
</script>

<form action="<?php $_SERVER['PHP_SELF'];?>" name="register" id="register" method="POST" onSubmit="return formValidator();">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<?php
 if(isset($_POST['submit'])){
if(isset($msg)){
echo $msg; }
  if(strlen($error) < 1) {
  echo"<strong>Thank you for registering ".$_POST['username'].", with the password of ".$_POST['password'].". You may now <a href=".$_POST['blogurl'].">Visit your blog</a> or <a href='login.php'>Login</a>"; } }
?>
<tr><td>Username:</td><td><input type="text" name="username" maxlength="45" value="<?php
if(isset($_POST['username'])){
echo(sql($_POST['username']));
}
?>"></td><td></td></tr>
<tr><td>Password:</td><td><input type="password" name="password" maxlength="30" value="<?php
if(isset($_POST['password'])){
echo(sql($_POST['password']));
}
?>"></td><td></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" maxlength="50" value="<?php
if(isset($_POST['email'])){
echo(sql($_POST['email']));
}
?>"></td><td></td></tr>
<tr><td>Blog Url:</td><td><input type="text" name="blogurl" maxlength="50" value="<?php
if(isset($_POST['blogurl'])){
echo(sql($_POST['blogurl']));
}
?>"></td><td></td></tr>
<tr>
  <td colspan="2" align="right"><input type="submit"  name="submit" value="Join!"></td></tr>
</tr>
</table>
</form>
<?php include("includes/footer.php");?>
But i can't figure out what is wrong, it just echo's : "0"
 
The concatenation operator in PHP is the period (.) (which is quite stupid IMO). So... use .= instead of +=.
 
I was thinking about that, i thought it was right since i use javascript to check forms and its just like that ;)
thanks
 
You can use $msg = $msg . "Blablabla"; too.

Which is what I redundantly do.. lol

But $msg .= $string; or $msg .= 'text'; are the fastest and shortest ways to do this. Quotes take longer to process since they're built to do more functions (like include variables)

I'm not sure if $msg .= $string.' text'; is faster than $msg .= "$string text";, but in the end it's all the same.. ha
 
I'm not sure if $msg .= $string.' text'; is faster than $msg .= "$string text";, but in the end it's all the same.. ha

The first, because you don't relay on PHP's string parsing engine. The first is also more clear (because in a string one would expect it to be a STRING, not a VARIABLE, what if you use the dollar sign for something? I can't believe they added the feature O.O).
 
Back