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


How to store data from user in php using wampserver?

我有一个大学作业,我必须设计一个网站,我必须存储用户的详细信息。我做了一个谷歌搜索,我找到了一个脚本,它适用于我的注册页面,所以我为我的索引.php页面复制了该脚本,我使用 wampserver 在 phpymadmin 中创建了一个表联系人并插入了 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')";