使用PHP将信息传递到MySQL数据库到HTML表单

时间:2016-07-31 19:07:09

标签: php html mysql html-form

我正在尝试连接到MySQL数据库以保存通过我网站上的注册表单传递的信息。由于某种原因,它不会连接。我在Stack Overflow中搜索过但找不到合适的答案。

这是我的PHP代码:

<?php
$con = mysqli_connect("mysite","my username","my password","My database");

if($con === false){
    die("ERROR: Could not connect. ". mysqli_connect_error());
}

$sql = "CREATE TABLE clients(client_id INT(4) NOT NULL PRIMARY KEY AUTO_INCREMENT, username CHAR(30) NOT NULL, password CHAR(30) NOT NULL)";

if (mysqli_query($con, $sql)){
    echo "Table clients created successfully";
}
else {
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
}

$user_info = “INSERT INTO clients(username, password) VALUES ( '$_POST["username"]','$_POST["password"]')”;
mysqli_query($con,$user_info);

if (!mysqli_query($con, $user_info)){ 
    die('Error: ' . mysql_error());
}

echo “Your information was added to the database.”;
mysqli_close($con); 
?>

这是我的HTML代码:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>

<div class="register">
  <h1>Register Here</h1>
  <form action="register.php" method="post">
    <input type="text" id="username" placeholder="Username" required="required" /><br><br>
    <input type="password" id="password" placeholder="Password" required="required" /><br>
    <button id="btnreg" type="submit">Let Me In</button>
  </form>
</div>


</body>
</html>

当我按下提交按钮时,它只显示一个黑色页面,没有错误,而在phpMyAdmin中,表格尚未创建,当然信息尚未传递。 我正在使用GoDaddy托管,如果它对此问题有任何意义,我检查了25次,我使用正确的主机名和数据库信息。

感谢。

3 个答案:

答案 0 :(得分:0)

尝试new mysqli而不是mysqli_connect。如果仍然无法正常工作,请检查以确保您输入的用户名,密码,数据库名称和服务器名称的值正确。\ n另外,我不确定你为什么使用这一行:

 $sql = "CREATE TABLE clients(client_id INT(4) NOT NULL PRIMARY KEY AUTO_INCREMENT, username CHAR(30) NOT NULL, password CHAR(30) NOT NULL)";

^每次加载页面时都会创建一个新表。你应该做的是首先通过在phpmyadmin中运行该代码来创建表,然后使用php插入它。试试这个:

<?php
    $servername = "your server name";
    $username = "your db username";
    $password = "your DB password";
    $dbname = "name of your database";

    $conn = new mysqli($servername, $username, $password, $dbname);

    $username = $_POST['username'];
    $password = $_POST['password'];
    //As jens pointed out, password is a reserved word in mysql. So change the value of the password column to pass_word or something else.
    $user_info = “INSERT INTO clients(username, pass_word) VALUES ( '".$username"', '".$password"')";
    $conn->query($user_info);
    echo "Your information was added to the database.";
?>

还要将name =添加到您的HTML:

  <form action="register.php" method="post">
    <input type="text" id="username" name="username" placeholder="Username" required="required" /><br><br>
    <input type="password" name="password" id="password" placeholder="Password" required="required" /><br>
    <button id="btnreg" name="btnreg" type="submit">Let Me In</button>
  </form>

答案 1 :(得分:0)

首先,您需要开始使用prepered语句。注意SQL注意事项。

其次,您需要更改列名“密码”,这是一个保留字,您将收到错误。

第三。您的输入没有任何名称,您已为输入分配了ID而不是名称。

<input type="text" id="username" name='username' placeholder="Username" required="required" /><br><br>
<input type="password" id="password" name='password' placeholder="Password" required="required" /><br>
提交表单后

使用mysqli_real_escape string()

$username = mysqli_real_escape_string($con,$_POST['username']);
$password = mysqli_real_escape_string($con,$_POST['password']);

答案 2 :(得分:0)

我认为这是一个空索引问题。我有一个解决方案供您尝试:if(isset('$_POST["btnreg"]')){$query = "your query;mysqli_query($con,$query);"}