Jump to content

Recommended Posts

hello everybody, i'm new here and also a biginner in PHP...i've just started learning it from a tutorial found on YT. Here is the code:

<?php

mysql_connect ("localhost","root") or die(mysql_error());
mysql_select_db("tutorial") or die(mysql_error());

?>

<form method="POST" action="tutorial.php">
<table border="0" style="font-size: 15px; font-family Tahoma; border: 1px solid black;">
<tr>
	<td>
	Username:
	</td>
	<td>
		<input type="text" name="username" value=" <?php echo isset($_POST['username']); ?>"
	</td>
</tr>
<tr>
	<td colspan="2" align="center">
	 <input type="submit" name="submit" value="submit" />
	</td>
</tr>
</table>
</form>
<?php



if(isset($_POST['submit'])) {

$username = $_POST['username'];

$curnum = 0;

if(!$username) {
	$curnum ++;
	echo $curnum . ". You didn't enter a username!</br>\n";
}
$sql = "SELECT * FROM users WHERE username=' ". $username ." '";
$res = mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($res) > 0) {

	$curnum ++;
	echo $curnum . ". The username '". $username ."' already exists!</br>\n";
	}
if($curnum == 0) {

	mysql_query("INSERT INTO users VALUES(`id`,' ". $username ." ')") or die(mysql_error());

}
}		


?>

When i press submit it says: 1. The username ' ' already exists! , even if i didn't write anything...

Can you please help me?

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/147791-solved-username-already-exists/
Share on other sites

One You are adding spaces inside the ' of the sql, this is not good practice and will cause different issues.

 

   $sql = "SELECT * FROM users WHERE username='". $username ."'";
// and this one here too:
mysql_query("INSERT INTO users VALUES(`id`,'". $username ."')") or die(mysql_error());

 

At least you were consistent.

 

Two you should trim the username coming from post and escape it:

   $username = mysql_real_escape_string(trim($_POST['username']));

 

Just incase anyone posts bad characters.

 

Finally you should check if username is populated before moving on:

   if(empty($username)) {

 

Fix those items and see where that gets you.

 

EDIT:

Also your form will show up no matter what, I do not think you want this. I would move the if before the form and put the form in it's own if statement after the if $_POST['submit'] isset. Something like:

 

if ($curnum > 0 || !isset($_POST['submit'])) {

 

Then put the form inside that. So it only shows if there is an error.

 

I would also make the mysql_num_rows part an if else:

   if(mysql_num_rows($res) > 0) {
   
         $curnum ++;
         echo $curnum . ". The username '". $username ."' already exists!</br>\n";
    }else {
            mysql_query("INSERT INTO users VALUES(`id`,' ". $username ." ')") or die(mysql_error());
   }

 

first, thanks for your reply...it partially works...when i press submit with no value...it says 1. You didn't enter a username!...but after that...on the box it appears a 1...

p.s. i didn't understand where to do this:

EDIT:

Also your form will show up no matter what, I do not think you want this. I would move the if before the form and put the form in it's own if statement after the if $_POST['submit'] isset. Something like:

 

if ($curnum > 0 || !isset($_POST['submit'])) {

 

Here is again the code modified without that step:

<?php
mysql_connect ("localhost","root") or die(mysql_error());
mysql_select_db("tutorial") or die(mysql_error());

?>

<form method="POST" action="tutorial.php">
<table border="0" style="font-size: 15px; font-family Tahoma; border: 1px solid black;">
   <tr>
      <td>
      Username:
      </td>
      <td>
         <input type="text" name="username" value=" <?php echo isset($_POST['username']); ?>"
      </td>
   </tr>
   <tr>
      <td colspan="2" align="center">
       <input type="submit" name="submit" value="submit" />
      </td>
   </tr>
</table>
</form>
<?php


   
if(isset($_POST['submit'])) {

   $username = mysql_real_escape_string(trim($_POST['username']));
   
   $curnum = 0;
   
   if(empty($username)) {
      $curnum ++;
      echo $curnum . ". You didn't enter a username!</br>\n";
   }
   $sql = "SELECT * FROM users WHERE username='". $username ."'";
   $res = mysql_query($sql) or die(mysql_error());
   
    if(mysql_num_rows($res) > 0) {
   
         $curnum ++;
         echo $curnum . ". The username '". $username ."' already exists!</br>\n";
    }else {
            mysql_query("INSERT INTO users VALUES(`id`,' ". $username ." ')") or die(mysql_error());
   }
}      
   

?>

 

Thanks again!

first, thanks for your reply...it partially works...when i press submit with no value...it says 1. You didn't enter a username!...but after that...on the box it appears a 1...

 

You are echoing the "isset" of the field. This will be true or false (ie, 1 or 0) depending on whether it has been set. However as you also have a space in there I think it will always be set, therefore checking isset will always return 1 (so next time round the field will contain a space and a 1).

 

p.s. i didn't understand where to do this:

 

What is probably needed is to move the HTML form from the top of the script down to the bottom (ie, after if has processed the submit), but put it within an if statement (ie, the one given in the reply).

 

All the best

 

Keith

Hi

 

Try this:-

 

<?php
mysql_connect ("localhost","root") or die(mysql_error());
mysql_select_db("tutorial") or die(mysql_error());

   
if(isset($_POST['submit'])) {

   $username = mysql_real_escape_string(trim($_POST['username']));
   
   $curnum = 0;
   
   if(empty($username)) {
      $curnum ++;
      echo $curnum . ". You didn't enter a username!</br>\n";
   }
   $sql = "SELECT * FROM users WHERE username='". $username ."'";
   $res = mysql_query($sql) or die(mysql_error());
   
    if(mysql_num_rows($res) > 0) {
   
         $curnum ++;
         echo $curnum . ". The username '". $username ."' already exists!</br>\n";
    }else {
            mysql_query("INSERT INTO users VALUES(`id`,' ". $username ." ')") or die(mysql_error());
   }
}      

if ($curnum > 0 || !isset($_POST['submit'])) 
{
?>

<form method="POST" action="tutorial.php">
<table border="0" style="font-size: 15px; font-family Tahoma; border: 1px solid black;">
   <tr>
      <td>
      Username:
      </td>
      <td>
         <input type="text" name="username" value=" <?php echo ((isset($_POST['username'])) ? $_POST['username'] : "") ; ?>"
      </td>
   </tr>
   <tr>
      <td colspan="2" align="center">
       <input type="submit" name="submit" value="submit" />
      </td>
   </tr>
</table>
</form>
<?php
}
else
{
echo "Username inserted";
}

 

This should put the form out if there is an error or it is the first time in, otherwise if the username is successfully inserted it will just put up a message to say so.

 

All the best

 

Keith

first of all...there is an error : Notice: Undefined variable: curnum in C:\wamp\www\tutorial.php on line 28

 

After that...if I just press submit without entering anything... I receive this message:

1. You didn't enter a username!

2. The username '' already exists!

Hi

 

For the error, move the initialisation of the $curnum to the start (so it is declared).

 

The other error is because the script checked for a blank username and issued an error if it found one. But then went ahead and tried to insert it anyway. Put an else after the check for a blank username and put the check for an existing username and the insert within that else.

 

Like this

 

<?php
mysql_connect ("localhost","root") or die(mysql_error());
mysql_select_db("tutorial") or die(mysql_error());
   
$curnum = 0;
   
if(isset($_POST['submit'])) {

   $username = mysql_real_escape_string(trim($_POST['username']));
   
   if(empty($username)) {
      $curnum ++;
      echo $curnum . ". You didn't enter a username!</br>\n";
   }
   else
   {
   $sql = "SELECT * FROM users WHERE username='". $username ."'";
   $res = mysql_query($sql) or die(mysql_error());
   
    if(mysql_num_rows($res) > 0) {
   
         $curnum ++;
         echo $curnum . ". The username '". $username ."' already exists!</br>\n";
    }else {
            mysql_query("INSERT INTO users VALUES(`id`,' ". $username ." ')") or die(mysql_error());
   }
   }
}      

if ($curnum > 0 || !isset($_POST['submit'])) 
{
?>

<form method="POST" action="tutorial.php">
<table border="0" style="font-size: 15px; font-family Tahoma; border: 1px solid black;">
   <tr>
      <td>
      Username:
      </td>
      <td>
         <input type="text" name="username" value=" <?php echo ((isset($_POST['username'])) ? $_POST['username'] : "") ; ?>"
      </td>
   </tr>
   <tr>
      <td colspan="2" align="center">
       <input type="submit" name="submit" value="submit" />
      </td>
   </tr>
</table>
</form>
<?php
}
else
{
echo "Username inserted";
}

 

All the best

 

Keith

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.