SQL不会插入数据,但会连接到数据库吗?

时间:2019-01-26 10:49:06

标签: php sql database insert

我已经尝试了好几个小时都无济于事,我的代码告诉我我已成功连接,但是当我单击“注册”页面只是刷新时,我检查了数据库没有任何变化,我尝试了几种不同的在线脚本来尝试检查这是否是我的代码,但没有用,我更新了我的高迪SQL版本,仍然什么都没有改变。

我希望有人可以告诉我即时消息做错了什么,谢谢,这是即时消息正在使用的代码。

<!DOCTYPE html>
<?php
$servername = "localhost";
$username="****";
$password="*****";
$dbname="****";
try{
$conn = mysqli_connect($servername, $username,$password,$dbname);
echo("successful in connection");
}catch(MySQLi_Sql_Exception $ex){
echo("error in connection");
}

if(isset($_POST['submit'])){
$username=$_POST['username'];
$password=$_POST['password'];
$email=$_POST['email'];
$register_query = "INSERT INTO `users`('email`, `username`, `password`) VALUES ('$email','$username','$password')";
try{
$register_result = mysqli_query($conn, $register_query);
if($register_result){
if(mysqli_affected_rows($conn)>0){
echo("registration successful");
}else{
echo("error in registration");
}

}
}catch(Exception $ex){
echo("error".$ex->getMessage());
}
}

?>

<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>Register</title>
  <link rel='stylesheet' href='http://netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css'>
  <link rel="stylesheet" href="css/style.css">
</head>

<body  id="fullBg">



<div class="container">
    <form class="form-signin"  id="login-register"  method="post" action="">
      <h1 class="form-signin-heading">Join The Club</h1>
      <div style="margin:auto; width:135px; height:135px; background-color:grey; margin-bottom:15px; border-radius:50%;">
        <br>

      </div>
        <input style="" type="text" class="form-control" name="username" value="<?php echo $username; ?>" placeholder="Username" />
        <input type="text" class="form-control" name="email" id="inputEmail" placeholder="Email" />
        <input style="margin:0px; margin-bottom:15px;" type="password" class="form-control" name="password" id="inputPassword"  placeholder="Password" />

  <input type="text" class="form-control" name="ppemail" value="<?php echo $ppemail; ?>" placeholder="Paypal Email"/>
<br>
      <button class="logo-button btn-block" type="submit">Register</button>
    </form>
  <a  href="/login.php"><p class="text-center sign-up" style="color:black !important;">Back To <strong>Login</strong></p></a>
  <?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
</div>



</body>

</html>

1 个答案:

答案 0 :(得分:0)

将此var_dump($register_query);exit;放在此行之后:

$register_result = mysqli_query($conn, $register_query);

然后检查sql或直接运行sql(通过在命令行中登录mysql,或使用phpmyadmin或一些GUI客户端,例如mysql workbench或mysql的Navicat等等)