Jump to content

php script just inserts one row. there is an auto increment id


rahul19dj

Recommended Posts

This is my registration code.

 

Once I enter the fields in the form it shows me registration successful but adds blank data in my database table. It adds number 0 in my mobileno column.

 

Please help me here asap

 

include ('database_connection.php');

 

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

 

$error = array();//Declare An Array to store any error message 

 

if (empty($_POST['mobileno'])) {//if no name has been supplied

    $error[] = 'Please Enter a Mobile Number ';//add to array "error"

} else {

    $name = $_POST['mobileno'];//else assign it a variable

}

if (empty($_POST['fname'])) {//if no name has been supplied

    $error[] = 'Please Enter a First name ';//add to array "error"

} else {

    $name = $_POST['fname'];//else assign it a variable

}

if (empty($_POST['lname'])) {//if no name has been supplied

    $error[] = 'Please Enter a Last name ';//add to array "error"

} else {

    $name = $_POST['lname'];//else assign it a variable

}

if (empty($_POST['email'])) {

    $error[] = 'Please Enter your Email ';

} else {

 

 

    if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-    Z0-9\._-]+)+$/", $_POST['email'])) {

      //regular expression for email validation

        $Email = $_POST['email'];

    } else {

        $error[] = 'Your EMail Address is invalid  ';

    }

 

 

}

 

 

if (empty($_POST['passwd1'])) {

    $error[] = 'Please Enter Your Password ';

} else {

    $Password = $_POST['passwd1'];

}

if (empty($_POST['passwd2'])) {

    $error[] = 'Please Verify Your Password ';

} else {

    $Password = $_POST['passwd2'];

}

 

if (empty($error)) //send to Database if there's no error '

 

{ //If everything's OK...

 

    // Make sure the mobile no is available:

    $query_verify_mobileno = "SELECT * FROM userdtls WHERE mobileno = '$mobileno'";

    $result_verify_mobileno = mysqli_query($dbc, $query_verify_mobileno);

    if (!$result_verify_mobileno)

    {//if the Query Failed ,similar to if($result_verify_mobileno==false)

        echo ' Database Error Occured ';

    }

 

    if (mysqli_num_rows($result_verify_mobileno) == 0) { // IF no previous user is using this number .

 

 

        // Create a unique  activation code:

      $activation = md5(uniqid(rand(), true));

 

 

        $query_insert_user = "INSERT INTO userdtls (`mobileno`, `pass`, `fname`, `lname`, `email`, `activation`) VALUES ( '$mobileno', '$passwd1', '$fname', '$lname', '$email', '$activation')";

 

 

        $result_insert_user = mysqli_query($dbc, $query_insert_user);

        if (!$result_insert_user) {

            echo 'Query Failed ';

        }

 

        if (mysqli_affected_rows($dbc) == 1) { //If the Insert Query was successfull.

 

 

            // Send the email:

          $message = " To activate your account, please click on this link:\n\n";

            $message .= WEBSITE_URL . '/activate.php?email=' . urlencode($Email) . "&key=$activation";

            mail($Email, 'Registration Confirmation', $message, 'From: [email protected]');

 

            // Flush the buffered output.

 

 

            // Finish the page:

            echo '<div class="success">Thank you for registering! A confirmation email has been sent to '.$email.' Please click on the Activation Link to Activate your account </div>';

 

 

        } else { // If it did not run OK.

            echo '<div class="errormsgbox">You could not be registered due to a system error. We apologize for any inconvenience.</div>';

        }

 

    } else { // The mobile number is not available.

        echo '<div class="errormsgbox" >That mobile number has already been registered.</div>';

    }

 

} else {//If the "error" array contains error msg , display them

 

 

 

echo '<div class="errormsgbox"> <ol>';

    foreach ($error as $key => $values) {

 

        echo '  <li>'.$values.'</li>';

 

 

 

    }

    echo '</ol></div>';

 

}

 

mysqli_close($dbc);//Close the DB Connection

 

} // End of the main Submit conditional.

 

 

Errm it looks a bit like you're assigning values to the same variable throughout your script:

 

[b]$name[/b] = $_POST['mobileno'];//else assign it a variable

 

And then in your query you appear to be referencing variables that don't exist...e.g. $lname

 

Or is there more to the script?

 

 

 

I could be wrong but it looks to me like you are overwriting the variable $name with various pieces of $_POST data and then running a query with variables that don't appear to exist anywhere.

 

Unless I am missing something I can't really see where the majority of htese variables come from? Which would explain why nothing is getting inserted.

 

VALUES ( '$mobileno', '$passwd1', '$fname', '$lname', '$email', '$activation')";

 

I changed my code to the following.  But I still can only insert one row .

 

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

    $error = array();//Declare An Array to store any error message 

    if (empty($_POST['mobileno'])) {//if no name has been supplied

        $error[] = 'Please Enter a Mobile Number ';//add to array "error"

    } else {

        $mobileno = $_POST['mobileno'];//else assign it a variable

    }

    if (empty($_POST['fname'])) {//if no name has been supplied

        $error[] = 'Please Enter a First name ';//add to array "error"

    } else {

        $fname = $_POST['fname'];//else assign it a variable

    }

if (empty($_POST['lname'])) {//if no name has been supplied

        $error[] = 'Please Enter a Last name ';//add to array "error"

    } else {

        $lname = $_POST['lname'];//else assign it a variable

    }

    if (empty($_POST['email'])) {

        $error[] = 'Please Enter your Email ';

    } else {

 

 

        if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['email'])) {

          //regular expression for email validation

            $email = $_POST['email'];

        } else {

            $error[] = 'Your EMail Address is invalid  ';

        }

 

 

    }

 

 

    if (empty($_POST['passwd1'])) {

        $error[] = 'Please Enter Your Password ';

    } else {

        $passwd1 = $_POST['passwd1'];

    }

    if (empty($_POST['passwd2'])) {

        $error[] = 'Please Verify Your Password ';

    } else {

        $passwd2 = $_POST['passwd2'];

    }

 

    if (empty($error)) //send to Database if there's no error '

 

    { //If everything's OK...

 

        // Make sure the mobile no is available:

        $query_verify_mobileno = "SELECT * FROM userdtls WHERE mobileno = '$mobileno'";

        $result_verify_mobileno = mysqli_query($dbc, $query_verify_mobileno);

        if (!$result_verify_mobileno)

{//if the Query Failed ,similar to if($result_verify_mobileno==false)

            echo ' Database Error Occured ';

        }

 

        if (mysqli_num_rows($result_verify_mobileno) == 0) { // IF no previous user is using this number .

 

 

            // Create a unique  activation code:

          //$activation = md5(uniqid(rand(), true));

 

 

            $query_insert_user = "INSERT INTO userdtls ( `mobileno`, `pass`, `fname`, `lname`, `email`, `MUM`, `PUN`, `BNG`, `MYS` ) VALUES ( '".$mobileno."', '".$passwd1."', '".$fname."', '".$lname."', '".$email."', '".$mumbai."', '".$pune."', '".$banglore."', '".$mysore."'  )";

 

 

            $result_insert_user = mysqli_query($dbc, $query_insert_user);

            if (!$result_insert_user) {

                echo 'Query Failed ';

            }

 

            if (mysqli_affected_rows($dbc) == 1) { //If the Insert Query was successfull.

 

 

                // Send the email:

              /*$message = " To activate your account, please click on this link:\n\n";

                $message .= WEBSITE_URL . '/activate.php?email=' . urlencode($email) . "&key=$activation";

                mail($email, 'Registration Confirmation', $message, 'From: [email protected]'); */

 

                // Flush the buffered output.

 

 

                // Finish the page:

                echo '<div class="success">Thank you for registering! </div>';

 

 

            } else { // If it did not run OK.

                echo '<div class="errormsgbox">You could not be registered due to a system error. We apologize for any inconvenience.</div>';

            }

 

        } else { // The mobile number is not available.

            echo '<div class="errormsgbox" >That mobile number has already been registered.</div>';

        }

 

    } else {//If the "error" array contains error msg , display them

       

       

 

echo '<div class="errormsgbox"> <ol>';

        foreach ($error as $key => $values) {

           

            echo ' <li>'.$values.'</li>';

 

 

     

        }

        echo '</ol></div>';

 

    }

 

    mysqli_close($dbc);//Close the DB Connection

 

} // End of the main Submit conditional.

Maybe this makes more sense. Give it a go

 

<?php

include ('database_connection.php');

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

$error = array();//Declare An Array to store any error message 

if (empty($_POST['mobileno'])) {//if no name has been supplied
    $error[] = 'Please Enter a Mobile Number ';//add to array "error"
} else {
    $mobile= $_POST['mobileno'];//else assign it a variable
}
if (empty($_POST['fname'])) {//if no name has been supplied
    $error[] = 'Please Enter a First name ';//add to array "error"
} else {
    $fname = $_POST['fname'];//else assign it a variable
}
if (empty($_POST['lname'])) {//if no name has been supplied
    $error[] = 'Please Enter a Last name ';//add to array "error"
} else {
    $lname = $_POST['lname'];//else assign it a variable
}
if (empty($_POST['email'])) {
    $error[] = 'Please Enter your Email ';
} else {


    if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-    Z0-9\._-]+)+$/", $_POST['email'])) {
       //regular expression for email validation
        $email = $_POST['email'];
    } else {
         $error[] = 'Your EMail Address is invalid  ';
    }


}


if (empty($_POST['passwd1'])) {
    $error[] = 'Please Enter Your Password ';
} else {
    $password = $_POST['passwd1'];
}
if (empty($_POST['passwd2'])) {
    $error[] = 'Please Verify Your Password ';
} else {
    $password = $_POST['passwd2'];
}

if (empty($error)) //send to Database if there's no error '

{ //If everything's OK...

    // Make sure the mobile no is available:
    $query_verify_mobileno = "SELECT * FROM userdtls WHERE mobileno = '$mobileno'";
    $result_verify_mobileno = mysqli_query($dbc, $query_verify_mobileno);
    if (!$result_verify_mobileno)
    {//if the Query Failed ,similar to if($result_verify_mobileno==false)
        echo ' Database Error Occured ';
    }

    if (mysqli_num_rows($result_verify_mobileno) == 0) { // IF no previous user is using this number .


        // Create a unique  activation code:
       $activation = md5(uniqid(rand(), true));


        $query_insert_user = "INSERT INTO userdtls (mobileno, pass, fname, lname, email, activation) VALUES ( '$mobile', '$password', '$fname', '$lname', '$email', '$activation')";


        $result_insert_user = mysqli_query($dbc, $query_insert_user);
        if (!$result_insert_user) {
            echo 'Query Failed ';
        }

        if (mysqli_affected_rows($dbc) == 1) { //If the Insert Query was successfull.


            // Send the email:
          $message = " To activate your account, please click on this link:\n\n";
            $message .= WEBSITE_URL . '/activate.php?email=' . urlencode($Email) . "&key=$activation";
            mail($Email, 'Registration Confirmation', $message, 'From: [email protected]');

            // Flush the buffered output.


            // Finish the page:
            echo '<div class="success">Thank you for registering! A confirmation email has been sent to '.$email.' Please click on the Activation Link to Activate your account </div>';


        } else { // If it did not run OK.
            echo '<div class="errormsgbox">You could not be registered due to a system error. We apologize for any inconvenience.</div>';
        }

    } else { // The mobile number is not available.
        echo '<div class="errormsgbox" >That mobile number has already been registered.</div>';
    }

} else {//If the "error" array contains error msg , display them



echo '<div class="errormsgbox"> <ol>';
    foreach ($error as $key => $values) {

        echo '  <li>'.$values.'</li>';



    }
    echo '</ol></div>';

}

mysqli_close($dbc);//Close the DB Connection

} // End of the main Submit conditional.

 

So now you are actually assigning a value to :

 

$mobile
$fname
$lname
$email
$password

 

 

Also - if you do get that script working you will want to use mysql_real_escape_string() on all of your data because inserting raw data into the query makes you ripe for sql injection.

 

Check it out here - http://php.net/manual/en/function.mysql-real-escape-string.php

Ok are you sure all the columns you name in your query exist in the table?

 

Do you get a "query failed" message from this code:

 


$result_insert_user = mysqli_query($dbc, $query_insert_user);
        if (!$result_insert_user) {
            echo 'Query Failed ';
        }

 

 

 

Ok bit confused now. I thought you said nothing was getting inserted?

 

If that query does work and you are inserting an entire row then I cant see what the issue is??

 

yes as the row gets inserted with valid fields but just one row .

This is what I did. Still no luck. CAN ANY ONE PLEASE HELP ME

 

<?php
include ('database_connection.php');

$mumbai=isset($_POST['checkbox']) ? 1 : 0;
$pune=isset($_POST['checkbox']) ? 1 : 0;
$banglore=isset($_POST['checkbox']) ? 1 : 0;
$mysore=isset($_POST['checkbox']) ? 1 : 0;

if (isset($_POST['formsubmitted'])) {
    $error = array();//Declare An Array to store any error message  
    if (empty($_POST['mobileno'])) {//if no name has been supplied 
        $error[] = 'Please Enter a Mobile Number ';//add to array "error"
    } else {
        $mobile = $_POST['mobileno'];//else assign it a variable
    }
    if (empty($_POST['fname'])) {//if no name has been supplied 
        $error[] = 'Please Enter a First name ';//add to array "error"
    } else {
        $fname = $_POST['fname'];//else assign it a variable
    }
if (empty($_POST['lname'])) {//if no name has been supplied 
        $error[] = 'Please Enter a Last name ';//add to array "error"
    } else {
        $lname = $_POST['lname'];//else assign it a variable
    }
    if (empty($_POST['email'])) {
        $error[] = 'Please Enter your Email ';
    } else {


        if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['email'])) {
           //regular expression for email validation
            $email = $_POST['email'];
        } else {
             $error[] = 'Your EMail Address is invalid  ';
        }


    }


    if (empty($_POST['passwd1'])) {
        $error[] = 'Please Enter Your Password ';
    } else {
        $password = $_POST['passwd1'];
    }
    if (empty($_POST['passwd2'])) {
        $error[] = 'Please Verify Your Password ';
    } else {
        $password = $_POST['passwd2'];
    }

    if (empty($error)) //send to Database if there's no error '

    { //If everything's OK...

        // Make sure the mobile no is available:
        $query_verify_mobileno = "SELECT * FROM userdtls WHERE mobileno = '$mobile'";
        $result_verify_mobileno = mysqli_query($dbc, $query_verify_mobileno);
        if (!$result_verify_mobileno) 
	{//if the Query Failed ,similar to if($result_verify_mobileno==false)
            echo ' Database Error Occured ';
        }

        if (mysqli_num_rows($result_verify_mobileno) == 0) { // IF no previous user is using this number .


            // Create a unique  activation code:
           //$activation = md5(uniqid(rand(), true));


            $query_insert_user = "INSERT INTO userdtls ( `mobileno`, `pass`, `fname`, `lname`, `email`, `MUM`, `PUN`, `BNG`, `MYS` ) VALUES ( '".$mobile."', '".$password."', '".$fname."', '".$lname."', '".$email."', '".$mumbai."', '".$pune."', '".$banglore."', '".$mysore."'  )";
            

            $result_insert_user = mysqli_query($dbc, $query_insert_user);
            if (!$result_insert_user) {
                echo 'Query Failed ';
            }

            if (mysqli_affected_rows($dbc) == 1) { //If the Insert Query was successfull.


                // Send the email:
               /*$message = " To activate your account, please click on this link:\n\n";
                $message .= WEBSITE_URL . '/activate.php?email=' . urlencode($email) . "&key=$activation";
                mail($email, 'Registration Confirmation', $message, 'From: [email protected]'); */

                // Flush the buffered output.


                // Finish the page:
                echo '<div class="success">Thank you for registering! </div>';


            } else { // If it did not run OK.
                echo '<div class="errormsgbox">You could not be registered due to a system error. We apologize for any inconvenience.</div>';
            }

        } else { // The mobile number is not available.
            echo '<div class="errormsgbox" >That mobile number has already been registered.</div>';
        }

    } else {//If the "error" array contains error msg , display them
        
        

echo '<div class="errormsgbox"> <ol>';
        foreach ($error as $key => $values) {
            
            echo '	<li>'.$values.'</li>';


       
        }
        echo '</ol></div>';

    }
  
    mysqli_close($dbc);//Close the DB Connection

} 

check the form

 

<body>
<table width="1000" border="1" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><img src="image/IndianHorsaeRacing.jpg" width="1000" height="169" longdesc="image/IndianHorsaeRacing.jpg"></td>
  </tr>
</table>
<table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><table width="973" border="0" align="right" cellpadding="0" cellspacing="0" class="contentbody">
      <tr>
        <td width="127" valign="top" class="bgnav"> </td>
        <td width="30"> </td>
        <td valign="top"><form name="form1" id="form1" method="post" action="/register.php">
          <table width="800" border="0" cellpadding="0" cellspacing="0" id="profilebox" class="blackbold">
            <tr>
              <td colspan="5" align="center"><?php echo $errmsg; ?></td>
              </tr>

            <tr>
              <td colspan="2" align="center" class="whitebold" bgcolor="#0000FF">SMS Services Registration</td>
              <td colspan="3" align="center" > </td>
              </tr>
            <tr>
              <td width="183">Mobile Number *</td>
              <td colspan="4"><input type="number" id="mobileno" name="mobileno" size="35" maxlength="15" ></td>
            </tr>
            <tr>
              <td>Service Provider *</td>
              <td colspan="4"><select name="country" size="1" style="font-family: Verdana; font-size: 10pt; font-weight: bold; background-color: #E7F7ED; border-style: dotted; border-color: #006600">
           

<?php

$row = 1;
$fp = fopen ("serviceprovider.txt","r");
$country =  $pInfo['country'];
while ($data = fgetcsv ($fp, 2000, ",")) {
    $num = count ($data);
    print "<p> $num fields in line $row: <br>";
    $row++;
   for ($c=0; $c<$num; $c++) {
        print $data[$c] . "<br>";
    } 

if ($country == $data[0]) {
echo "<option selected>$data[0]</option>";
} else {
echo "<option>$data[0]</option>";
} // checks for same
} // end while loop
fclose ($fp);

?> 
		   
		   
		   </select></td>
            </tr>

            <tr>
              <td>Password *</td>
              <td colspan="4"><input name="passwd1" type="password" class="inpttxt" id="passwd" size="35" maxlength="15" onkeyup="noSpace(this);"></td>
            </tr>
            <tr>
              <td>Verify Password *</td>
              <td colspan="4"><input name="passwd2" type="password" class="inpttxt" id="passwd" size="35" maxlength="15" onkeyup="noSpace(this);"></td>
            </tr>
            <tr>
              <td>Email Address *</td>
              <td colspan="4"><input name="email" type="email" class="inpttxt" id="email" size="35" maxlength="50" ></td>
            </tr>
            <tr>
              <td>First Name *</td>
              <td colspan="4"><input name="fname" type="text" class="inpttxt" id="firstname" size="35" maxlength="35" onkeyup="noSpace(this);" ></td>
            </tr>
            <tr>
              <td>Last Name *</td>
              <td colspan="4"><input name="lname" type="text" class="inpttxt" id="lastname" size="35" maxlength="35" onkeyup="noSpace(this);"></td>
            </tr>
            <tr>
              <td>SMS Centers *</td>
              <td colspan="2"><table width="210" border="0" class="greennbold">
                <tr>
                  <td width="22" align="center"><input name="checkbox[]" type="checkbox" class="tickbox" id="mumbai" value="mumbai"></td>
                  <td width="188">MUMBAI</td>
                </tr>
                <tr>
                  <td align="center"><input name="checkbox[]" type="checkbox" class="tickbox" id="pune" value="pune"></td>
                  <td>PUNE</td>
                </tr>
                <tr>
                  <td align="center"><input name="checkbox[]" type="checkbox" class="tickbox" id="banglore" value="banglore"></td>
                  <td>BANGALORE</td>
                </tr>
                <tr>
                  <td align="center"><input name="checkbox[]" type="checkbox" class="tickbox" id="mysore" value="1"></td>
                  <td>MYSORE (August)</td>
                </tr>
              </table></td>
              </tr>
            <tr>
              <td>City / State</td>
              <td colspan="4"><input name="citystate" type="text" class="inpttxt" id="citystate" size="35" maxlength="70" value="<?php echo $pInfo['citystate']; ?>"></td>
            </tr>
           <tr>
              <td> </td>

                  <td colspan="2" align="left"><table width="327" border="0" class="redbold">
                <tr>
                  <td width="22" align="center"><input name="contender[<?=$sno; ?>]" type="checkbox" id="contender[<?=$sno; ?>]" value="<?=$hcode; ?>"></td>
                  <td width="295" align="left"> I agree to the terms of Subscription.</td>
                </tr>
              </table></td>
                </tr>
            <tr>
              <td height="45"> </td>
              <td width="250"><input type="hidden" name="formsubmitted" value="TRUE" />
              <input type="submit" value="Register" /></td>
              <td width="367" colspan="3"> </td>
            </tr>
          </table>
                </form>
        </td>
      </tr>
    </table></td>
  </tr>
</table>
<table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><table width="1000" border="0" align="right" cellpadding="0" cellspacing="0" class="bgfooter">
      <tr>
        <td valign="top"><img src="/images/1_1spcr.gif" alt="" width="1" height="47"><img src="image/EndLogoX40.jpg" width="1000" height="40" longdesc="image/EndLogoX40.jpg"></td>
        </tr>
    </table></td>
  </tr>
</table>
<p> </p>
</body>
</html>

not the issue is different.

 

I can add rows but I get "query failed" when the fname is repeated I mean a duplicate fname.

 

I havent put any such condition but still I get this problem

 

Let me know about this ASAP

 

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.