我试图将“$phone”存储在我的sql数据库中,但是如果在phpmyadmin中将列类型设置为“int”,则不会输入任何数据。一旦我将coulmn类型更改为“varchar”,就可以推送数据
在form.php中:
<fieldset>
<input placeholder="Your Phone Number" type="text" name="phone" value="<?= $phone ?>" tabindex="4">
<span class="error"><?= $phone_error ?></span>
</fieldset>
$phone在formprocess.php中声明:
$fname_error = $lname_error = $email_error = $phone_error = $job_error = $nameauth_error = $privacy_error = $decline_error = "";
$fname = $lname = $email = $phone = $message = $jobtitle = $nameauth = $success = $privacy = "";
if (empty($_POST["phone"])) {
$phone_error = "Phone is required";
} else {
$phone = test_input($_POST["phone"]);
// check if e-mail address is well-formed
if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}
[\s-]?\d{4}$/i",$phone)) {
$phone_error = "Invalid phone number";
}
}`
if ($privacy == 'agree') {
if ($fname_error == '' and $lname_error == '' and $email_error == '' and $phone_error == '' and $privacy_error == '' and $nameauth_error == '' and $job_error == '')
{
$sql = "INSERT INTO data (phoneNo) VALUES ('$phone');";
mysqli_query($con, $sql);
$success = "Enjoy your game!!";
$fname = $lname = $email = $phone = $message = $jobtitle = $nameauth = "";
}
} else if ($privacy == '') {
$privacy_error = "Please Accept or Decline Our Policy";
} else if ($privacy == 'disagree') {
$decline_error = "Please Accept Our Privacy Policy to Continue";
}
}
如果phpmyadmin中的列数据类型是varchar,那么这段代码可以很好地工作,但是如果我使用int,代码就会中断
这与我将变量初始化为“”这使它成为varchar有关吗?
是否必须将int值初始化为=0?
2条答案
按热度按时间k4aesqcs1#
嗨,我用以下代码成功地解决了这个问题,它现在可以工作了:
if ($fname_error == '' and $lname_error == '' and $email_error == '' and $phone_error == '' and $privacy_error == '' and $nameauth_error == '' and $job_error == '') { $intphone = (int)$phone; $sql = "INSERT INTO data (phoneNo) VALUES ('$intphone');"; mysqli_query($con, $sql); $success = "Enjoy your game!!"; $fname = $lname = $email = $phone = $message = $jobtitle = $nameauth = "";
我得到$phone的值,并通过以下方式将其转换为int:$intphone = (int)$phone;
假设我的数据库中的电话号码是:99999999999,我得到的是:2147483647我认为这是int字段可以显示的最高值,因为我的电话号码中的过滤只允许11个号码,而且不能少于11个号码,所以我猜这会转换成比int可以显示的更高的值。
我在phpmyadmin中选择了int(40),但是这似乎不是实际的字符长度,而是某种形式的位表示(我将继续阅读!)
谢谢大家,如果有任何方向的指示,你可以推荐我来读!
谢谢大家!
afdcj2ne2#
你必须将它转换为int。
此外,电话号码总是字符串,因为它们可能有“+”或以“0”开头。