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

时间:2018-07-11 03:34:34

标签: php mysql phpmyadmin

我无法将记录插入表中,甚至没有收到任何错误,但是当我尝试使用相同的方法创建表时,查询将正确执行。最近两天来,我一直坚持这个问题。谁能帮帮我。 $database->create_database()函数用于创建动态数据库。 $database->create_tables()函数用于通过MySQL multi-query方法创建表。

index.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.');
}
//}

?>

database_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类用于验证表单数据并写入一些配置文件。

0 个答案:

没有答案