我有4个页面:register.php,view.php,edit.php和display.php 在view.php上,我有一个显示数据库中所有数据的表单。 我有一个搜索框,其结果显示在display.php中(它只显示数据库中的一行)。 在显示php我有一个带有编辑按钮的表单。编辑按钮将我重定向到edit.php,我可以在其中更改我的数据。当我保存时,它将我重定向到view.php但我想在显示php中保存已编辑条目的值。 我试过但它干了工作。 我在这里遇到的第二个问题是在edit.php中保留我在register.php中选择的下拉选项
我;我的新编程,我希望有人能帮助我解决这个问题。 THX寻求帮助。
我的网页:
Display.php的
<?php
include('connect-db.php');
$client = $_POST['client'];
$contract = $_POST['contract'];
if(
$contract = $_POST['contract'] )
{$query = "select * from users where contract = '$contract'"; }
else{
$query= "select * from users where client = '$client'";
}
$result = mysql_query($query);
echo "<table>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
// echo out the contents of each row into a table
echo '<label>Contract</label><input readonly="true" value=' . $row['contract'] . '>';
echo '<label>Client</label><input readonly="true" type="text" value="' . $row['client'] . '">';
echo '<label>Step</label><input type="text" readonly="true" value=' . $row['Step'] . '>';
echo '<br><a href="edit.php?contract=' . $row['contract'] . '"><input type="submit"value="Change"></a>';
echo '<br>';
echo "</table>";
}
?>
&#13;
EDIT.PHP
<?php
function renderForm($contract, $client, $step
{
?>
<!DOCTYPE html>
<body>
<form id="base" name="base" method="post" action="">
<input data-validate="number" value="<?php echo $contract; ?>" readonly="true" id="contract">
<input data-validate="text" value="<?php echo $client; ?>" id="client">
<select name="step">
<option value="option1">Option1</option>
<option value="option2">Option2</option>
</select>
<input type="submit" value="Change">
</form>
</body>
</html>
<?php
}
include('connect-db.php');
if (isset($_POST['submit']))
{
if (is_numeric($_POST['contract']))
{
$contract = mysql_real_escape_string(htmlspecialchars($_POST['contract']));
$client = mysql_real_escape_string(htmlspecialchars($_POST['client']));
$step = mysql_real_escape_string(htmlspecialchars($_POST['step']));
if ($contract == '' || $client == '' )
{
$error = 'ERROR: Please fill in all required fields!';
renderForm($contract, $client, $step, $error);
}
else
{
mysql_query("UPDATE users SET
contract='$contract', client='$client', step='$step', WHERE contract='$contract'")
or die(mysql_error());
header("Location: view.php");
}
}
else
{
echo 'Error!';
}
}
else
{
if (isset($_GET['contract']) && is_numeric($_GET['contract']) && $_GET['contract'] > 0)
{
$contract = $_GET['contract'];
$result = mysql_query("SELECT * FROM users WHERE contract=$contract")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($row)
{
$contract = $row['contract'];
$client = $row['client'];
$step = $row['step'];
renderForm($contract, $client, $step, '');
}
else
{
echo "No results!";
}
}
else
{
echo 'Error!';
}
}
?>
&#13;
VIEW.PHP
<!DOCTYPE html>
<html>
<?php
include('connect-db.php');
$sql="SELECT * FROM users";
$result =mysql_query($sql);
{
?>
<table>
<thead>
<tr>
<th span style="font-weight: normal;">Contrat</th>
<th span style="font-weight: normal;">Client</th>
<th span style="font-weight: normal;">Step</th>
</tr>
</thead>
<?php
}
while ($data=mysql_fetch_assoc($result)){
?>
<tbody>
<tr>
<td><?php echo $data['contract'] ?></td>
<td><?php echo $data['client'] ?></td>
<td><?php echo $data['step'] ?></td>
<td><a href="edit.php?contract=<?php echo $data['contract'] ?>"><input type="button" value="Change"></a></td>
</tr> <?php } ?>
</tbody>
</table>
</body>
</html>
&#13;
REGISTER.PHP
<!DOCTYPE html>
<html>
<form id="base" method="post" action="insert.php">
<br>
<br>
<input data-validate="number" id="contract" name="contract">
<input data-validate="text" id="client" name="client">
<select name="step">
<option value="option1">OPTION1</option>
<option value="option2">OPTION2</option>
</select>
<button data-validate="submit">Register</button>
</form>
</body>
</html>
&#13;
答案 0 :(得分:1)
use this instead of your edit.php
<?php
function renderForm($contract, $client, $step
{
?>
<!DOCTYPE html>
<body>
<form id="base" name="base" method="post" action="">
<input data-validate="number" value="<?php echo $contract; ?>" readonly="true" id="contract">
<input data-validate="text" value="<?php echo $client; ?>" id="client">
<select name="step">
<option value="option1">Option1</option>
<option value="option2">Option2</option>
</select>
<input type="submit" value="Change">
</form>
</body>
</html>
<?php
}
include('connect-db.php');
if (isset($_POST['submit']))
{
if (is_numeric($_POST['contract']))
{
$contract = mysql_real_escape_string(htmlspecialchars($_POST['contract']));
$client = mysql_real_escape_string(htmlspecialchars($_POST['client']));
$step = mysql_real_escape_string(htmlspecialchars($_POST['step']));
if ($contract == '' || $client == '' )
{
$error = 'ERROR: Please fill in all required fields!';
renderForm($contract, $client, $step, $error);
}
else
{
mysql_query("UPDATE users SET
contract='$contract', client='$client', step='$step', WHERE contract='$contract'")
or die(mysql_error());
header("Location:display.php");
}
}
else
{
echo 'Error!';
}
}
else
{
if (isset($_GET['contract']) && is_numeric($_GET['contract']) && $_GET['contract'] > 0)
{
$contract = $_GET['contract'];
$result = mysql_query("SELECT * FROM users WHERE contract=$contract")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($row)
{
$contract = $row['baza_contract'];
$client = $row['baza_client'];
$step = $row['step'];
renderForm($contract, $client, $step, '');
}
else
{
echo "No results!";
}
}
else
{
echo 'Error!';
}
}
?>