我创建了一个包含一些数据的数据库。我已经能够使用Jquery Mobile显示数据。唯一的问题是,当我来到我的编辑页面,更新数据,并返回主页时,网址保持不变。例如:
我在example.com/index.php --->点击指向example.com/index.php?id=1(或任何其他数字)的链接 - >更新数据并单击以返回带有更新数据的index.php,但url保留在example.com/index.php?id=1,其内容为index.php。我糊涂了!!这是我的2个文件:
的index.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>
edit.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!';
}
}
?>
任何人都可以提供帮助吗?
答案 0 :(得分: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');