SQL更新查询不合作

时间:2013-11-20 07:10:29

标签: php html mysql css mysqli

我在管理面板上的编辑表单底部添加了一个保存/更新按钮。出于某种原因,每当我对表单进行更改并单击“保存”时,只需重新加载页面而不进行任何更改。我还注意到,只有当我尝试运行pages.php文件中的代码时(从索引运行然后单击页面就可以了),它说:

  

未定义的变量:dbc in   C:\用户\杰克\桌面\ XAMPP \ htdocs中\ jakefordsite \ ADMIN \内容\ pages.php   在第12行

     

警告:mysqli_query()期望参数1为mysqli,null给定   在   C:\用户\杰克\桌面\ XAMPP \ htdocs中\ jakefordsite \ ADMIN \内容\ pages.php   在第12行

我可以通过在pages.php中声明一个新的$ dbc(databaseconnection)变量来摆脱这个错误,但是我在更新表单数据时仍遇到同样的问题。

PAGES.PHP:

<?php ## Page Manager ?>

<h2>Page Manager</h2>

<div class="col sidebar">

    <ul>

        <?php

           $q = "SELECT * FROM pages ORDER BY name ASC"; 
           $r = mysqli_query($dbc, $q);

           if ($r)
           {
                while ($link = mysqli_fetch_assoc($r))
                {
                    echo '<li><a href="?page=pages&id='.$link['id'].'">'.$link['name'].'</a></li>';
                }
           }

        ?>

    </ul>
</div>

<div class="col editor">

    <?php

     if (isset($_POST['submitted']) == 1) { 

            $q = "UPDATE pages SET title='$_POST[title]', name='$_POST[name]', body='$_POST[body]', WHERE id = '$_POST[id]'"; 
            $r = mysqli_query($dbc, $q);

     }

     if (isset($_GET['id'])) { 

            $q = "SELECT * FROM pages WHERE id = '$_GET[id]' LIMIT 1";
;  
            $r = mysqli_query($dbc, $q);
            $opened = mysqli_fetch_assoc($r);


    ?>

        <form action="?page=pages&id=<?php echo $opened['id'] ?>" method="post">

            <p><label>Page title: </label><input type="text" size="30" name="title" value="<?php echo $opened['title']?>"></p>
            <p><label>Page name:</label> <input type="text" size="30" name="name" value="<?php echo $opened['name']?>"></p> 

            <label>Page body: </label><br>
            <textarea name="body" cols="30" rows="8"><?php echo $opened['body'] ?></textarea>

               <input type="hidden" name="submitted" value="1"/>
               <input type="hidden" name="id" value="<?php echo $opened['id'] ?>"/>

             <p><input type="submit" name="submit" value="Save Changes"/></p>

        </form>

    <?php } ?>

</div>

的index.php:

<?php
// Setup document:
include('config/setup.php');
?>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title><?php //echo $page_title; ?>JakeForDesign - Admin Panel</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">

</head>

<body>

<div class="wrap_overall">

    <div class="header"> <?php head(); ?> </div>

    <div class="nav_main"> <?php nav_main(); ?> </div>

    <div class="content"> <?php include('content/'.$pg.'.php'); ?> </div>

    <div class="footer"> <?php footer(); ?> </div>

</div>    

</body>
</html>

SETUP.PHP

<?php
## Setup Document

// host(or location of the database), username, //password, database name

$dbc = @mysqli_connect('127.0.0.1', 'root', 'password', 'main') OR die ('Could not connect to the database because: '. mysqli_connect_error() );

include('Functions/sandbox.php');
include('Functions/template.php');

if (isset($_GET['page']) == '')
{
    $pg = 'home';       
}
else
{
    $pg = $_GET['page'];
}

$page_title = get_page_title($dbc, $pg);

?>

1 个答案:

答案 0 :(得分:0)

Pages.php 你有

$r = mysqli_query($dbc, $q);

$q没问题,但你没有提到$dbc

setup page上,创建一个连接类,声明connection method,然后在 PAGES.PHP

$db_obj =  new setup();  /* create object for setup class */
$dbc = $db_obj -> connect_db();/* call connection method */