我知道在一个页面上有两个关于两个查询命令的问题,但我已经尝试了所有内容,但我无法让它工作。
我正在尝试创建一个简单的表单,将输入的值添加到数据库中,然后使用select语句输出数据库。
我做错了什么?
这是我得到的。非常感谢!
<?php
$dbhost = "localhost";
$dbuser = "widget_cms";
$dbpass = "blah123";
$dbdata = "widget_corp";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbdata);
if(mysqli_connect_errno()) {
die("database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
} else {
echo "success!";
}
?>
<?php
if (isset($_POST["submit"])) {
$menu_name = ($_POST["menuName"]);
$position = ($_POST["pos"]);
$visibility = ($_POST["vis"]);
$query1 = "INSERT INTO subjects (menu_name, position, visibility) VALUES ('{$menu_name}', '{$position}', '{$visibility}'); ";
$result = mysqli_query($connection, $query1);
if(!$result) {
die("Database query failed");
}
} else {
echo "something didnt work idiot";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>phpMysql</title>
</head>
<body>
<ul>
<form action="phpMysql.php" method="post">
Menu Name: <input type="text" name="menuName" value=""><br>
Position: <input type="text" name="pos" value=""><br>
Visibility: <input type="text" name="vis" value=""><br>
<input type="submit" name="submit" value="Create account"><br>
</form>
</ul>
<?php print_r($_POST); ?>
<?php
$query1 = "SELECT * FROM subjects;" ;
$result = mysqli_query($connection, $query1);
if(!$result) {
die("Database query failed");
}
?>
<?php while ($row = mysqli_fetch_assoc($result)) {
var_dump($row);
echo "<hr />" ;
}
?>
<?php mysqli_free_result($result); ?>
</body>
</html>
<?php mysqli_close($connection); ?>
答案 0 :(得分:0)
首先,您的编码有几处错误。这一行:
$query1 = "INSERT INTO subjects (menu_name, position, visibility) VALUES ('{$menu_name}', '{$position}', '{$visibility}'); ";
错了。你不需要围绕变量的任何{}。另外,你有一个额外的;在你的报价内。它不会与那些人一起工作。这是正确的代码:
$query1 = "INSERT INTO subjects (menu_name, position, visibility) VALUES ('$menu_name', '$position', '$visibility')";
此行也很糟糕,你还包括一个额外的;在您的查询中。
$query1 = "SELECT * FROM subjects;" ;
应该是这样的:
$query1 = "SELECT * FROM subjects";
你还可以做一些清理工作。很多php块可以合并为单个块,使您的代码看起来更整洁。这是您编辑的代码,包含我注意到的所有修复。
<?php
$dbhost = "localhost";
$dbuser = "widget_cms";
$dbpass = "blah123";
$dbdata = "widget_corp";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbdata);
if(mysqli_connect_errno()) {
die("database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
} else {
echo "success!";
}
if (isset($_POST["submit"])) {
$menu_name = ($_POST["menuName"]);
$position = ($_POST["pos"]);
$visibility = ($_POST["vis"]);
$query1 = "INSERT INTO subjects (menu_name, position, visibility) VALUES ('$menu_name', '$position', '$visibility')";
$result = mysqli_query($connection, $query1);
if(!$result) {
die("Database query failed");
}
} else {
echo "something didnt work idiot";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>phpMysql</title>
</head>
<body>
<ul>
<form action="phpMysql.php" method="post">
Menu Name: <input type="text" name="menuName" value=""><br>
Position: <input type="text" name="pos" value=""><br>
Visibility: <input type="text" name="vis" value=""><br>
<input type="submit" name="submit" value="Create account"><br>
</form>
</ul>
<?php
print_r($_POST);
$query1 = "SELECT * FROM subjects";
$result = mysqli_query($connection, $query1);
if(!$result) {
die("Database query failed");
}
while ($row = mysqli_fetch_assoc($result)) {
var_dump($row);
echo "<hr />" ;
}
mysqli_free_result($result);
?>
</body>
</html>
<?php mysqli_close($connection); ?>
如果您不确定如何正确编码或遇到您不了解的问题,请参阅有关查询的文档。 http://php.net/manual/en/mysqli.query.php我还建议调查准备好的陈述。它们比标准的mysqli查询更好,更安全。 http://php.net/manual/en/mysqli.quickstart.prepared-statements.php