保存到MySQL的数据为VarChar为1,Int为0

时间:2015-01-28 17:44:22

标签: php html mysql

我正在尝试为我的学期项目创建类似于Facebook的东西,并遇到一些问题。

当我通过表单发布值并尝试将其保存在我的数据库中时,存储在数据库中的数据对于VarChar数据类型为1,对于Int数据类型为0。

我无法找到错误。我的HTML和PHP代码如下:

<?php
$db_host="localhost";
$db_username="root";
$db_password="vertrigo";
$db_name="php_db";

mysql_connect($db_host,$db_username,$db_password) or die("Couldn't connnect  to the database");
mysql_select_db("php_db") or die("Couldn't connnect to the database");

?>

<form name="reg" action="signup.php"  method="post">

    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <table width="440" border="0" align="center" cellpadding="2"   cellspacing="0">
    <tr>
    <td><div align="left">First Name:</div></td>
    <td><input type="text" name="fname" /></td>
    </tr>
    <tr>
    <td><div align="left">Last Name:</div></td>
    <td><input type="text" name="lname" /></td>
    </tr>

    <tr>
    <td><div align="left">Address:</div></td>
    <td><input type="text" name="address" /></td>
    </tr>
    <tr>
    <td><div align="left">Country:  </div></td>
    <td><select name="country" >
    <option value="Select Country:"> Select Country: </option>
    <option value="United States">United States</option>
    <option value="United Kingdom">United Kingdom</option>
    <option value="Afghanistan">Afghanistan</option>
    <option value="Albania">Albania</option>
    <option value="Algeria">Algeria</option>
    <option value="American Samoa">American Samoa</option>
    <option value="Andorra">Andorra</option>
    <option value="Angola">Angola</option>
    <option value="Anguilla">Anguilla</option>
    <option value="Antarctica">Antarctica</option>
    <option value="Antigua and Barbuda">Antigua and Barbuda</option>
    <option value="Argentina">Argentina</option>
    <option value="Armenia">Armenia</option>
    <option value="Aruba">Aruba</option>
    <option value="Australia">Australia</option>
    <option value="Austria">Austria</option>
    <option value="Azerbaijan">Azerbaijan</option>
    <option value="Bahamas">Bahamas</option>
    <option value="Bahrain">Bahrain</option>
    <option value="Bangladesh">Bangladesh</option>
    <option value="Barbados">Barbados</option>
    <option value="Belarus">Belarus</option>
    <option value="Belgium">Belgium</option>
    <option value="Belize">Belize</option>
    <option value="Benin">Benin</option>
    <option value="Bermuda">Bermuda</option>
    <option value="Bhutan">Bhutan</option>
    <option value="Bolivia">Bolivia</option>
    <option value="Bosnia and Herzegovina">Bosnia and Herzegovina</option>
    <option value="Botswana">Botswana</option>
    <option value="Bouvet Island">Bouvet Island</option>
    <option value="Brazil">Brazil</option>
    <option value="British Indian Ocean Territory">British Indian Ocean       Territory</option>
    <option value="Brunei Darussalam">Brunei Darussalam</option>
    <option value="Bulgaria">Bulgaria</option>

    </select>
    <div align="left"></tr>
    <tr>
    <td><div align="left">Contact No.:</div></td>
    <td><input type="text" name="contact" /></td>
    </tr>
    <tr>
    <td><div align="left">  Your Email:  </div> </td>
    <td>    <div align="left"> <input type="text" name="email"  size="25" class="inputbox" /> </div></td>
    </tr>
    <tr>
    <td><div align="left">  Re-enter Email:  </div>  </td>
    <td><div align="left"> <input type="text" name="remail"  size="25" class="inputbox" /> </div></td> 
    </tr>
    <tr>    
    <td><div align="left"> New Password:  </div> </td>
    <td><div align="left"> <input type="password" name="password" size="25"   class="inputbox" /> </div></td>
    </tr>
    <tr>
    <td><div align="left">Gender:  </div></td>
    <td><div align="left">
    <select name="gender" >
    <option value="Select Gender:"> Select Gender: </option>
    <option value="Female"> Female </option>
    <option value="Male"> Male </option>
    </select>
    </div></td>       
    <td>&nbsp;</td>

</tr>

<tr>
<td><div align="right"></div></td>
    <td><input name="submit" type="submit" value="Submit" /></td>
    </tr>
    </table>
    </form>
    </div>

    <?php
    include 'connection.php';
if (isset($_POST['submit'])){


    $first_name = isset( $_POST['fname']);
    $last_name = isset($_POST['lname']);
    $address = isset($_POST['address']);
    $country = isset($_POST['country']);
    $contact = isset($_POST['contact']);  
    $email = isset($_POST['email']); 
    $remail = isset($_POST['remail']); 
    $pw = isset($_POST['password']); 
    $gender = isset($_POST['gender']); 



$query=  mysql_query(" INSERT INTO fb_sign (fname, lname,  address,country,     contact, email,
    remail, password, gender) VALUES('$first_name', '$last_name',   '$address'  , '$country', 'contact', '$email', '$remail', '$pw', '$gender')");

    if(! $query )
        {
            die('Could not enter data: ' . mysql_error());
        }
    else
        { 
            echo "Registered successfully!\n";
        }
}
?>

1 个答案:

答案 0 :(得分:3)

您要将所有变量设置为isset($_POST['XXX'])的结果,truefalse,而不是输入字段本身的值。所以

$first_name = isset($_POST['fname']);

应该是:

$first_name = isset($_POST['fname']) ? $_POST['fname'] : '';

和所有其他变量类似。