dongyan6235 2016-12-14 15:15
浏览 10
已采纳

插入错误(PHP)[关闭]

I've been looking for a solution to this problem, but with no luck. Basically when inserting an item in a table it doesn't return a result (doesn't insert anything). I've checked the table fields and all match, I can't seem to find what error I'm doing.

Here's the portion of the code I currently have, where the problem occurs.

            <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();
                    }
                }
            ?>

If anyone could help I'd be really grateful!

EDIT: Changed $$component_name to $component_name. (Typo)

EDIT: Full page code.

<!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条回答 默认 最新

  • dry18813 2016-12-14 15:38
    关注

    First when you create a table... get a result of query... i think should be like this :

     <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在