douhuang5623 2016-10-08 20:00
浏览 40
已采纳

无法将值插入数据库表[重复]

This question already has an answer here:

$a = "INSERT INTO `uc_messages` (from, to, time, subject, description) VALUES ('q', 'a', CURRENT_DATE(), 'a', 'a')";
$con = mysqli_connect('localhost', '', '');

if(!mysqli_query($con, $a)) { die('Invalid query: ' . mysqli_error($con)); } 

This is the code I'm using to insert values into my table, but for some reason this code doesn't work. This is my database and this is the table I'm trying to insert values into. This is the error I get "Invalid query: No database selected" although I include the file that includes the database.

<?php

//Database Information
$db_host = "localhost"; //Host address (most likely localhost)
$db_name = "users"; //Name of Database
$db_user = "root"; //Name of database user
$db_pass = ""; //Password for database user
$db_table_prefix = "uc_";

GLOBAL $errors;
GLOBAL $successes;

$errors = array();
$successes = array();

/* Create a new mysqli object with database connection parameters */
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
GLOBAL $mysqli;

if(mysqli_connect_errno()) {
    echo "Connection Failed: " . mysqli_connect_errno();
    exit();
}

//Direct to install directory, if it exists
if(is_dir("install/"))
{
    header("Location: install/");
    die();

}

?>
</div>
  • 写回答

2条回答 默认 最新

  • duan0818 2016-10-08 20:43
    关注

    Can you try next code and let us know what is the output?

    // database connection, update user, password and database
    $mysqli = new mysqli("localhost", "user", "password", "database");
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
        exit;
    }
    
    // insert data
    $query = "INSERT INTO `uc_messages` (`from`, `to`, `time`, `subject`, `description`) VALUES ('q', 'a', CURRENT_DATE(), 'a', 'a')";
    if(!mysqli_query($query)) {
        echo 'Invalid query: ' . mysqli_error($mysqli);
        exit;
    } 
    
    echo 'Success';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题