douduan3203 2014-04-30 06:06
浏览 89

无法从数据库中获取数据

I am trying to build up a system where I have web pages content generated by a user in a form, saved in a database, make a list of all the pages existing in the database and by clicking on that list editing the desired page.

The only thing that is not working is the editing part. It seems that I cannot grab the data from the database because when I click on a page to edit, the "template" has nothing in the forms.

Here is the code:

Page with the list of pages existing on the database:

<?php
    require_once "../scripts/conector.php";

    if (!$_GET['pid']) {
    $pageid = '1';
    } else {
        $pageid = preg_replace('/[^0-9]/', "", $_GET['pid']); 
    }

    $sqlCommand = "SELECT id, producent FROM pages WHERE showing='1' ORDER BY id ASC"; 
    $query = mysqli_query($myConnection, $sqlCommand) or die('Error: ' . mysqli_error($myConnection)); 

    $producentnamn = '';
    while ($row = mysqli_fetch_array($query)) { 
        $pid = $row["id"];
        $producent = $row["producent"];
        $producentnamn .= '<a href="edit_page.php?pid=' . $pid . '">' . $producent . '</a>';
    } 
    mysqli_free_result($query);
?>

<form id="form1" name="form1" method="post" action="edit_page.php">
    <input type="submit" name="button2" id="button2" class="botao" value="Redigera" />
    <input name="pid" type="text" id="pid" value="<?php echo $producentnamn; ?>"/>
</form>

edit_page.php

<?php
    $pid = ereg_replace("[^0-9]", "", $_POST['pid']);  

    include_once "../scripts/conector.php";
    $sqlCommand = "SELECT pagetitle, linklabel, evenemang, presentation, producent, pagebody, mapa FROM pages WHERE id='$pid' LIMIT 1";  
    $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
    while ($row = mysqli_fetch_array($query)) { 
        $pagetitle = $row["pagetitle"];
        $linklabel = $row["linklabel"];
        $evenemang = $row["evenemang"];
        $presentation = $row["presentation"];
        $producent = $row["producent"];
        $pagebody = $row["pagebody"];
        $mapa = $row["mapa"];
    } 
    mysqli_free_result($query);
?>

<label>Map</label>
<textarea name="mapa" id="mapa"><?php echo $mapa; ?></textarea>

So, what am I doing wrong here? Many thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongrang2140 2014-04-30 06:11
    关注

    Try changing this line

    <input name="pid" type="text" id="pid" value="<?php echo $producentnamn; ?>"/>
    

    to

    <input name="pid" type="text" id="pid" value="<?php echo $pid; ?>"/>
    

    Because you are passing <a href="edit_page.php?pid=' . $pid . '"> to the form. which I think doesnt exist in the db. You got to pass the value/id of the page to the form.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大