使用 PHP/phpmyadmin 将整数插入 SQL



我正在尝试将"$phone"存储在我的SQL数据库中,但是如果我在phpmyadmin中将COLUMN类型设置为"INT",则不会输入任何数据。 一旦我将 COULMN 类型更改为"VARCHAR",就可以推送数据

在形式上.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; 吗?

你必须强制转换它才能将其用作 int。

$phone = (int) $phone.

此外,电话号码始终是字符串,因为它们可能具有"+"或以"0"开头。

嗨,我设法使用以下代码解决了它,它现在可以工作了: 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(,但是似乎这不是实际的字符长度,而是某种形式的位表示(我将阅读!

谢谢大家,如果有任何方向的指示,你可以推荐我阅读!

谢谢大家!

在你的phpmyadmin中,尝试将你的数据类型更改为longtext.在此处输入图像描述

最新更新