简体   繁体   English

创建MySQL表不起作用

[英]Creating MySQL Table not working

I don't understand where my error in this code to create a simple Table in a MySQL database is: 我不知道在这段代码中在MySQL数据库中创建简单表的错误是:

<?php

// Create connection
$con=mysqli_connect("localhost", "administrator", "199992", "test");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Select DB
mysqli_select_db($con, "test");

// Create table
$sql = "CREATE TABLE IF NOT EXISTS Notizen(Benutzer TEXT,Datum TEXT,Notiz TEXT)";  


This should actually work, shouldn't it? 这实际上应该起作用,不是吗?
But when I try to insert something later: 但是,当我稍后尝试插入一些内容时:

if(!(empty($_POST[vorname]) and empty($_POST[nachname]) and empty($_POST[notiz]))) {
$sql = "INSERT INTO Notizen (Benutzer, Datum, Notiz)
    VALUES
    ('$_POST[vorname] $_POST[nachname]', 'datum', '$_POST[notiz]')"; 

if (!mysqli_query($con,$sql))
{
echo "Error: " . mysqli_error($con);
 }
}

I get an error: 我收到一个错误:

"Error: Table 'test.notizen' doesn't exist". “错误:表'test.notizen'不存在”。

  1. As I see in your code, you are not executing the create query. 正如我在您的代码中看到的那样,您没有执行create查询。 execute it by mysqli_query($con,$sql) . 通过mysqli_query($con,$sql)执行它。
  2. Give the space between Notizen (Benutzer table name and braces in create table query. Notizen (Benutzer表名和创建表查询中的大括号之间) Notizen (Benutzer出空格。
  3. Check that your query is executing or not by using the error function to create table. 通过使用错误函数创建表,检查查询是否正在执行。

确保使用mysqli_query()运行查询。在将表定义存储在数据库中之前无法插入

After this line 在这行之后

$sql = "CREATE TABLE IF NOT EXISTS Notizen(Benutzer TEXT,Datum TEXT,Notiz TEXT)";  

You must run a 您必须运行

if (!mysqli_query($con,$sql))
{
    echo "Error creating a database: " . mysqli_error($con);
}

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

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