Help on entering values into mysql

Discussion in 'PHP' started by AT-XE, Apr 21, 2008.

  1. #1
    Hello, i have this code:

    <?php
    if ($_POST['fname'] && $_POST['lname'] && $_POST['gender'] && $_POST['bm'] && $_POST['bd'] && $_POST['by'] && $_POST['cty'] && $_POST['cou'] && $_POST['inter'] && $_POST['pcode'] && $_POST['email'] && $_POST['vemail'] && $_POST['password'] && $_POST['password2']) {
    	    if (!function_exists('checkdnsrr')) {
        	function checkdnsrr($host, $type = '') {
        		if(!empty($host)) {
        			if($type == '') $type = "MX";
        			@exec("nslookup -type=$type $host", $output);
        			while(list($k, $line) = each($output)) {
        				if(eregi("^$host", $line)) {
        					return true;
        				}
        			}
        			return false;
        		}
        	}
        }
        function check_email_mx($email) { 
        	if( (preg_match('/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/', $email)) || 
        		(preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/',$email)) ) { 
        		$host = explode('@', $email);
        		if(checkdnsrr($host[1].'.', 'MX') ) return true;
        		if(checkdnsrr($host[1].'.', 'A') ) return true;
        		if(checkdnsrr($host[1].'.', 'CNAME') ) return true;
        	}
        	return false;
        }
      if ($_POST['email'] == $_POST['vemail']) {
      if (check_email_mx($_POST['email'])) {
        if ($_POST['password'] == $_POST['password2']) {
    	 $con = mysql_connect("localhost","root","");
         if (!$con)
          {
          die('Error while connectiong to the database: ' . mysql_error());
          }
         mysql_select_db("db", $con);
    	 $pass = md5(base64_encode($_POST['password']));
    	 $fname_p = mysql_real_escape_string($_POST['fname'], $con);
    	 $lname_p = mysql_real_escape_string($_POST['lname'], $con);
    	 $gender_p = mysql_real_escape_string($_POST['gender'], $con);
    	 $bm_p = mysql_real_escape_string($_POST['bm'], $con);
    	 $bd_p = mysql_real_escape_string($_POST['bd'], $con);
    	 $by_p = mysql_real_escape_string($_POST['by'], $con);
    	 $cty_p = mysql_real_escape_string($_POST['cty'], $con);
    	 $cou_p = mysql_real_escape_string($_POST['cou'], $con);
    	 $inter_p = mysql_real_escape_string($_POST['inter'], $con);
    	 $pcode_p = mysql_real_escape_string($_POST['pcode'], $con);
    	 $email_p = mysql_real_escape_string($_POST['email'], $con);
    	 $password_p = mysql_real_escape_string($pass, $con);
    function uip(){
    	$ipParts = explode(".", $_SERVER["REMOTE_ADDR"]);
    	if ($ipParts[0] == "165" && $ipParts[1] == "21") {    
        	if (getenv("HTTP_CLIENT_IP")) {
            	$ip = getenv("HTTP_CLIENT_IP");
            } elseif (getenv("HTTP_X_FORWARDED_FOR")) {
                $ip = getenv("HTTP_X_FORWARDED_FOR");
            } elseif (getenv("REMOTE_ADDR")) {
                $ip = getenv("REMOTE_ADDR");
            }
        } else {
           return $_SERVER["REMOTE_ADDR"];
       	}
       	return $ip;
    }
    function gen() {
        $length        =    8;
        $num=array("1","2","3","4","5","6","7","8","9");
        $password="";
        srand ((double)microtime()*1000000);
        $max = $length/2;
        for($i=1; $i<=$max; $i++)
        {
        $idt.=$num[rand(0,6)];
        }
    	$id = $idt . "-" . rand(1,1000000);
       $con = mysql_connect("localhost","root","");
       if (!$con)
        {
        die('Error while connectiong to the database: ' . mysql_error());
        }
      mysql_select_db("db", $con);
      $sql = mysql_query("SELECT * FROM users WHERE userid='$id'");
      $result = mysql_num_rows($sql);
      if($result=="0"){
      $reok = $id;
      } else {
       gen();
      }
    }
    gen();
    $userip = uip();
    	 mysql_query("INSERT INTO users (userid, ip, fname, lname, gender, bm, bd, by, cty, cou, inter, pcode, email, password) 
    VALUES ('{$reok}', '{$userip}', '{$fname_p}', '{$lname_p}', '{$gender_p}', '{$bm_p}', '{$bd_p}', '{$by_p}', '{$cty_p}', '{$cou_p}', '{$inter_p}', '{$pcode_p}', '{$email_p}', '{$password_p}')");
          $message = "<b>Thank you for your registration at WebB3V.</b>";
    mysql_close($con);
    	} else {
    	  $message = "<b>Please verify your password again.</b>";
    	}
      } else {
        $message = "<b>Please verify your email again.</b>";
      }
      } else {
        $message = "<b>Please verify your email again.</b>";
      }
    } else {
      $message = "<b>Please fill all the fields correctly.</b>";
    }
    echo $message;
    ?>
    PHP:
    That doesn't work, can someone explain me?

    Thank you,
    AT-XE
     
    AT-XE, Apr 21, 2008 IP
  2. manni

    manni Active Member

    Messages:
    320
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Do you get an error?

    add this to the end of the mysql insert " or die(mysql_error()); " and that should tell you the problem.
     
    manni, Apr 21, 2008 IP
  3. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hmmm ok, I used another script with less values and it works.... I will try later with the full set of values and I will see, thank you!
     
    AT-XE, Apr 21, 2008 IP
  4. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hmmm, with the other script the query was:

    mysql_query("INSERT INTO $table (ip, fname, lname, gender, bm, bd, by, cty, cou, inter, pcode, email, password, username) 
    VALUES ('$userip', '$fname', '$lname', '$gender', '$bm', '$bd', '$by', '$cty', '$cou', '$inter', '$pcode', '$email, '$password', '$username')") or die(mysql_error());
    PHP:
    And the error was:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'by, cty, cou, inter, pcode, email, password, username) VALUES ('127.0.0.1', 'a' at line 1
    Code (markup):
    Thanks in advance...
     
    AT-XE, Apr 22, 2008 IP
  5. manni

    manni Active Member

    Messages:
    320
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Can you show me what you are inserting in the "by, cty,cou etc" field please
     
    manni, Apr 22, 2008 IP
  6. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I will post the whole code here:
    I checked more than 6 times already.....

    <?php
    if ($_POST['fname'] && $_POST['lname'] && $_POST['gender'] && $_POST['bm'] && $_POST['bd'] && $_POST['by'] && $_POST['cty'] && $_POST['cou'] && $_POST['inter'] && $_POST['pcode'] && $_POST['email'] && $_POST['email2'] && $_POST['password'] && $_POST['password2'] && $_POST['username']) {
     if ($_POST['email'] == $_POST['email2']) {
                if (!function_exists('checkdnsrr')) {
            function checkdnsrr($host, $type = '') {
                if(!empty($host)) {
                    if($type == '') $type = "MX";
                    @exec("nslookup -type=$type $host", $output);
                    while(list($k, $line) = each($output)) {
                        if(eregi("^$host", $line)) {
                            return true;
                        }
                    }
                    return false;
                }
            }
        }
        function check_email_mx($email) { 
            if( (preg_match('/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/', $email)) || 
                (preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/',$email)) ) { 
                $host = explode('@', $email);
                if(checkdnsrr($host[1].'.', 'MX') ) return true;
                if(checkdnsrr($host[1].'.', 'A') ) return true;
                if(checkdnsrr($host[1].'.', 'CNAME') ) return true;
            }
            return false;
        }
    	if (check_email_mx($_POST['email'])) {
    	   if ($_POST['password'] == $_POST['password2']) {
    		   $host="localhost";
      $dbusername="username";
      $dbpassword="password";
      $database="database";
      $table="table";
    mysql_connect("$host", "$dbusername", "$dbpassword")or die("Error while connectiong to MySQL.");
    mysql_select_db("$database")or die("Error while selectiong database.");
    $userip = $_SERVER["REMOTE_ADDR"];
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $gender = $_POST['gender'];
    $bm = $_POST['bm'];
    $bd = $_POST['bd'];
    $by = $_POST['by'];
    $cty = $_POST['cty'];
    $cou = $_POST['cou'];
    $inter = $_POST['inter'];
    $pcode = $_POST['pcode'];
    $email = $_POST['email'];
    $password=md5(base64_encode($_POST['password']));
    $username=$_POST['username'];
    $fname = stripslashes($fname);
    $lname = stripslashes($lname);
    $fname = mysql_real_escape_string($fname);
    $lname = mysql_real_escape_string($lname);
    $gender = stripslashes($gender);
    $bm = stripslashes($bm);
    $gender = mysql_real_escape_string($gender);
    $bm = mysql_real_escape_string($bm);
    $bd = stripslashes($bd);
    $by = stripslashes($by);
    $bd = mysql_real_escape_string($bd);
    $by = mysql_real_escape_string($by);
    $cty = stripslashes($cty);
    $cou = stripslashes($cou);
    $cty = mysql_real_escape_string($cty);
    $cou = mysql_real_escape_string($cou);
    $inter = stripslashes($inter);
    $pcode = stripslashes($pcode);
    $inter = mysql_real_escape_string($inter);
    $pcode = mysql_real_escape_string($pcode);
    $email = stripslashes($email);
    $email = mysql_real_escape_string($email);
    $username = stripslashes($username);
    $password = stripslashes($password);
    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);
    mysql_query("INSERT INTO $table (ip, fname, lname, gender, bm, bd, by, cty, cou, inter, pcode, email, password, username) 
    VALUES ('$userip', '$fname', '$lname', '$gender', '$bm', '$bd', '$by', '$cty', '$cou', '$inter', '$pcode', '$email', '$password', '$username')") or die(mysql_error());
    $message = 'Thank you for registering.';
         } else {
         $message = "The passwords you entered are not the same.";
         }
    	} else {
    	$message = "Please enter a vaild email.";
    	}
     } else {
       $message = "The emails you entered are not the same.";
       }
    echo $message;
    } else {
    ?>
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <form action="register.php" method="post">
    <td>
    <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
    <tr>
    <td colspan="3"><strong>Register</strong></td>
    </tr>
    <tr>
    <td width="100">First Name</td>
    <td width="6">:</td>
    <td width="294"><input name="fname" type="text" /></td>
    </tr>
    <tr>
    <td>Last Name</td>
    <td>:</td>
    <td><input name="lname" type="text" /></td>
    </tr>
    <tr>
    <td>Gender</td>
    <td>:</td>
    <td><select name="gender">
    <option selected="selected">Male</option>
    <option>Female</option>
    </select></td>
    </tr>
    <tr>
    <td>You born in</td>
    <td>:</td>
    <td><input name="by" type="text"></td>
    <td><select name="bm">
    	<option value="1" selected="selected">January</option>
    	<option value="2" >February</option>
    	<option value="3" >March</option>
    	<option value="4" >April</option>
    	<option value="5" >May</option>
    	<option value="6" >June</option>
    	<option value="7" >July</option>
    	<option value="8" >August</option>
    	<option value="9" >September</option>
    	<option value="10" >October</option>
    	<option value="11" >November</option>
    	<option value="12" >December</option></select></td>
    <td><input name="bd" type="text"></td>
    </tr>
    <tr>
    <td>Country</td>
    <td>:</td>
    <td><select name="cou">
    	<option selected="selected">Afghanistan</option>
    	<option>Aland Islands</option>
    	<option>Albania</option>
    	<option>Algeria</option>
    	<option>American Samoa</option>
    	<option>Andorra</option>
    	<option>Angola</option>
    	<option>Anguilla</option>
    	<option>Antarctica</option>
    	<option>Antigua and Barbuda</option>
    	<option>Argentina</option>
    	<option>Armenia</option>
    	<option>Aruba</option>
    	<option>Australia</option>
    	<option>Austria</option>
    	<option>Azerbaijan</option>
    	<option>Bahamas</option>
    	<option>Bahrain</option>
    	<option>Bangladesh</option>
    	<option>Barbados</option>
    	<option>Belarus</option>
    	<option>Belgium</option>
    	<option>Belize</option>
    	<option>Benin</option>
    	<option>Bermuda</option>
    	<option>Bhutan</option>
    	<option>Bolivia</option>
    	<option>Bosnia and Herzegovina</option>
    	<option>Botswana</option>
    	<option>Bouvet Island</option>
    	<option>Brazil</option>
    	<option>British Indian Ocean Territory</option>
    	<option>British Virgin Islands</option>
    	<option>Brunei</option>
    	<option>Bulgaria</option>
    	<option>Burkina Faso</option>
    	<option>Burundi</option>
    	<option>Cambodia</option>
    	<option>Cameroon</option>
    	<option>Canada</option>
    	<option >Cape Verde</option>
    	<option>Cayman Islands</option>
    	<option>Central African Republic</option>
    	<option>Chad</option>
    	<option>Chile</option>
    	<option>China</option>
    	<option>Christmas Island</option>
    	<option>Cocos (Keeling) Islands</option>
    	<option>Colombia</option>
    	<option>Comoros</option>
    	<option>Congo</option>
    	<option>Cook Islands</option>
    	<option>Costa Rica</option>
    	<option>Croatia</option>
    	<option>Cyprus</option>
    	<option>Czech Republic</option>
    	<option>Democratic Republic of Congo</option>
    	<option>Denmark</option>
    	<option>Disputed Territory</option>
    	<option>Djibouti</option>
    	<option>Dominica</option>
    	<option>Dominican Republic</option>
    	<option>East Timor</option>
    	<option>Ecuador</option>
    	<option>Egypt</option>
    	<option>El Salvador</option>
    	<option>Equatorial Guinea</option>
    	<option>Eritrea</option>
    	<option>Estonia</option>
    	<option>Ethiopia</option>
    	<option>Falkland Islands</option>
    	<option>Faroe Islands</option>
    	<option>Federated States of Micronesia</option>
    	<option>Fiji</option>
    	<option>Finland</option>
    	<option>France</option>
    	<option>French Guyana</option>
    	<option>French Polynesia</option>
    	<option>French Southern Territories</option>
    	<option>Gabon</option>
    	<option>Gambia</option>
    	<option>Georgia</option>
    	<option>Germany</option>
    	<option>Ghana</option>
    	<option>Gibraltar</option>
    	<option>Greece</option>
    	<option>Greenland</option>
    	<option>Grenada</option>
    	<option>Guadeloupe</option>
    	<option>Guam</option>
    	<option>Guatemala</option>
    	<option>Guinea</option>
    	<option>Guinea-Bissau</option>
    	<option>Guyana</option>
    	<option>Haiti</option>
    	<option>Heard Island and Mcdonald Islands</option>
    	<option>Honduras</option>
    	<option>Hong Kong</option>
    	<option>Hungary</option>
    	<option>Iceland</option>
    	<option>India</option>
    	<option>Indonesia</option>
    	<option>Iraq</option>
    	<option>Iraq-Saudi Arabia Neutral Zone</option>
    	<option>Ireland</option>
    	<option>Israel</option>
    	<option>Italy</option>
    	<option>Ivory Coast</option>
    	<option>Jamaica</option>
    	<option>Japan</option>
    	<option>Jordan</option>
    	<option>Kazakhstan</option>
    	<option>Kenya</option>
    	<option>Kiribati</option>
    	<option>Kuwait</option>
    	<option>Kyrgyzstan</option>
    	<option>Laos</option>
    	<option>Latvia</option>
    	<option>Lebanon</option>
    	<option>Lesotho</option>
    	<option>Liberia</option>
    	<option>Libya</option>
    	<option>Liechtenstein</option>
    	<option>Lithuania</option>
    	<option>Luxembourg</option>
    	<option>Macau</option>
    	<option>Macedonia</option>
    	<option>Madagascar</option>
    	<option>Malawi</option>
    	<option>Malaysia</option>
    	<option>Maldives</option>
    	<option>Mali</option>
    	<option>Malta</option>
    	<option>Marshall Islands</option>
    	<option>Martinique</option>
    	<option>Mauritania</option>
    	<option>Mauritius</option>
    	<option>Mayotte</option>
    	<option>Mexico</option>
    	<option>Moldova</option>
    	<option>Monaco</option>
    	<option>Mongolia</option>
    	<option>Montserrat</option>
    	<option>Morocco</option>
    	<option>Mozambique</option>
    	<option>Myanmar</option>
    	<option>Namibia</option>
    	<option>Nauru</option>
    	<option>Nepal</option>
    	<option>Netherlands</option>
    	<option>Netherlands Antilles</option>
    	<option>New Caledonia</option>
    	<option>New Zealand</option>
    	<option>Nicaragua</option>
    	<option>Niger</option>
    	<option>Nigeria</option>
    	<option>Niue</option>
    	<option>Norfolk Island</option>
    	<option>North Korea</option>
    	<option>Northern Mariana Islands</option>
    	<option>Norway</option>
    	<option>Oman</option>
    	<option>Pakistan</option>
    	<option>Palau</option>
    	<option>Palestinian Occupied Territories</option>
    	<option>Panama</option>
    	<option>Papua New Guinea</option>
    	<option>Paraguay</option>
    	<option>Peru</option>
    	<option>Philippines</option>
    	<option>Pitcairn Islands</option>
    	<option>Poland</option>
    	<option>Portugal</option>
    	<option>Puerto Rico</option>
    	<option>Qatar</option>
    	<option>Reunion</option>
    	<option>Romania</option>
    	<option>Russia</option>
    	<option>Rwanda</option>
    	<option>Saint Helena and Dependencies</option>
    	<option>Saint Kitts and Nevis</option>
    	<option>Saint Lucia</option>
    	<option>Saint Pierre and Miquelon</option>
    	<option>Saint Vincent and the Grenadines</option>
    	<option>Samoa</option>
    	<option>San Marino</option>
    	<option>Sao Tome and Principe</option>
    	<option>Saudi Arabia</option>
    	<option>Senegal</option>
    	<option>Serbia and Montenegro</option>
    	<option>Seychelles</option>
    	<option>Sierra Leone</option>
    	<option>Singapore</option>
    	<option>Slovakia</option>
    	<option>Slovenia</option>
    	<option>Solomon Islands</option>
    	<option>Somalia</option>
    	<option>South Africa</option>
    	<option>South Georgia and South Sandwich Islands</option>
    	<option>South Korea</option>
    	<option>Spain</option>
    	<option>Spratly Islands</option>
    	<option>Sri Lanka</option>
    	<option>Suriname</option>
    	<option>Svalbard and Jan Mayen</option>
    	<option>Swaziland</option>
    	<option>Sweden</option>
    	<option>Switzerland</option>
    	<option>Syria</option>
    	<option>Taiwan</option>
    	<option>Tajikistan</option>
    	<option>Tanzania</option>
    	<option>Thailand</option>
    	<option>Togo</option>
    	<option>Tokelau</option>
    	<option>Tonga</option>
    	<option>Trinidad and Tobago</option>
    	<option>Tunisia</option>
    	<option>Turkey</option>
    	<option>Turkmenistan</option>
    	<option>Turks And Caicos Islands</option>
    	<option>Tuvalu</option>
    	<option>Uganda</option>
    	<option>Ukraine</option>
    	<option>United Arab Emirates</option>
    	<option>United Kingdom</option>
    	<option>United Nations Neutral Zone</option>
    	<option>United States</option>
    	<option>United States Minor Outlying Islands</option>
    	<option>Uruguay</option>
    	<option>US Virgin Islands</option>
    	<option>Uzbekistan</option>
    	<option>Vanuatu</option>
    	<option>Vatican City</option>
    	<option>Venezuela</option>
    	<option>Vietnam</option>
    	<option>Wallis and Futuna</option>
    	<option>Western Sahara</option>
    	<option>Yemen</option>
    	<option>Zambia</option>
    	<option>Zimbabwe</option></select>
    </td>
    </tr>
    <tr>
    <td>City</td>
    <td>:</td>
    <td><input name="cty" type="text" /></td>
    </tr>
    <tr>
    <td>Interests:</td>
    <td>:</td>
    <td><textarea name="inter" cols="40" rows="5"></textarea></td>
    </tr>
    <tr>
    <td>Postal Code</td>
    <td>:</td>
    <td><input name="pcode" type="text" /></td>
    </tr>
    <td>Username</td>
    <td>:</td>
    <td><input name="username" type="text" /></td>
    </tr>
    <tr>
    <td>Password</td>
    <td>:</td>
    <td><input name="password" type="password" /></td>
    </tr>
    <tr>
    <td>Confirm Password</td>
    <td>:</td>
    <td><input name="password2" type="password" /></td>
    </tr>
    <tr>
    <td>E-mail</td>
    <td>:</td>
    <td><input name="email" type="text" /></td>
    </tr>
    <tr>
    <td>Confirm E-mail</td>
    <td>:</td>
    <td><input name="email2" type="text" /></td>
    </tr>
    <tr>
    <td><input type="submit" name="submit" value="Register" /></td>
    </tr>
    </table>
    </td>
    </form>
    </tr>
    </table>
    <?php
    }
    ?>
    PHP:
     
    AT-XE, Apr 23, 2008 IP
  7. manni

    manni Active Member

    Messages:
    320
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #7
    I think that you should do some debugging, check if all the data is getting posted through in the $cy, $by values, check that the whole code is working and hopefully you should find an error.
     
    manni, Apr 23, 2008 IP