即使发送不同的值,也会在数据库中输入相同的值

时间:2017-06-21 08:01:08

标签: php html mysql sql mysqli

我在php上制作一个表格,将数据保存到数据库中 这是我的代码,我将数据从PHP表单发送到数据库:

<?php
$users_name = $_POST['name'];
$users_email = $_POST['email'];
$users_phoneNumber = $_POST['phNo'];
$users_cnic = $_POST['cnic'];
$users_voucherNumber = $_POST['vNum'];
$users_modelNumber = $_POST['prod'];


if( $_POST )
        {

          $dbhost = "localhost";
          $dbuser = "root";
          $dbpassword = "";
          $dbname = "dany";
          $query = "";
          $sqlQuery = ""; 


          $conn = new mysqli($dbhost, $dbuser, $dbpassword, $dbname);

          if ($conn->connect_error) {
              die("Connection failed: " . $conn->connect_error);
          } 


          echo ("Variables values: " . " "  . $users_cnic . " " . 
                          $users_phoneNumber . "<br>");
          $query = "INSERT INTO customer( c_name, c_phoneNumber, c_cnic, 
                    c_email, c_voucherNumber, c_modelNumber) VALUES 
                     ('$users_name','$users_phoneNumber', '$users_cnic', 
                    '$users_email', $users_voucherNumber, 
                       '$users_modelNumber')";

          if ($conn->query($query) === TRUE) {
              //if successful
            echo ("Variables values after success: " . " "  . $users_cnic . 
                   " " . $users_phoneNumber . "<br>");
          } else {
              //if data not successfully entered
              echo "Error: " . $query . "<br>" . $conn->error;
          }

          $conn->close();

        }

?>

但问题是当我尝试发送数据时,&#34; c_phoneNumber&#34;和&#34; c_cnic&#34;每次发送的时间是&#34; 2147483647&#34;。即表中的每一行都有两列,每列都有值&#34; 2147483647&#34;。

如果你看到我确定的行,如果它们是正确的,则检查这些值:

echo ("Variables values: " . " "  . $users_cnic . " " . $users_phoneNumber . 
      "<br>");

这里它们的值与从表单的文本框发送的值完全相同。但是当在下一行代码中进行查询时:

$query = "INSERT INTO customer( c_name, c_phoneNumber, c_cnic, 
                    c_email, c_voucherNumber, c_modelNumber) VALUES 
                     ('$users_name','$users_phoneNumber', '$users_cnic', 
                    '$users_email', $users_voucherNumber, 
                       '$users_modelNumber')";

这两个变量都包含&#34; 2147483647&#34;。

当成功将数据输入表格后if语句运行时:

if ($conn->query($query) === TRUE) {
              //if successful
            echo ("Variables values after success: " . " "  . $users_cnic . 
                   " " . $users_phoneNumber . "<br>");
          }

它具有从文本框传递的相同值。 我无法弄清楚在整个程序运行状态下变量数据保持不变但插入数据库时​​更改的问题。

我试过了:

1-创建新表

2-创建两个新列

3-发送硬编码值。

 $query = "INSERT INTO customer( c_name, c_phoneNumber, c_cnic, 
                    c_email, c_voucherNumber, c_modelNumber) VALUES 
                     ('$users_name',123, 456, 
                    '$users_email', $users_voucherNumber, 
                       '$users_modelNumber')";

当我执行第4步时,值完全发送并插入123,456。

4-创建一个新查询,将其保存在另一个变量中,运行查询=相同结果。(错误)

2 个答案:

答案 0 :(得分:0)

2147483647是MySQL中INT字段的最大(有符号)值。任何大于该数字的数字都将重置为该最大值。见https://dev.mysql.com/doc/refman/5.7/en/integer-types.html。如果您需要存储更大的数字,可以使用BIGINT类型。

答案 1 :(得分:-1)

试试这位兄弟希望它会帮助你

<?php
$users_name = $_POST['name'];
$users_email = $_POST['email'];
$users_phoneNumber = $_POST['phNo'];
$users_cnic = $_POST['cnic'];
$users_voucherNumber = $_POST['vNum'];
$users_modelNumber = $_POST['prod'];

if(!empty($_POST['name']))
{
          $dbHost = "localhost";
          $dbUsername = "nextgbsl_pay";
          $dbPassword = "Welive@2017";
          $dbName = "nextgbsl_tbl_payment";



          $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
           //Display error if failed to connect
       if ($db->connect_errno) {
           printf("Connect failed: %s\n", $db->connect_error);
             exit();
                   } 

echo ("Variables values: " . " "  . $users_cnic . " " . 
                          $users_phoneNumber . "<br>");
            $query =  $db->query("INSERT INTO customer( c_name, c_phoneNumber, c_cnic, 
                    c_email, c_voucherNumber, c_modelNumber) VALUES 
                     ('".$users_name."','".$users_phoneNumber."', '".$users_cnic."', 
                    '".$users_email."', '".$users_voucherNumber."', 
                       '".$users_modelNumber."')";);


          if ($conn->query($query) === TRUE) {
              //if successful
            echo ("Variables values after success: " . " "  . $users_cnic . 
                   " " . $users_phoneNumber . "<br>");
          } else {
              //if data not successfully entered
              echo "Error: " . $query . "<br>" . $conn->error;
          }

          mysqli_close($con);


}

?>