<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Php form for MySQL</title>
</head>
<body>
<form action="home.php" method="post">
<p>
<label for="table">Name your Table</label>
<input type="text" name="table" id="table">
</p>
<p>
<label for="firstName">First Name:</label>
<input type="text" name="firstname" id="firstName">
</p>
<p>
<label for="lastName">Last Name:</label>
<input type="text" name="lastname" id="lastName">
</p>
<p>
<label for="emailAddress">Email Address:</label>
<input type="text" name="email" id="emailAddress">
</p>
<button name="submit" type="submit" value="submit">Create Table</button>
</form>
if (isset($_POST['submit'])){
//enter database details
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
$table = $_POST['table'];
// Create a MySQL table in the selected database
mysqli_query("CREATE TABLE $table (
firstname VARCHAR(30),
lastname VARCHAR(30),
email VARCHAR(50))") or die(mysql_error());
if ($conn->query($sql) === TRUE) {
echo "Table account created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
if(isset($_POST['firstname']) || ($_POST['lastname']) || ($_POST['email']))
{
$order="INSERT INTO $table (firstname,lastname,email) VALUES ('$firstname','$lastname','$email')";
$result = mysqli_query($order) or die (mysql_error());
}
mysqli_close($conn);
}
?>
</body>
</html>
我正在尝试允许用户为数据库选择一个名称并被卡住了。我收到错误
用户''@ localhost'拒绝访问(使用密码:否)
我想要的图片:
我不知道该怎么办。希望你能帮忙。感谢
答案 0 :(得分:1)
您获得的错误意味着您没有提供有效的凭据来连接数据库。请勿在您的问题中更新它们,因为这些详细信息应该是私有的,但您需要更改代码:
#include <stdlib.h>
#include <stdio.h>
// self - referential structure
struct listNode {
char data; // each listNode contains a character
struct listNode *nextPtr; //pointer to next node
};
typedef struct listNode ListNode; // synonym for struct listNode
typedef ListNode *ListNodePtr; // synonym for struct listnode*
void insert(ListNodePtr *sPtr, char value)
{
ListNodePtr newPtr = malloc(sizeof(ListNode)); // create node
if (newPtr != NULL) // is space availale?
{
newPtr->data = value; // place value in node
newPtr->nextPtr = NULL; // node does not link to another node
ListNodePtr previousPtr = NULL;
ListNodePtr currentPtr = *sPtr;
// loop to find the correct location in the list
while (currentPtr != NULL && value > currentPtr->data)
{
previousPtr = currentPtr; // walk to...
currentPtr = currentPtr->nextPtr; // ... next node
}
if (previousPtr == NULL) // insert new node at beginning of list
{
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else
{
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
else
{
printf("%c not inserted. No memory available. \n", value);
}
}
是实际值。例如,如果您的mysql用户名是$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
,则用户名行将如下所示:
'tomi'
你真的没有名为'database'的数据库吗?如果您的实际密码是“密码”,则需要将其更改为更安全的密码。
答案 1 :(得分:0)
您打开mysqli
连接,然后使用mysql_query
进行查询,该时间应为mysqli_query
。 mysql_*
和mysqli_*
是完全不同的东西,你永远不应该使用mysql_*
。更改后,请检查您的身份验证详细信息。只要您使用私人计算机,也可以使用var_dump()
查看它们并确保它们符合您的预期。看起来它们可能会作为空字符串传递到mysqli
。如果您的身份验证详细信息绝对正确,请仔细检查数据库中的给定用户是否确实具有所需的权限。
答案 2 :(得分:0)
使用我认为适合您的完整php代码编辑我的最佳答案:
<?php
if (isset($_POST['submit'])){
//enter database details
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
echo "Failed to connect: (" . $conn->connect_error . ") ";
}
$table = $_POST['table'];
// SQL to Create a table in the selected database
$sql = "CREATE TABLE $table (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30),
lastname VARCHAR(30),
email VARCHAR(50))";
if ($conn->query($sql) === TRUE) {
echo "Table account created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
// Check connection
if (!$conn) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
if(isset($_POST['firstname']) || isset($_POST['lastname']) || isset($_POST['email'])) {
$order="INSERT INTO $table (firstname,lastname,email) VALUES ('$firstname','$lastname','$email')";
if ($conn->query($order) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $order . "<br>" . $conn->error;
}
}
$conn->close();
}
?>
重要提示:
1) 确保在php代码的顶部输入正确的数据库详细信息。例如,对于我在Windows上使用xampp,它是:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
我之前已经手动创建了数据库“database”。
2)如果您想在代码的后半部分确保所有字段都不为空,您可能更愿意使用&amp;&amp; (和)而不是|| (或)你在if的条件。目前,只要填写了任何字段,它就会运行,即使其余字段是EMPTY也是如此。
示例:
if(isset($_POST['firstname']) && isset($_POST['lastname']) && isset($_POST['email'])) {
而不是:
if(isset($_POST['firstname']) || isset($_POST['lastname']) || isset($_POST['email'])) {
我还强烈建议您查看使用php实现MySQLi的不同方法;例如 创建表格:http://www.w3schools.com/php/php_mysql_create_table.asp