douluchuo0801 2015-11-17 02:08
浏览 12
已采纳

使用PHP将数据传递到数据库[关闭]

I am new to programming.

Today, I started learning some mysqli so I can make a login form for my "practising site", but I have a problem. If you check down the code you will understand what I want to do.

So any help? Because this doesn't work. It doesn't pass the data I enter in the form. And, is that a correct way to do it and if no which way is more professional? Thanks in advance.

<form action = "<?php $_PHP_SELF ?>" method ="POST">
 Username: <input type ="text" name = "username"/> </br>
 Password: <input type ="password" name = "password"/> </br>
 Email: <input type = "text" name = "email"/> </br>
 <input type = "button" value = "Submit"/> 
</form>

<?php

ini_set('display_errors', '1');

   $dbhost = 'localhost';
   $dbuser = 'root';
   $dbpass = ''; 
   $dbname = 'dbtesting';
   $username = $_POST["username"];
   $password = $_POST['passsword'];
   $email = $_POST['email'];

   $query = "INSERT INTO mywebpageusers (username, password, email)
             VALUES ('$username', '$password', '$email')";

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

   if($conn->connect_error > 0)
   {
    die ('Could not connect to database [' . $conn->connect_error . ']');
   }
   echo 'Connected succesfully!!';

  $conn->query($query);

?>
  • 写回答

2条回答 默认 最新

  • dqz30992 2015-11-17 02:44
    关注

    You have some errors:

    1. Forgot the function "echo"

    2. $_PHP_SELF is not a PHP variable, it should be $_SERVER['PHP_SELF'], it's better if you use $_SERVER['SCRIPT_NAME'].

    3. Wrong submit button's type: type="submit"

    4. "$conn->connect_error" is a string, when you compared with 0, it will become 0 => (0 > 0) will return false, so the function "die" never execute

       <form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method ="POST">
       Username: <input type ="text" name = "username"/> </br>
       Password: <input type ="password" name = "password"/> </br>
       Email: <input type = "text" name = "email"/> </br>
       <input type = "submit" value = "Submit"/> 
      </form>
      
      <?php
      
      ini_set('display_errors', '1');
      
         $dbhost = 'localhost';
         $dbuser = 'root';
         $dbpass = '';
         $dbname = 'dbtesting';
         $username = $_POST['username'];
         $password = $_POST['passsword'];
         $email = $_POST['email'];
      
         $query = "INSERT INTO mywebpageusers (username, password, email)
                   VALUES ('$username', '$password', '$email')";
      
         $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
         var_dump($conn->connect_error); //You can debug variables by var_dump function
         if ($conn->connect_error) {
             die('Could not connect to database ['.$conn->connect_error.']');
         }
         echo 'Connected succesfully!!';
      
        $conn->query($query);
      
      ?>
      

    Hope this help. This code is enough for practise.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站