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

时间:2016-02-12 10:03:39

标签: php mysql wampserver

我有一个大学作业,我必须设计一个网站,我必须存储用户的详细信息。我做了一个谷歌搜索,我找到了一个适用于我的注册页面的脚本所以我复制了我的index.php页面的脚本,我在phpymadmin中使用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>
&#13;
&#13;
&#13;

<?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 个答案:

答案 0 :(得分:0)

1)在name='register'中添加<button>。因为,您使用的是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')";