【发布时间】:2016-09-18 23:26:07
【问题描述】:
我在终端的mysql 中创建了一个users 表,我正在尝试创建简单的任务:从表单中插入值。这是我的dbConfig file
<?php
$mysqli = new mysqli("localhost", "root", "pass", "testDB");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
这是我的Index.php。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="$1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="style.css">
<title>test</title>
<?php
include_once 'dbConfig.php';
?>
</head>
<body>
<?php
if(isset($_POST['save'])){
$sql = "INSERT INTO users (username, password, email)
VALUES ('".$_POST["username"]."','".$_POST["password"]."','".$_POST["email"]."')";
}
?>
<form method="post">
<label id="first"> First name:</label><br/>
<input type="text" name="username"><br/>
<label id="first">Password</label><br/>
<input type="password" name="password"><br/>
<label id="first">Email</label><br/>
<input type="text" name="email"><br/>
<button type="submit" name="save">save</button>
<button type="submit" name="get">get</button>
</form>
</body>
</html>
点击我的保存按钮后,没有任何反应,数据库仍然是空的。我尝试了echo'ing INSERT 查询,它从表单中获取所有值。在我尝试从终端检查这是否有效后,我登录到我的sql 尝试从用户表中返回所有数据,但我得到了一个空集。
【问题讨论】:
-
使用函数mysqli_query进行SQL查询
-
您的代码只是
echo正在执行 SQL 查询... -
您需要
mysqli_query($mysqli, $sql);查询。您目前只是echoing 查询,基本上只是将其输出到屏幕上。 -
mysqli_query($mysqli, $sql); mysqli_query