从db表中检索数据以填充链接

时间:2014-11-28 20:16:46

标签: php

抱歉,我不知道如何描述我遇到的这个问题的标题。 :/我正在创建一个CRUD应用程序,我正处于更新阶段。它正在奇妙地更新数据库中的数据。

在更新数据库中的信息的过程中,我创建了链接以检索db表中的数据。我正在通过id调用列表(自动递增)我可以看到表中的所有id号,但是当它显示在网页上时,它只显示表中的最后一个id号,但显示全部报告名称正确。我不知道为什么。

很抱歉这很难解释,我已经附上了一个用户名和密码以及网站代码片段,希望有人可以看到我无法看到的内容。

http://consolidatedgypsum.ca/cgs-new-site/admin/project-leads/index.php 用户:Test1 密码:test1234 要进入更新页面,您需要单击更新报告。

这是我的更新页面:

<?php include("includes/session.php"); ?>

<?php include("includes/reports_mysql_connect.php"); ?>
<?php

    $id = $_GET['id'];

    if(!isset($id)){
        $x = mysql_query('SELECT id FROM Reports ORDER BY name') or die(mysql_error());
        while($row = mysql_fetch_array($x)){
            $id = $row['id'];
        }
    }

    //step 3: user submits changes, updates DB to show new information
    if(isset($_POST['submit'])){

        $new_sale = $_POST['sale'];
        $new_questiontwo = $_POST['questiontwo'];
        $new_questionthree = $_POST['questionthree'];
        $new_questionfour = $_POST['questionfour'];
        $new_questionfive = $_POST['questionfive'];
        $new_questionsix = $_POST['questionsix'];
        $new_questionseven = $_POST['questionseven'];
        $new_questioneight = $_POST['questioneight'];

        echo "$new_sale | $new_questiontwo" ; 

        mysql_query("UPDATE Reports SET

            sale = '$new_sale',
            questiontwo = '$new_questiontwo',
            questionthree = '$new_questionthree',
            questionfour = '$new_questionfour',
            questionfive = '$new_questionfive',
            questionsix = '$new_questionsix',
            questionseven = '$new_questionseven',
            questioneight = '$new_questioneight'

            WHERE id = '$id' ") or die(mysql_error());
    }


    //step 1: Retrieve data to create links
    $result = mysql_query("SELECT * FROM Reports") or die(mysql_error());

    while($row = mysql_fetch_array($result)){
        $sale = $row['sale'];
        $questiontwo = $row['questiontwo'];
        $questionthree = $row['questionthree'];
        $questionfour = $row['questionfour'];
        $questionfive = $row['questionfive'];
        $questionsix = $row['questionsix'];
        $questionseven = $row['questionseven'];
        $questioneight = $row['questioneight'];

        //$id = $row['id'];
        //echo $id;

        echo "<div> $id <a href=\"updated-reports.php?id=$id\">$sale</a></div>";
    }

    //Step 2: Retrieve Data for selected report only to prepopulated form.

    $result = mysql_query("SELECT * FROM Reports WHERE id = '$id'") or die(mysql_error());
    while($row = mysql_fetch_array($result)){

        $thissale = $row['sale'];
        $thisquestiontwo = $row['questiontwo'];
        $thisquestionthree = $row['questionthree'];
        $thisquestionfour = $row['questionfour'];
        $thisquestionfive = $row['questionfive'];
        $thisquestionsix = $row['questionsix'];
        $thisquestionseven = $row['questionseven'];
        $thisquestioneight = $row['questioneight'];

        //$thisid = $row['id'];
        //echo $id;
    }

?>



<?php include("../includes/admin-leads-header.php") ?>

<div class="container">
    <div class="sixteen columns">
         <section class="fifteen columns">
            <div class="general">
                <div class="project-leads">

                    <?php

                        if($session->logged_in){
                        if($session->isAdmin()){
                        }

                        echo "<h3>Hello $session->username</h3>";
                        echo "<h5>Please enter your sales leads reports</h5>";
                    ?>

                    <div class="project-leads-reports">
                        <form name="contactform" id="contactform" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
                            <ul>
                                <li>
                                    <span class="required">*</span>
                                    <label for="name">From: </label>
                                    <input type="text" name="name" id="name" value="<?php echo $session->username?>" />
                                    <span class="error"></span>
                                </li>
                                <li>
                                    <span class="required">*</span>
                                    <label for="email">E-mail Address: </label>
                                    <input type="text" name="email" id="email" value="<?php if($form->value("email") == ""){
                                            echo $session->userinfo['email'];
                                        }else{
                                            echo $form->value("email");
                                        }?>" />
                                    <span class="error"></span>
                                </li>
                                <li>
                                    <span class="required">*</span>
                                    <label for="boss">Regional Sales Manager E-mail Address:</label>
                                    <input type="text" name="boss" id="boss" value="<?php if($form->value("boss") == ""){
                                            echo $session->userinfo['boss'];
                                        }else{
                                            echo $form->value("boss");
                                        }?>" />
                                    <span class="error"></span>
                                </li>
                                <li>
                                    <span class="required">*</span>
                                    <label for="Sales">Sales Lead Subject:</label>
                                    <input type="text" name="sale" id="sale" value="<?php echo trim($thissale);?>" />
                                    <span class="error"></span>
                                </li>
                                <li>
                                    <span class="required">*</span>
                                    <label for="questiontwo">When did you followup with the General Contractor?</label>
                                    <textarea name="questiontwo"  id="questiontwo" ><?php echo trim($thisquestiontwo);?></textarea>
                                    <span class="error"><?php echo $strValidationMessage2; ?></span>
                                </li>
                                <li>
                                    <span class="required">*</span>
                                    <label for="questionthree">Who is/are the contractors working under the General Contractor?</label>
                                    <textarea name="questionthree" id="questionthree"><?php echo trim($thisquestionthree);?></textarea>
                                    <span class="error"><?php echo $strValidationMessage3; ?></span>
                                </li>
                                <li>
                                    <span class="required">*</span>
                                    <label for="questionfour">Did the General Contractor have their own subcontractors, or did we refer our contractor customers to them?</label>
                                    <textarea name="questionfour"  id="questionfour"><?php echo trim($thisquestionfour);?></textarea>
                                    <span class="error"><?php echo $strValidationMessage4; ?></span>
                                </li>
                                <li>
                                    <span class="required">*</span>
                                    <label for="questionfive">Can we or did we submit a price?</label>
                                    <textarea name="questionfive" id="questionfive" ><?php echo trim($thisquestionfive);?></textarea>
                                    <span class="error"><?php echo $strValidationMessage5; ?></span>
                                </li>
                                <li>
                                    <span class="required">*</span>
                                    <label for="questionsix">Did we win and why did we win or did we lose or why did we lose?</label>
                                    <textarea name="questionsix" id="questionsix" ><?php echo trim($thisquestionsix);?></textarea>
                                    <span class="error"><?php echo $strValidationMessage6; ?></span>
                                </li>
                                <li>
                                    <span class="required">*</span>
                                    <label for="questionseven">What is being done to progress any sales forward with this Project Lead?</label>
                                    <textarea name="questionseven" id="questionseven" ><?php echo trim($thisquestionseven);?></textarea>
                                    <span class="error"><?php echo $strValidationMessage7; ?></span>
                                </li>
                                <li>
                                    <span class="required"></span>
                                    <label for="questioneight">Did you know about this project before it was assigned and or any additional comments?</label>
                                    <textarea name="questioneight"  id="questioneight"><?php echo trim($thisquestioneight);?></textarea>
                                    <span class="error"></span>
                                </li>
                                <li>
                                    <input type="submit" name="submit" id="submit" value="Submit" />
                                </li>
                            </ul>
                        </form>
                    </div><!--end of project lead reports -->   


                <?php }?>



                </div><!--end of project-leads-->
            </div><!--end of general-->
        </section>
    </div> 
</div>


<?php include("../includes/admin_footer.php") ?>

我的SQL DB表:

Valid XHTML http://consolidatedgypsum.ca/cgs-new-site/admin/project-leads/68.178.136.47.png。 链接到图片:http://consolidatedgypsum.ca/cgs-new-site/admin/project-leads/68.178.136.47.png

图像在这里看起来不太好。

我还有第二个问题,与这个问题有关。当用户登录例如“Test1”时,有没有办法只显示数据库中的测试报告?

以下是Reports和用户的表结构。

Valid XHTML http://consolidatedgypsum.ca/cgs-new-site/admin/project-leads/users.pnghttp://consolidatedgypsum.ca/cgs-new-site/admin/project-leads/users.png

Valid XHTML http://consolidatedgypsum.ca/cgs-new-site/admin/project-leads/Reports.pnghttp://consolidatedgypsum.ca/cgs-new-site/admin/project-leads/Reports.png

提前感谢...自昨天以来我一直试图解决这个问题。

1 个答案:

答案 0 :(得分:2)

在输出报告的while循环中,$ id等于它在页面顶部设置的位置。在循环中尝试以下内容:

echo '<div>' . $row['id'] . '<a href="updated-reports.php?id=' . $row['id'] . '">' . $sale . '</a></div>';

谢谢,

安德鲁

相关问题