简体   繁体   English

php mysql使用注册表格插入mysql

[英]php mysql INSERT INTO mysql by using register form

I am php beginner and I am trying to make e-commerce by using php. 我是php的初学者,我正尝试使用php进行电子商务。 I am trying to make register form and I want to save these data into mysql server. 我正在尝试制作注册表,我想将这些数据保存到mysql服务器中。 The coding looks like OK, but the data did not store in mysql server. 编码看起来不错,但是数据没有存储在mysql服务器中。 Could you give your answer for this? 你能给出答案吗? php language is first time that it is what I am struggled. php语言是我第一次感到挣扎。 Please give some advice. 请给一些建议。 Thanks. 谢谢。

--registerForm.php-- --registerForm.php--

<h4>Create a new account</h4>
                <div class="box">
                <form action="register.php" method="post">
                    <p>User ID: <input type="text" name="userId" size="30"/>*</p>
                    <p>Password: <input type="password" name="password" size="30"/>*</p>
                    <p>Retype Password: <input type="password" name="repassword" size="30"/>*</p>
                    <p>First Name: <input type="text" name="firstName" size="30"/>*</p>
                    <p>Last Name: <input type="text" name="lastName" size="30"/>*</p>
                    <p>Your Address (*):</p>
                    <p> <textarea name="address" rows="5" cols="30"></textarea></p>
                    <p>Phone: <input type="text" name="phone" size="20"/>*</p>
                    <p>E-mail: <input type="text" name="email" size="21"/>*</p>
                    <p><input type="submit" value="Create Account"/></p>
                </form>
                </div>

--register.php-- --register.php--

<?php
require "sql_connection.php";
if(isset($_POST['submit']))
{
if($_GET["userId"]==$_GET["repassword"]){
mysql_query("insert into customer (userId, password, firstName, lastName, address,
phone, email) 
values
  ('$_GET[userId]','$_GET[password]','$_GET[firstName]','$_GET[lastName]','$_GET[address]','$_GET[phone]','$_GET[email]')")
or die(mysql_error());
}
echo "Done!!!!";
}
?>

--sql_connection.php-- --sql_connection.php--

<?php
$db_host = "localhost";
$db_username = "root";
$db_pass = "**MY_PASS**";
$db_name = "**MY_DB**";

@mysql_connect("$db_host", "$db_username", "$db_pass", "$db_name") or die("connection is fail.");
@mysql_select_db("$db_name") or die("database does not exsist.");
echo "Successfully connection!!";
?>
if($_GET["userId"]==$_GET["repassword"])

Why do you compare userid to a retype pssword field? 为什么将userid与重新输入pssword字段进行比较?

I think it should be : 我认为应该是:

if($_GET["password"]==$_GET["repassword"])

Also make sure you escape strings to prevent SQL Injection Attacks. 还要确保转义字符串以防止SQL注入攻击。

http://php.net/manual/en/function.mysql-real-escape-string.php http://php.net/manual/zh/function.mysql-real-escape-string.php

And Like Paul said, to correctly retrieve the data use $_POST 就像保罗所说,要正确检索数据,请使用$ _POST

Few things. 一些事情。 Your $_GET and $_POST are mixed up. 您的$_GET$_POST混在一起了。 and NEVER post your db_pass and uername in public. 并且永远不要公开发布您的db_pass和uername。 Also, you're suppressing errors using @ . 另外,您可以使用@抑制错误。 don't do that. 不要那样做

ie

if($_GET["userId"]==$_GET["repassword"]){

should be 应该

if($_POST["userId"]==$_POST["repassword"]){

and changes all these to $_POST 并将所有这些更改为$_POST

Your code: 您的代码:

$_GET[userId]','$_GET[password]','$_GET[firstName]','$_GET[lastName]','$_GET[address]','$_GET[phone]','$_GET[email]')

Should be: 应该:

$_POST[userId]','$_POST[password]','$_POST[firstName]','$_POST[lastName]','$_POST[address]','$_POST[phone]','$_POST[email]')"

As your form method defined is POST so use $_POST to get values after submit instead of $_GET 由于您定义的表单方法是POST因此请使用$_POST在提交后获取值,而不是$_GET

require "sql_connection.php";
if(isset($_POST['submit']))
{
if($_POST["userId"]==$_POST["repassword"]){
mysql_query("insert into customer (userId, password, firstName, lastName, address,
phone, email) 
values
  ('$_POST[userId]','$_POST[password]','$_POST[firstName]','$_POST[lastName]','$_POST[address]','$_POST[phone]','$_POST[email]')")
or die(mysql_error());
}
echo "Done!!!!";
}
?>

Values are not quoted properly. 值未正确引用。 You should quote then before insert. 在插入之前,您应该先引用。

mysql_query("insert into customer (userId, password, firstName, lastName, address,
phone, email) 
values
  ('".$_POST[userId]."','".$_POST[password]."','".$_POST[firstName]."','".$_POST[lastName]."','".$_POST[address]."','".$_POST[phone]."','".$_POST[email]."')")

我认为您正在尝试做的是:

if($_GET["password"]==$_GET["repassword"]) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM