无法在php mysql的表中插入记录

vyswwuz2  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(241)

我无法在表中插入记录,甚至没有得到任何错误,但当我尝试使用相同的方法创建表时,查询正在正确执行。我在过去的两天里一直在解决这个问题。谁能帮帮我吗。 $database->create_database() 函数用于创建动态数据库。 $database->create_tables() 函数用于创建具有 MySQL multi-query 方法。
索引.php

<?php

ini_set('display_errors', '1');
error_reporting(E_ALL);

session_start();

$dbusername = $_SESSION['dbusername'];
$dbpassword=$_SESSION['dbpassword'];
$dbname=$_SESSION['dbname'];
$clienturl=$_SESSION['clienturl'];
$email=$_SESSION['email'];

error_reporting(0); 

$db_config_path = '../application/config/database.php';

$_POST = array('hostname' => 'localhost' ,
           'username' =>  $dbusername,
           'password' =>  $dbpassword,
           'database' =>  $dbname ,
           'clienturl' =>  $clienturl ,

          );

require_once 'includes/core_class.php';
require_once 'includes/database_class.php';

$core = new Core();
$database = new Database();

// Validate the post data
if ($core->validate_post($_POST) == true) {

    // First create the database, then create tables, then write a config file

    if ($database->create_database($_POST) == false) {
        $message = $core->show_message('error', 'The database could not be created, please verify your settings.');
    }
     elseif($database->create_tables($_POST) == false) {
        $message = $core->show_message('error', 'The database tables could not be created, please verify your settings.');
    } 

    elseif ($core->write_config_url($_POST) == false) {
        $message = $core->show_message('error', 'The database configuration url file could not be written, please chmod application/config/database.php file to 777');
    }

     elseif ($core->write_config($_POST) == false) {
        $message = $core->show_message('error', 'The database configuration file could not be written, please chmod application/config/database.php file to 777');

    } 
    elseif ($database->update_tables($_POST) == false) {
        $message = $core->show_message('error', 'There is a problem while updating email id and password');
    } 

    // If no errors, redirect to registration page
    if (!isset($message)) {

        $redir = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http');
        $redir .= '://'.$_SERVER['HTTP_HOST'];
        $redir .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
        $redir = str_replace('install/', '', $redir);
        header('Location: '.$redir);
    }

} else {
    $message = $core->show_message('error', 'Not all fields have been filled in correctly. The host, username, password, and database name are required.');
}
//}

?>

数据库\u class.php文件

<?php

class Database
{
// Function to the database and tables and fill them with the default data
public function create_database($data)
{
    // Connect to the database
    $mysqli = new mysqli($data['hostname'], $data['username'], $data['password'], '');

    // Check for errors
    if (mysqli_connect_errno()) {
        return false;
    }

    // Create the prepared statement
    $mysqli->query('CREATE DATABASE IF NOT EXISTS '.$data['database']);

    // Close the connection
    $mysqli->close();

    return true;
}

// Function to create the tables and fill them with the default data
public function create_tables($data)
{
    // Connect to the database
    $mysqli = new mysqli($data['hostname'], $data['username'], $data['password'], $data['database']);

    // Check for errors
    if (mysqli_connect_errno()) {
        return false;
    }

    // Open the default SQL file
    $query = file_get_contents('assets/install.sql');

    // Execute a multi query
    $mysqli->multi_query($query);

    // Close the connection
    $mysqli->close();

    return true;
}

public function update_tables($data)
{

    // Connect to the database
    $mysqli = new mysqli($data['hostname'], $data['username'], $data['password'], $data['database']);

    // Check for errors
    if (mysqli_connect_errno()) {
        return false;
    }

   $insert =  "INSERT INTO xyz (firstname) VALUES ('Tacos')";

   $mysqli->query($insert);

    $mysqli->close();

    return true;
}

}

以及 core_class.php 类用于验证表单数据并编写一些配置文件。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题