Hi i have the code i am not able to insert the datas into database while executing i got some error. i have tried many codes but no help .[After entering the data]when i click the submit button i got The Username is already taken!. [Without entering the data]when i click the submit button i got User Added Successfully.<a href='index.php'>Click here to Login.Please correct the code and modify the code as simple to understand

<?php
require_once('conn.php');
 
 
 
if(isset($_POST['submit']))
{
 if($_POST['name']=="" ||$_POST['username']=="" || $_POST['password']=="" || $_POST['gender']=="" || $_POST['dob']=="" || $_POST['resitown']=="" || $_POST['nativetown']=="" || $_POST['qualify']=="" || $_POST['jobdetail']=="" || $_POST['income']==""|| $_POST['disability']==""|| $_POST['maritalstatus']==""|| $_POST['emailid']==""|| $_POST['phoneno']==""|| $_POST['mobile']==""|| $_POST['address']=="")
 {
 $err="All Fields are required";
 }
$namecheck = mysql_query("SELECT username FROM personal WHERE username ='$username'");
 
$count = mysql_num_rows($namecheck);
 
 
    if($count!=0)//Record is present!
 
    {
 
        die("<h2>ERROR: The Username is already taken!</h2>");
    }
 
 
 $_POST["dob"]= $_POST["Year"]."-". $_POST["Month"]."-". $_POST["Date"];
 
  mysql_query("insert into personal(name,username,password,gender,dob,resitown,nativetown,qualify,jobdetail,income,disability,maritalstatus,emailid,phoneno,mobile,address) values('$_POST[name]','$_POST[username]','$_POST[password]','$_POST[gender]','$_POST[dob]','$_POST[resitown]','$_POST[nativetown]','$_POST[qualify]','$_POST[jobdetail]','$_POST[income]','$_POST[disability]','$_POST[maritalstatus]','$_POST[emailid]','$_POST[phoneno]','$_POST[mobile]','$_POST[address]')") or die(mysql_error());
  $err="User Added Successfully.<a href='index.php'>Click here to Login.</a>";
 
}
?>
<html>
<head>
</head>
<body>
<?php if(isset($_POST['submit']))
{echo $err;}
 
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" name="submit">
<table>
<tr>
 
<td>Name</td>
<td><input type="text" name="name" value="" size="30" maxlength="30"/></td>
</tr>
 
<tr>
          <td>Username</td>
          <td><input name="username" type="text" size="30" maxlength="30" /></td>
        </tr>
        <tr>
 
<tr>
<td>Password</td>
<td><input name="password" type="password" value="" size="30" id="password" />
</td>
</tr>
<tr>
          <td>Gender:</td>
          <td>
 
          <input type="radio" name="gender" value="Male" id="Gender_0" />
          Male
 
          <input type="radio" name="gender" value="Female" id="Gender_1" />
          Female
      </td>
        </tr>
 
      <tr>
        <td>Date of birth</td>
        <td><select name="Date">  <option>Date</option>
 
		  <?php for ($i=1;$i<=31;$i++){ ?>
 
          <option><?php echo $i?></option>
          <?php }?>
        </select>
          <select name="Month"/>    <option>Month</option>
 
		   <?php for ($i=01;$i<=12;$i++){ ?>
 
          <option><?php echo $i?></option>
          <?php }?>
 
          </select>
 
          <select name="Year">   <option>Year</option>
 
            <?php for ($i=2011;$i>=1950;$i--){ ?>
 
            <option><?php echo $i?></option>
            <?php }?>
      </select> </td>
      </tr>
      <tr>
      <?php
 
  //calculate years of age (input string: YYYY-MM-DD)
  function dob ($dob){
    list($year,$month,$day) = explode("-",$dob);
    $year_diff  = date("Year") - $year;
    $month_diff = date("Month") - $Month;
    $day_diff   = date("Date") - $Date;
    if ($day_diff < 0 || $month_diff < 0)
      $year_diff--;
    return $year_diff;
  }
  ?>
<td>Age</td>
<td><input name="age" type="text" value="<?php print $year_diff; ?>" size="10" id="age" /></td></tr>
 
 
       <tr>
        <td>Residing Town/City:</td>
        <td><input name="resitown" type="text" size="30" maxlength="30" /></td>
      </tr>
 
      <tr>
        <td>Native Town/City :</td>
        <td><input name="nativetown" type="text" size="30" maxlength="30" /></td>
      </tr>
      <tr>
<tr>
 <tr>
        <td>Qualification</td>
        <td><select name="qualify">
          <option>Phd</option>
          <option>M.B.B.S</option>
          <option>B.E/B.tech</option>
          <option>Bsc</option>
          <option>Bcom</option>
          <option>Diploma</option>
          <option>Other</option>
        </select></td>
      </tr>
<tr>
 
 <tr>
        <td>Job Details :</td>
        <td><input name="jobdetail" type="text" size="30" maxlength="20" /></td>
      </tr>
       <tr>
      <td>Income</td>
            <td><input name="income" type="text" size="30" maxlength="30" /></td>
      </tr>
      <tr>
        <td>Disability(if any) :</td>
        <td>
          <label>
            <input type="radio" name="disability" value="yes" id="Disability_0" />
            Yes</label>
          <label>
            <input type="radio" name="disability" value="no" id="Disability_1" />
            No</label>
        </td>
      </tr>
 
      <tr>
        <td>Marital Status :</td>
        <td>
          <label>
            <input type="radio" name="maritalstatus" value="un married" id="Maritalstatus_0" />
            un married</label>
          <label>
            <input type="radio" name="maritalstatus" value="Divorced" id="Maritalstatus_1" />
            Divorced</label>
          <label>
            <input type="radio" name="maritalstatus" value="Widow/widower" id="Maritalstatus_2" />
            Widow/widower</label>
        </td>
      </tr>
       <tr>
        <td>Email ID :</td>
        <td><input name="emailid" type="text" size="30" maxlength="30" /></td>
      </tr>
      <tr>
        <td>Phone no:</td>
        <td><input name="phoneno" type="text" size="30" maxlength="30" /></td>
      </tr> <tr>
        <td>Mobile no :</td>
        <td><input name="mobile" type="text" size="30" maxlength="30" /></td>
      </tr>
       <tr>
        <td>Address</td>
        <td><input name="address" type="text" size="30" maxlength="90" /></td>
      </tr>
<td><input type="submit" name="submit" value="Sumbit" id="submit"/></td>
</tr>
</table>   
 
</form>
</body>
</html>

Please help me as soon as possible

Recommended Answers

All 10 Replies

Member Avatar for iamthwee

Perhaps it is a language issue, but the way you word your question is like an order. People on these forums are here by their free will and their own time.

Ordering is likely to be ignored.

Member Avatar for cuonic

<?php
Try this :

if($count !== 0)//Record is present!
 
{
 
die("<h2>ERROR: The Username is already taken!</h2>");
}

Also you need to protect your from field imo... You have form fields that can be 1 char long, or 560 char long, also you do not sanitize them before DB input. This all needs to be done unless you want a possible security hole in your web app. ;)

Sorry sir i assure that in future this will not happen

Thank you cuonic

i have changed the code as per your instruction.but i got only The Username is already taken eventhough i am using new username . [Without entering the data]when i click the submit button i als got The Username is already taken
Please help me

Just echo your count value and see the result

echo "count=".$count = mysql_num_rows($namecheck);
Member Avatar for cuonic

Wow, I can't believe I missed that :[

$username isn't set to anything....

$username = mysql_real_escape_string($_POST['username']);
$namecheck = mysql_query("SELECT username FROM personal WHERE username ='$username'");
$namecheck = mysql_query("SELECT username FROM personal WHERE username ='$username'");

Where did you pass the value for $username?

Thank you karthik & cuonic
now i am getting The Username is already taken eventhough i have not entered all the fields

Please help me

Member Avatar for cuonic

--'

Use Javascript to validate null inputs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.