如何解决“无法添加或更新子行"? [英] How to solve "Cannot add or update child row"

查看:82
本文介绍了如何解决“无法添加或更新子行"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试插入数据库时​​遇到一些错误,最让我担心的是,我不断收到错误:

I have been getting some errors while trying to insert into my database, what mostly concerns me is that i keep getting the error:

无法添加或更新子行

这是我的用户表:

users:

+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| name     | varchar(20) | NO   |     |         |                |
| email    | varchar(25) | NO   |     |         |                |
| avatar   | varchar(60) | NO   |     |         |                |
| username | varchar(25) | NO   |     |         |                |
| password | varchar(25) | NO   |     |         |                |
| about    | text        | NO   |     |         |                |
+----------+-------------+------+-----+---------+----------------+

这里我只有1个条目,它的索引为1

Here i have only 1 entry and it is with index 1

这是我的categoryf表:

And this is my categoryf table:

categoryf:

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

这里我也有1个条目,并且索引也为1

Here i also have 1 entry, and also with index 1

这是主题类别:

topic:

+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| c_id     | int(11)     | NO   |     |         |                |
| user_id  | int(11)     | NO   |     |         |                |
| avatar   | varchar(60) | NO   |     |         |                |
| title    | varchar(25) | NO   |     |         |                |
| body     | text        | NO   |     |         |                |
+----------+-------------+------+-----+---------+----------------+

c_id(category_id)user_id是表categoryfuser

这是我使用的表格:

 <form method="post" action="create.php">
          <div class="form-group">
            <label>Наслов</label>
            <input type="text" name="title" class="form-control">
          </div>
          <div class="form-group">
            <label>Вашата Дискусија</label>
            <textarea class="form-control" name="post_text" rows="10" cols="80"></textarea>
            <script>CKEDITOR.replace('post_text');</script>
          </div>
          <div class="form-group">
            <label>Категорија</label>
            <select class="form-control" name="category">
              <?php foreach(Categoryf_id() as $category) : ?>
              <option><?php echo $category['name']; ?></option>
            <?php endforeach ;?>
            </select>
          </div>
          <button type="submit" name="post" class="btn btn-primary">Креирајте Дискусија</button></a>
        </form>

其中Categoryf_id为:

Where Categoryf_id is:

function Categoryf_id()
{
$db = new Database();

//Create Query Category name
$query="SELECT * FROM categoryf";

//Run Query Category name
$row=$db->select($query);

return $row;
}

最后是我的Post函数:

And finally my Post function:

function Post()
{

$db=new Database();

if(isset($_POST['post']))
{
$title = mysqli_real_escape_string($db->link, $_POST['title']);
$post_text = mysqli_real_escape_string($db->link, $_POST['post_text']);
$category = mysqli_real_escape_string($db->link, $_POST['category']);
}
$query= "INSERT INTO topic
        (category_id, user_id, title, body) 
    VALUES('$category', '".$_SESSION['id']."', '$title', '$post_text')";

$insert_row = $db->insert($query);
}

我知道我的查询是正确的,因为我在phpmyadmin中使用了它来添加一个主题,其中ID仅为1,但是在这里我得到了这些错误:

I know that my query is correct, because i used it in phpmyadmin to add a topic, where is used the id's as just 1, but here i get these errors:

Notice: Undefined variable: category in /opt/lampp/htdocs/PHP-    Wizard/helpers/query.php on line 132

Notice: Undefined variable: title in /opt/lampp/htdocs/PHP-Wizard/helpers/query.php on line 132

Notice: Undefined variable: post_text in /opt/lampp/htdocs/PHP-Wizard/helpers/query.php on line 132
Cannot add or update a child row: a foreign key constraint fails (`PHPWizard`.`topic`, CONSTRAINT `topic_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categoryf` (`id`))47

我什至试图用mysql_query('SET foreign_key_checks = 0');删除外键检查,但是没有运气.

I even tried to remove the foreign key checks with mysql_query('SET foreign_key_checks = 0');, with no luck.

我在堆栈溢出问题上也搜索过其他问题,也没有运气.

I searched other Questions on Stack Overflow, also with no luck.

是什么导致此问题,我该如何解决?

What is causing this problem and how can i fix it?

如果这篇文章这么长,我也深表歉意,但是我认为如果有人要帮助我,我需要显示此部分的所有代码.

Also i apologize if this is such a long post, but i think i needed to show all the code for this part, if someone was going to help me.

推荐答案

我有点怀疑,您对$category作为字符串的处理而topic.category_id被定义为INT(11)的处理可能会引起您的悲伤.值得将您的查询更改为一个准备好的语句(出于这个原因和许多其他原因),如下所示:

I have a nagging suspicion that your handling of $category as a string while topic.category_id is defined as INT(11) might be causing you grief. It would be worth changing your query to a prepared statement (for this and a multitude of other reasons) as follows:

$query= "INSERT INTO topic
    (category_id, user_id, title, body) 
    VALUES(?, ?, ?, ?)";
$stmt = $db->link->prepare($query);
// The first arg, 'iiss', means "int, int, string, string"
$stmt->bind_param('iiss', $category, $_SESSION['id'], $title, $post_text);
$stmt->execute();

这使驱动程序层能够自动处理引号,特殊字符的转义以及数据的类型匹配.

This allows the driver layer to automagically deal with quoting, escaping of special characters, and type-matching of data.

希望有帮助.

这篇关于如何解决“无法添加或更新子行"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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