数据未插入到mysql数据库中,但代码运行时没有错误

时间:2016-08-25 09:20:06

标签: php mysql html5

我正在尝试将表单数据插入到mysql数据库中,但它没有插入到表中并且没有错误!

这是我的代码

<?php
$con = mysqli_connect('localhost', 'root', '', 'register');

if (isset($_POST['submit'])) {
  $shop = $_POST['shopname'];
  $name = $_POST['name'];
  $user = $_POST['username'];
  $email = $_POST['email'];
  $password = $_POST['password'];
  $repassword = $_POST['repassword'];
  $phone = $_POST['phone'];
  $sql = "INSERT INTO registration (shop_name,name,username,email,password,repassword,phone) VALUES  ('$shop','$name''$user','$email','$password','$repassword','$phone')";
  if (mysqli_query($con, $sql)) {
    echo "Signup Sucessfull";
  } else {
    echo mysqli_error();
  }
}
?>

如何解决此问题?

5 个答案:

答案 0 :(得分:2)

原来你忘记在名字后面提一个逗号。

'$name''$user'   // Missing comma in between

此外,它应该是mysqli_error($ con)而不是mysqli_error()

尝试一些调试:

$sql = "INSERT INTO registration (shop_name,name,username,email,password,repassword,phone) VALUES  ('".$shop."','".$name."', '".$user."','".$email."','".$password."','".$repassword."','".$phone."')";

mysqli_query($con, $sql) or die(mysqli_error($con));

答案 1 :(得分:0)

  

您似乎错过了插入值之间的“,”。这段代码可以正常使用。

<?php
$con = mysqli_connect('localhost', 'root', '', 'register');

if (isset($_POST['submit'])) {
  $shop = $_POST['shopname'];
  $name = $_POST['name'];
  $user = $_POST['username'];
  $email = $_POST['email'];
  $password = $_POST['password'];
  $repassword = $_POST['repassword'];
  $phone = $_POST['phone'];
  $sql = "INSERT INTO registration (shop_name,name,username,email,password,repassword,phone) VALUES  ('".$shop."','".$name."','".$user."','".$email."','".$password."','".$repassword."','".$phone."')";
  if (mysqli_query($con, $sql)) {
    echo "Signup Sucessfull";
  } else {
    die(mysqli_error($con));
  }
}
?>

答案 2 :(得分:0)

  

是的,已经 @ObjectManipulator 指出了你的愚蠢错误   靠近'$name''$user'

我强烈建议您使用mysqli_prepare来避免SQL注入。

<?php
$con = mysqli_connect('localhost', 'root', '', 'register');
if (isset($_POST['submit'])) {

  $stmt = mysqli_prepare($con, "INSERT INTO registration (shop_name,name,username,email,password,repassword,phone) VALUES (?, ?, ?, ?,?, ?, ?)");
  mysqli_stmt_bind_param($stmt, 'sssssss',$_POST['shopname'],$_POST['name'],$_POST['username'],$_POST['email'],$_POST['password'],$_POST['repassword'],$_POST['phone']);

  if (mysqli_stmt_execute($stmt)) {
    echo "Signup Sucessfull";
  } else {
    echo mysqli_error($con);
  }
}
?>
  

并且, @JonStirling 建议不要以纯文本格式存储密码   并使用任何Password API加密密码。

加密密码的方法有很多种。使用其中任何一个。现在,我用md5()说明了。

并且,为什么要在数据库表中存储passwordrepassword。将用户数据存储到数据库表中时,请检查密码和密码。无论是否匹配。

只是一个建议。你可以选择它。

<?php
$con = mysqli_connect('localhost', 'root', '', 'register');
if (isset($_POST['submit'])) {

  if(isset($_POST['password']) && isset($_POST['repassword']) && ($_POST['password'] == $_POST['repassword'])){
    $stmt = mysqli_prepare($con, "INSERT INTO registration (shop_name,name,username,email,password,phone) VALUES (?, ?, ?, ?, ?, ?)");
    mysqli_stmt_bind_param($stmt, 'ssssss',$_POST['shopname'],$_POST['name'],$_POST['username'],$_POST['email'],md5($_POST['password']),$_POST['phone']);

    if (mysqli_stmt_execute($stmt)) {
      echo "Signup Sucessfull";
    } else {
      echo mysqli_error();
    }
  } else {
    echo "Password must match.";
  }
}
?>

答案 3 :(得分:0)

$ionicPlatform.ready(function () 
{
        if (window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }
        //setTimeout(function() {
        //    navigator.splashscreen.hide();
        //},2000);
        //    ----------------network information--starts------------
       var type = $cordovaNetwork.getNetwork();
        **var isOnline = $cordovaNetwork.isOnline();**
        localStorage.network_type = type;
        localStorage.isOnline = isOnline;
        localStorage.network_state;
        if (localStorage.isOnline) {
            localStorage.network_state = "online";
        } else {
            localStorage.network_state = "offline";
        }

问题解决了。您忘记了MySQL错误输出的连接详细信息else { echo mysqli_error($con); } 。现在,这将从您的查询中正确输出MySQL语法错误。

其他注释:

  • 使用MySQLi的准备语句(link
  • 使用正确的密码哈希算法,例如Password_hash。不要使用MD5(它太快且碰撞太多)而且从不将密码存储为明文。
  • 使用POSTed变量上的各种filter_Var清除它们,并确保捕获任何无效数据(例如不正确的电子邮件地址)

答案 4 :(得分:0)

将逗号放在sql查询中,如下所示

 $sql = "INSERT INTO registration (shop_name,name,username,email,password,repassword,phone)VALUES  
('$shop','$name','$user','$email','$password','$repassword','$phone')";