php - 为什么我的代码条件不正确?
问题描述
我是 PHP 和 MySQL 的初学者,我想将来自 HTML 输入的值添加到 MySQL 数据库。
我必须在 Internet 上找到一些东西,但这不起作用,所以我尝试多学习一点 PHP,但我仍然不明白为什么下面代码中的条件无效:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8">
<title>SHAR-APP</title>
</head>
<body>
<div class='div1'>
<div class='div2'>
<label for="name">Name of the user:</label>
<input class ='in'type="text" id="name" name="name" size="20">
</div>
<div class='div2'>
<label class = 'label' for="name">Code:</label>
<input class='in' id ='code' type="text" name="code" size="20">
</div>
<div class="div2" id='b'>
<input type="submit" value="send" class='button'>
</div>
</div>
<?php
echo "test1";
if (isset($_POST['name'])) {
echo "test2";
$mtsqli = mysqli_connect('localhost','the_name_of_my_project','my_password');
mysqli_select_db('project_database', $msqli);
$requete = 'INSERT INTO the_name_of_the_database's_table VALUES(NULL,"' . $_POST['name'] . '","' . $_POST['code'] . '")';
$query = "SELECT * FROM the_name_of_the_database's_table";
echo $_POST['name'];
echo "test3";
}
?>
</body>
</html>
我在这个上待了 3 天,我真的被封锁了。也许我在 PHP 代码中有其他错误。如果我可以用另一种语言做到这一点,我更愿意继续使用 PHP,因为我不想学习太多语言。如果我可以使用 Python 或 JavaScript 在 HTML 和 MySQL 之间架起一座桥梁,我可以知道这一点。
这部分很好,但出现了另一个问题......当我想连接我的数据库时,会显示此错误消息
C:\Users\titou>set PATH=$PATH$;C:\Program Files\MySQL\MySQL Server 8.0\bin
C:\Users\titou>mysql -h localhost -u root -p
Enter password: **********
ERROR 1045 (28000): AccŠs refus‚ pour l'utilisateur: 'root'@'@localhost' (mot de passe: OUI)
它是法语的,但你可以看到有两个@ instand of one ('root'@'localhost')
解决方案
首先你需要添加一个表单方法
<form action="" method="POST">
<div class='div2'>
<label for="name">Name of the user:</label>
<input class ='in'type="text" id="name" name="name" size="20">
</div>
<div class='div2'>
<label class = 'label' for="name">Code:</label>
<input class='in' id ='code' type="text" name="code" size="20">
</div>
<div class="div2" id='b'>
<input type="submit" value="send" class='button'>
</div>
</form>
这是某种类,它告诉浏览器期待输入,然后单击按钮来处理它们。Action=""用于初始化处理给定输入的位置。在您的情况下,由于您的 php 代码与表单在同一类中,因此您可以将其留空,无论哪种方式您都应该初始化要发送这些数据的路径。Method="POST"只是一种在网络上处理您的数据的方法。它也比 GET 方法更安全,它也可以工作,但它更敏感,因为来自输入的所有数据也将在 URL 中公开。
此外,我希望你已经安装了 XAMPP 并且已经在 MySQL 中创建了一个数据库。
推荐阅读
- powerbi - PowerBI上缺少某些行时如何计算平均值?
- odata - 如何通过 IQueryable 注入授权逻辑
在呈现 sql 语句之前进入 EFCore? - salesforce - Mulesoft Salesforce 连接器插入多个对象记录
- xslt - Xslt 1.0 将日期转换为 dateTime 格式
- python - 在 Pyspark 中导入 csv 时如何修复“'路径不存在”
- python - 使用 python 导出大型数据集(从 json 到 dataframe 到 excel)
- reactjs - React-Final-Form csv 上传提交
- android - 如何注册 OrientationEventListener 以接收 OrientationChange 通知?
- orchardcms - Orchard CMS - 无法从命令行设置租户
- c++ - 如何使用 cmake 管理多个构建目标配置