简体   繁体   English

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

[英]Passing information to MySQL database to an HTML form using PHP

I am trying to connect to a MySQL database to save information passed via a registration form on my website. 我正在尝试连接到MySQL数据库,以保存通过注册表在我的网站上传递的信息。 for some reason, it won't connect. 由于某种原因,它无法连接。 I have searched in Stack Overflow but can't find the right answer. 我已经在Stack Overflow中进行了搜索,但是找不到正确的答案。

Here is my PHP code: 这是我的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); 
?>

And here is my HTML code: 这是我的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>

When I press the submit button, it just shows me a black page, with no errors, and in phpMyAdmin, the table has not been created and of course the information has not been passed. 当我按下提交按钮时,它只显示一个黑页,没有错误,并且在phpMyAdmin中,表尚未创建,当然信息也没有传递。 I'm using GoDaddy hosting if it has any signification for this issue, and i have checked like 25 times that I'm using the correct hostname and database information. 我正在使用GoDaddy托管(如果对此问题有任何含义),并且我已经检查了25次,即我使用的是正确的主机名和数据库信息。

Thanks. 谢谢。

Try new mysqli instead of mysqli_connect. 尝试使用new mysqli而不是mysqli_connect。 If this still doesn't work, I'd check to make sure that you're entering the correct values for the username, password, database name, and servername. 如果仍然无法解决问题,我将检查您是否为用户名,密码,数据库名称和服务器名称输入了正确的值。 Also, I'm unsure why you are using this line: 另外,我不确定您为什么使用此行:

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

^ That will create a new table each time the page is loaded. ^这将在每次加载页面时创建一个新表。 What you should do is create the table first by running that code in phpmyadmin, and then insert into it with php. 您应该做的是首先通过在phpmyadmin中运行该代码来创建表,然后使用php将其插入其中。 Try this: 尝试这个:

<?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.";
?>

Also add name= to your HTML: 还要在HTML中添加name =:

  <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>

First of all you need to start using prepered statements. 首先,您需要开始使用预先声明的语句。 Be aware of SQL njections. 注意SQL喷射。

Second you need to change your column name "Password", it is a reserve word and you will get errors. 其次,您需要更改列名“ Password”,这是一个保留字,您会收到错误消息。

Third. 第三。 Your inputs don't have any name, you have assign a id to your inputs but not a name. 您的输入内容没有任何名称,您为输入内容分配了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>

after submiting your form use mysqli_real_escape string() . mysqli_real_escape string()表单后,使用mysqli_real_escape string()

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

I think it is an empty index problem. 我认为这是一个空索引问题。 I have a solution for you to try: if(isset('$_POST["btnreg"]')){$query = "your query;mysqli_query($con,$query);"} 我有一个解决方案供您尝试: if(isset('$_POST["btnreg"]')){$query = "your query;mysqli_query($con,$query);"}

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

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