无法在 ubuntu 上进行 mysql 插入 [英] Can't do mysql insert on ubuntu

查看:42
本文介绍了无法在 ubuntu 上进行 mysql 插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近使用 ssh 将我的网站从我的电脑上传到了 ubuntu.
在我上传之前,它在 xampp 上运行良好.
现在一切正常,但 mysql 插入查询.
连接正在工作并且 mysql 更新.mysql error.log 中没有任何内容.

I recently uploaded my site from my pc to ubuntu with ssh.
Before I uploaded it worked fine with xampp.
Now everything else is working but mysql insert query.
Connect is working and mysql update. Nothing in mysql error.log.

Select 也能正常工作.

Select is working too.

这不起作用

$user_query = $db->prepare("INSERT INTO users (id, userid, name) VALUES (:id, :userid, :name)");
if($url_query->execute(array(":id" => "", ":userid" => $userid, ":name" => $name))){
    header("location: /");
}

这是有效的

$user_query = $db->prepare("UPDATE users SET name=:name WHERE userid =:userid");
if($user_query->execute(array(":name" => $name, ":userid" => $userid))){
    header("location: /");
}

我看不出这两个查询之间有任何区别.

I don't see any difference between these two querys.

表格

+----------+---------+------+-----+---------+----------------+
| Field    | Type    | Null | Key | Default | Extra          |
+----------+---------+------+-----+---------+----------------+
| id       | int(11) | NO   | PRI | NULL    | auto_increment |
| userid   | text    | NO   |     | NULL    |                |
| name     | text    | NO   |     | NULL    |                |
+----------+---------+------+-----+---------+----------------+

我的主机来自 digitalocean,我使用了 这个 当我安装了我的 apache 和 mysql

My host is from digitalocean and I used this when I installed my apache and mysql

$user_query = $db->prepare("SELECT id FROM users WHERE userid=:userid");
    if($user_query->execute(array(":userid" => $userid))){
        if($user_query->rowCount()){

            $user_query = $db->prepare("UPDATE users SET name=:name WHERE userid =:userid");
            if($user_query->execute(array(":name" => $name, ":userid" => $userid))){
                header("location: /");
            }

        } else {

            $user_query= $db->prepare("INSERT INTO users (id, userid, name) VALUES (:id, :userid, :name)");
            if($user_query->execute(array(":id" => "", ":userid" => $userid, ":name" => $name))){
                header("location: /");
            }
        }
    }

推荐答案

由于 id 字段设置为 auto_increment,请将其从插入语句中删除.MySQL 会为您处理.在您当前的查询中,您实际上是在尝试将 id 字段设置为空字符串(这应该会导致错误,因为它是设置为自动递增整数的不可为空的唯一主键).

Since the id-field is set to auto_increment, remove it from your insert statement. MySQL will handle that for you. In your current query, you're actually trying to set the id-field to an empty string (which should result in an error, since it's a non nullable, unique primary key set as an auto incremented integer).

试试:

$user_query= $db->prepare("INSERT INTO users (userid, name) VALUES (:userid, :name)");
if($user_query->execute(array(":userid" => $userid, ":name" => $name))){
    header("location: /");
}

如果您让它在一台服务器上运行而不是在另一台服务器上运行,则可能取决于 MySQL 版本.较新的 MySQL 版本对值的完整性更加挑剔.

If you get it to work on one server and not another, it might depend on the MySQL version. Newer MySQL-versions are much more picky about the integrity of the values.

这篇关于无法在 ubuntu 上进行 mysql 插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆