插入错误(PHP)

时间:2016-12-14 15:15:50

标签: php mysql

我一直在寻找这个问题的解决方案,但没有运气。基本上,当在表格中插入项目时,它不会返回结果(不会插入任何内容)。我检查了表格字段和所有匹配项,我似乎无法找到我正在做的错误。

这是我目前所拥有的代码部分,问题出现在哪里。

            <br>
                        <h3><center><b>Components management</b></center></h3>
                        <br>
                        <form name = "registo" method = "POST" action = "">
                        <center><fieldset>
                                        <legend>Insert component</legend>
                                        <br>
                                        <p><b> Nome: <input type = "text" name = "component_name" required></b> </p><br>

                                        <p> <b>Type:</b> </p>
                                        <div class = "mytable">
                                        <table><tr>
            <?php                            
                                    $queryComp_Type = "select id, name from conc_type order by name";
                                    $resultadoQueryComp_Type = mysqli_query($conn, $queryComp_Type);
                                    foreach ($resultadoQueryComp_Type as $rowComp_Type)
                                    {
            ?>
                                            <th><?php echo $rowComp_Type['name']; ?></th>  
            <?php
                                    }                      
            ?>
                                            </tr><tr>
            <?php
                                    foreach ($resultadoQueryComp_Type as $rowComp_Type)
                                    {
            ?>
                                            <td><input class= "radio" type= "radio" name= "component_type" value= "<?php echo $rowComp_Type['id']; ?>" required> </td>  
            <?php
                                    }
            ?>
                                        </tr></table>
                                        </div><br>
                                        <p> <b>State:</b></p> 
                                            <table class="mytable">
                                            <tr><th>Active</th><th>Inactive</th></tr>
                                            <tr>
                                                <td> <input class= "radio" type= "radio" name= "component_state" value= "active" required></td>
                                                <td> <input class= "radio" type= "radio" name= "component_state" value= "inactive"></td>
                                            </tr>
                                            </table>
                                        <br>

                                        <!--Insert button-->
                                        <input type= "hidden" name= "state" value= "inserir">
                                        <input class= "button" type= "submit" value= "Inserir Componente">
                                        <br><br>

                        </fieldset></center>
                        </form>
            <?php
                    }
                    else if($_REQUEST['state'] == "inserir")
                    {
            ?>
                        <h3>Components management - inserçtion</h3>
            <?php
                        $component_name = $_REQUEST['component_name'];
                        $component_type = $_REQUEST['component_type'];
                        $component_state = $_REQUEST['component_state'];


                                $insert = sprintf("INSERT INTO `concept` (`name`,`conc_type_id`,`state`) VALUES ('%s', '$component_type', '%s')", mysqli_real_escape_string($conn, $component_name), 
                                mysqli_real_escape_string($conn, $component_state));

                        $resultado_insert = mysqli_query($conn, $insert);

                        if($resultado_insert)
                        {
                            mysqli_query($conn,'COMMIT');
            ?>                             
                            <p>Insert successfuly</p>
                            <p>Click in <a href = "gestao-de-componentes">Continue</a> to advance.</p>
            <?php
                        }
                        else
                        {
                            mysqli_query($conn,'ROLLBACK');
            ?>
                            <p>Insertion error.</p>
            <?php
                        }
                        back();
                    }
                }
            ?>

如果有人能提供帮助,我真的很感激!

编辑:将$$ component_name更改为$ component_name。 (错字)

编辑:整页代码。

<!DOCTYPE html>
            <html>
            <head>
            <link rel="stylesheet" type="text/css"href="/custom/css/ag.css">
            </head>
            <body>
            <?php

                require_once("custom/php/common.php");

                if(!is_user_logged_in() && !current_user_can('manage_components'))
                {
                    echo "You don't have permission to access this page";
                }
                else
                {
                    if($_REQUEST['state'] == "")
                    {   
                        $verify_component = "SELECT * FROM concept";
                        $result_component = mysqli_query($conn, $verify_component);
                        if(mysqli_num_rows($result_component) == 0)
                        {
                                        echo "Não há componentes";
                        }
                        else
                        {
            ?>
                                        <table class="mytable">
                                        <thead><tr>
                                            <th>Type</th>
                                            <th>ID</th>
                                            <th>Name</th>
                                            <th>State</th>
                                            <th>Ação</th>
                                        </tr></thead>
                                        <!--<tbody>-->
            <?php

                                        $choice_comp_type = "SELECT * FROM conc_type";
                                        $resultado_comp_type = mysqli_query($conn, $choice_comp_type);
                                        while($type = mysqli_fetch_array($resultado_comp_type))
                                        {

                                            $choice_components = "SELECT component.*
                                                                                    FROM concept,conc_type
                                                                                    WHERE conc_type.id = concept.conc_type_id
                                                                                    AND conc_type_id = '".$type['id']."'";
                                            $result_components = mysqli_query($conn, $choice_components);
                                            $num_rows = mysqli_num_rows($result_components);

                                            if($num_rows > 0)
                                            {
            ?>
                                                <tr>
                                                <td class= "Name" colspan = "1" rowspan = "<?php echo $num_rows; ?>"> <?php echo $type['name']; ?> </td>

            <?php
                                                while($row = mysqli_fetch_array($result_components))
                                                {
            ?>
                                                    <td><?php echo $row['id'];?></td>
                                                    <td><?php echo $row['name'];?></td>
            <?php
                                                    if($row['state'] == "active")
                                                    {
            ?>
                                                            <td>active</td>
                                                            <td>[edit] [desactivate]</td>
            <?php
                                                    }
                                                    else
                                                    {
            ?>
                                                            <td>inactive</td>
                                                            <td>[edit] [activate]</td>
            <?php
                                                    }
            ?>
                                                </tr>
            <?php
                                                }   
                                            }
                                        }
            ?>
                                        <!--</tbody>-->
                                        </table>
            <?php
                            }
            ?>
                        <br>
                        <h3><center><b>Management Compontent - introduction</b></center></h3>
                        <br>
                        <form name = "registo" method = "POST" action = "">
                        <center><fieldset>
                                        <legend>Insert component</legend>
                                        <br>
                                        <p><b> Name: <input type = "text" name = "component_name" required></b> </p><br>

                                        <p> <b>Type:</b> </p>
                                        <div class = "mytable">
                                        <table><tr>
            <?php                            
                                    $queryComp_Type = "select id, name from conc_type order by name";
                                    $resultadoQueryComp_Type = mysqli_query($conn, $queryComp_Type);
                                    foreach ($resultadoQueryComp_Type as $rowComp_Type)
                                    {
            ?>
                                            <th><?php echo $rowComp_Type['name']; ?></th>  
            <?php
                                    }                      
            ?>
                                            </tr><tr>
            <?php
                                    foreach ($resultadoQueryComp_Type as $rowComp_Type)
                                    {
            ?>
                                            <td><input class= "radio" type= "radio" name= "component_type" value= "<?php echo $rowComp_Type['id']; ?>" required> </td>  
            <?php
                                    }
            ?>
                                        </tr></table>
                                        </div><br>
                                        <p> <b>State:</b></p> 
                                            <table class="mytable">
                                            <tr><th>active</th><th>Inactive</th></tr>
                                            <tr>
                                                <td> <input class= "radio" type= "radio" name= "component_state" value= "active" required></td>
                                                <td> <input class= "radio" type= "radio" name= "component_state" value= "inactive"></td>
                                            </tr>
                                            </table>
                                        <br>

                                        <!--Botão Inserir-->
                                        <input type= "hidden" name= "state" value= "inserir">
                                        <input class= "button" type= "submit" value= "Inserir Componente">
                                        <br><br>

                        </fieldset></center>
                        </form>
            <?php
                    }
                    else if($_REQUEST['state'] == "inserir")
                    {
            ?>
                        <h3>Management component - insertion</h3>
            <?php
                        $component_name = $_REQUEST['component_name'];
                        $component_type = $_REQUEST['component_type'];
                        $component_state = $_REQUEST['component_state'];


                                $insert = sprintf("INSERT INTO `concept` (`name`,`conc_type_id`,`state`) VALUES ('%s', '%s', '%s')", mysqli_real_escape_string($conn, $component_name), mysqli_real_escape_string($conn, $component_type), 
                                mysqli_real_escape_string($conn, $component_state));

                        $resultado_insert = mysqli_query($conn, $insert);

                        if($resultado_insert)
                        {
                            mysqli_query($conn,'COMMIT');
            ?>                             
                            <p>Successfull insertion</p>
                            <p>Click in <a href = "gestao-de-componentes">Continue</a> to advance.</p>
            <?php
                        }
                        else
                        {
                            mysqli_query($conn,'ROLLBACK');
            ?>
                            <p>Insertion error</p>
            <?php
                        }
                        back();
                    }
                }
            ?>
            </body>
            </html>

1 个答案:

答案 0 :(得分:0)

首先,当你创建一个表...得到一个查询结果......我认为应该是这样的:

 <div class = "mytable">
     <?php
     $queryComp_Type = "select id, name from conc_type order by name";
     $resultadoQueryComp_Type = mysqli_query($conn, $queryComp_Type);
     $tableHeader = "";
     $tableBody = "";
     ?>
    <table>
        <thead>
            <?php 
            while ($row = mysqli_fetch_array($resultadoQueryComp_Type)) {
                $tableHeader .= '<tr>
                                    <th>'.$row['name'].';</th>
                                </tr>';
            }
            ?>
            <?=$tableHeader?>
        </thead>
        <tbody>
            <?php 
            while ($row = mysqli_fetch_array($resultadoQueryComp_Type)) {
                $tableBody .= '<tr>
                                <td>'.$row['id'].'</td>
                              </tr>';
            }

            ?>
            <?=$tableBody?>
        </tbody>
    </table>
</div>