如何使用 wampserver 在 php 中存储来自用户的数据?

How to store data from user in php using wampserver?

我有一项大学作业,我必须在其中设计一个网站,并且我必须存储用户的详细信息。我进行了 google 搜索,找到了一个适用于我的注册页面的脚本,所以我为我的 index.php 页面复制了该脚本,并在 phpymadmin 中使用创建了一个 table 联系人wampserver 并插入 3 列用户名、电子邮件、消息。但是现在当我尝试输入一些详细信息并单击提交按钮时,页面正在刷新并且数据没有存储在数据库中。

<form role="form" method="post" action="index.php">
                 
 <!-- Name -->
 <div class="row">
  <div class="col-md-6">
   <div class="form-group">
    <input type="text" class="form-control" placeholder="Your name" name="name">
   </div>
  </div>

  <!-- E-Mail -->
  <div class="col-md-6">
   <div class="form-group">
    <input type="email" class="form-control" placeholder="Email address" name="email">
   </div>
  </div>
 </div>

 <!-- Message Area -->
 <div class="form-group">
  <textarea class="form-control" name="message" placeholder="Write you message here..." style="height:232px;"></textarea>
 </div>

 <!-- Subtmit Button -->
 <button type="submit" class="btn btn-send" value="register">
  Send message
 </button>

</form>

<?php

include("contact.php");//make connection here
if(isset($_POST['register']))
{
    $Username=$_POST['name'];//here getting result from the post array after submitting the form.
    $Email=$_POST['email'];//same
    $Message=$_POST['message'];//same


    if($Username=='')
    {
        //javascript use for input checking
        echo"<script>alert('Please enter the name')</script>";
exit();//this use if first is not work then other will not show
    }

    if($Email=='')
    {
        echo"<script>alert('Please enter the email')</script>";
exit();
    }

    if($Message=='')
    {
        echo"<script>alert('Please enter the message')</script>";
    exit();
    }
//here query check weather if user already registered so can't register again.
    $check_email_query="select * from contact WHERE Email='$Email'";
    $run_query=mysqli_query($dbcon,$check_email_query);

    if(mysqli_num_rows($run_query)>0)
    {
echo "<script>alert('Email $user_email is already exist in our database, Please try another one!')</script>";
exit();
    }
//insert the user into the database.
    $insert_user="insert into contact (Username,Email,Message) VALUE ('$Username','$Email','$Message')";
    if(mysqli_query($dbcon,$insert_user))
    {
        echo"<script>alert('Thank you for contacting us')</script>";
    }

}

?>

1) 在<button>中添加name='register'。因为,您正在使用 isset($_POST['register']) 并且不需要 value="register",因为您已经在按钮中声明了 Send Message 作为值。

<button type="submit" class="btn btn-send" name="register">
        Send message
</button>

2) 在此查询中将 VALUE 更改为 VALUES

$insert_user="insert into contact (Username,Email,Message) VALUES ('$Username','$Email','$Message')";