0

我正在尝试将“$phone”存储在我的 SQL 数据库中,但是如果我在 phpmyadmin 中将 COLUMN 类型设置为“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;?

4

2 回答 2

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 个数字并且不少于,我想这会转换为高于 INT 可以显示的值。

我在 phpmyadmin 中选择了我的 INT(40),但是这似乎不是实际的字符长度,而是某种形式的位表示(我将继续阅读!)

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

谢谢大家!

于 2018-08-31T11:16:35.723 回答
0

您必须对其进行强制转换才能将其用作 int。

$phone = (int) $phone.

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

于 2018-08-31T11:16:48.350 回答