0

我创建了一个包含一些数据的数据库。我已经能够使用 Jquery Mobile 显示数据。唯一的问题是当我来到我的编辑页面,更新数据并返回主页时,url 保持不变。例子:

我在 example.com/index.php ---> 点击指向 example.com/index.php?id=1(或任何其他数字)的链接 --> 更新数据并点击返回index.php 与更新的数据,但 url 停留在 example.com/index.php?id=1 与 index.php 的内容。我很困惑!!这是我的 2 个文件:

索引.php

  <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />

<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>

 </head>
 <body>


<div data-role="page">
    <div data-role="header">
        <h3>
            AmateurStandenLive
        </h3>
    </div>
    <div data-role="content">
        <div data-role="collapsible-set" data-collapsed="true" >
                 <div data-role="collapsible" data-collapsed="true" data-theme="a">
                <h3>Oost</h3>


                <div data-role="collapsible" data-collapsed="true" data-theme="b">
                <h3>4G</h3>

                <ul data-role="listview" >
                    <?php
                try
                {
                    $connection = mysql_connect("localhost","zomervak_amalive","***");
                    mysql_select_db("***", $connection);
                    $result = mysql_query("SELECT * FROM zattop");

                    while($row = mysql_fetch_array($result))
                    {
                        echo "<li><a href='edit.php?id=" . $row['id'] . "'><h2>" . $row['thuis'] . " - "  . $row['uit'] . "</h2><h3>" . $row['thuisscore'] . " - " . $row['uitscore'] . "</h3></a></li>";
                    }

                    mysql_close($connection);
                }
                catch(Exception $e)
                {
                    echo $e->getMessage();
                }
            ?>
                </ul>
                </div>
            </div>
          <div data-role="collapsible" data-collapsed="true" data-theme="a">
                <h3>West</h3>  
                <div data-role="collapsible" data-collapsed="true" data-theme="b">
                <h3>4B</h3>
      </div>
        </div>

    </div>

编辑.php

 </head>
 <body>
 <?php

 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?>

 <form action="" method="post">
 <input type="hidden" name="id" value="<?php echo $id; ?>"/>

 <table border="1">
 <tr>
 <td colspan="2"><b><font color='Red'>Edit Records </font></b></td>
   </tr>
 <tr>
 <td width="179"><b><font color='#663300'>Name<em>*</em></font></b></td>
 <td><label>
 <input type="text" name="thuisscore" value="<?php echo $thuisscore; ?>" />
 </label></td>
 </tr>

 <tr>
 <td width="179"><b><font color='#663300'>Address<em>*</em></font></b></td>
 <td><label>
 <input type="text" name="uitscore" value="<?php echo $uitscore; ?>" />
 </label></td>
 </tr>


 <tr align="Right">
 <td colspan="2"><label>
 <input type="submit" name="submit" value="Edit Records">
  </label></td>
 </tr>
 </table>
 </form>
 </body>
 </html>
 <?php
  }


 /* Database Connection */

 $sDbHost = 'localhost';
 $sDbName = '***';
 $sDbUser = 'zomervak_amalive';
 $sDbPwd = '***';

 $dbConn = mysql_connect ($sDbHost, $sDbUser, $sDbPwd) or die ('MySQL connect failed. ' . mysql_error());
 mysql_select_db($sDbName,$dbConn) or die('Cannot select database. ' . mysql_error());


if (isset($_POST['submit']))
{

if (is_numeric($_POST['id']))
{

$id = $_POST['id'];
$thuisscore = mysql_real_escape_string(htmlspecialchars($_POST['thuisscore']));
$uitscore = mysql_real_escape_string(htmlspecialchars($_POST['uitscore']));



if ($thuisscore == '' || $uitscore == '' )
{

$error = 'ERROR: Please fill in all required fields!';


valid($id, $thuisscore, $uitscore, $error);
}
else
{

mysql_query("UPDATE zattop SET thuisscore='$thuisscore', uitscore='$uitscore' WHERE     id='$id'")
or die(mysql_error());

header('Location: index.php');
}
}
else
{

echo 'Error!';
}
}
else

{

if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{

$id = $_GET['id'];
$result = mysql_query("SELECT * FROM zattop WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);

if($row)
{

$thuisscore = $row['thuisscore'];
 $uitscore = $row['uitscore'];


valid($id, $thuisscore, $uitscore,'');
}
else
{
echo "No results!";
}
}
else

{
echo 'Error!';
}
}
?>

任何人都可以帮忙吗?

4

1 回答 1

0

您收到未定义的错误,这意味着尚未设置某些变量。

问题在于您的输入字段。名称未设置。

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

     <div data-role="fieldcontain">
     <label for="name"><b>Uitteam:</b></label>
     <input type="text" name="name" id="name" value="<?php echo $uitscore; ?>"  />

请注意,它们都具有相同的名称 'name="name"',您稍后要求这些作为 POST 变量:

$thuisscore = mysql_real_escape_string(htmlspecialchars($_POST['thuisscore']));

$uitscore = mysql_real_escape_string(htmlspecialchars($_POST['uitscore']));

但它们不存在。

所以试试这个。

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

     <div data-role="fieldcontain">
     <label for="name"><b>Uitteam:</b></label>
     <input type="text" name="uitscore" id="name" value="<?php echo $uitscore; ?>"  />

同样在页面顶部初始化 $error 变量 ($error = null;)

请记住将重定向更改为 view.php 而不是 index.php

header('Location: index.php'); // becomes header('Location: view.php');
于 2013-01-13T01:08:32.140 回答