使用php代码保存问题编辑数据

时间:2013-04-22 23:11:32

标签: php sql sql-update sql-delete

这是我的表格页面editFormpackage.php
我可以将我的数据恢复到一个表格进行更新,但是当我更改值并单击更新时,iformation不会保存。可以有人请帮忙!我是php的新手。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> AB Delivery </title>
<title> *Please enter details of employee took order. </title>
<link rel="stylesheet" type="text/css" href="style.css" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> AB Delivery </title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link href= rel='stylesheet' type='text/css'>
<!-- jQuery file -->
<script src="js/jquery.min.js"></script>
<script src="js/jquery.tabify.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var $ = jQuery.noConflict();
$(function() {
$('#tabsmenu').tabify();
$(".toggle_container").hide(); 
$(".trigger").click(function(){
    $(this).toggleClass("active").next().slideToggle("slow");
    return false;
});
});
</script>
</head>
<body>
<div id="panelwrap">

    <div class="header">
    <div class="title"><a href="#">AB Delivery</a></div>

    <div class="header_right">Welcome Admin </div>

    <div class="menu">
    <ul>
    <li><a href="http://localhost/delivery2/homepage.html" class="selected">New Entry</a></li>
    <li><a href="http://localhost/delivery2/showall.php">Show All</a></li>
    <li><a href="http://localhost/delivery2/edit.php">Edit</a></li>
    <li><a href="http://localhost/delivery2/search.html">Search</a></li>
    </ul>
    </div>


   <div class="center_content">  

    <div id="right_wrap">
    <div id="left_content">             
    <h2>Tables section</h2> 


<table id="rounded-corner">



<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("delivery", $con);

if(isset($_GET['package_id']))
{
$package_id=$_GET['package_id'];
if(isset($_POST['submit']))
{
$customer_id=$_POST['customer_id'];
$order_taken_by_employee_id=$_POST['order_taken_by_employee_id'];
$package_details=$_POST['package_details'];
$result=mysql_query("UPDATE package SET customer_id ='$customer_id',order_taken_by_employee_id='$order_taken_by_employee_id', package_details='$package_details' where package_id='$package_id'");
if($result)
{
header('location:edit.php');
}
}
$query1=mysql_query("SELECT * FROM package  WHERE package_id='$package_id'");
$query2=mysql_fetch_array($query1);
?>
<form method="post" action="http://localhost/delivery2/editFormpackage.php">
Customer ID:<input type="text" name="customer_id" value="<?php echo $query2['customer_id']; ?>" /><br />
Order Taken by Employee ID:<input type="text" name="order_taken_by_employee_id" value="<?php echo $query2['order_taken_by_employee_id']; ?>" /><br /><br />
Package Details:<input type="text" name="package_details" value="<?php echo $query2['package_details']; ?>" /><br /><br />
<br />
<input type="submit" name="submit" value="update" />
</form>


<?php
}
?>



 <div 



    <div class="clear"></div>
    </div> <!--end of center_content-->

    <div class="footer">
AB Delivery 
</div>

</div>


</body>
</html>

这是edit.php中的代码,您可以看到我为所有要更新的表创建了一个新页面。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> AB Delivery </title>
<title> *Please enter details of employee took order. </title>
<link rel="stylesheet" type="text/css" href="style.css" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> AB Delivery </title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link href= rel='stylesheet' type='text/css'>
<!-- jQuery file -->
<script src="js/jquery.min.js"></script>
<script src="js/jquery.tabify.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var $ = jQuery.noConflict();
$(function() {
$('#tabsmenu').tabify();
$(".toggle_container").hide(); 
$(".trigger").click(function(){
    $(this).toggleClass("active").next().slideToggle("slow");
    return false;
});
});
</script>
</head>
<body>
<div id="panelwrap">

    <div class="header">
    <div class="title"><a href="#">AB Delivery</a></div>

    <div class="header_right">Welcome Admin </div>

    <div class="menu">
    <ul>
    <li><a href="http://localhost/delivery2/homepage.html" class="selected">New Entry</a></li>
    <li><a href="http://localhost/delivery2/showall.php">Show All</a></li>
    <li><a href="http://localhost/delivery2/edit.php">Edit</a></li>
    <li><a href="http://localhost/delivery2/search.html">Search</a></li>
    </ul>
    </div>


   <div class="center_content">  

    <div id="right_wrap">
    <div id="left_content">             
    <h2>Tables section</h2> 


<table id="rounded-corner">


<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("delivery", $con);

$result = mysql_query("SELECT * FROM customers");



echo "<table border='19'>
<tr>
<th>Customer ID</th>
<th>Customer Address ID</th>
<th>Name</th>
<th>Phone Number</th>
<th>Email</th>
<th>Action</th>
</tr>";

while($row1 = mysql_fetch_array($result))

  {
  echo "<tr>";
  echo "<td>" . $row1['customer_id'] . "</td>";
  echo "<td>" . $row1['customer_address_id'] . "</td>";
  echo "<td>" . $row1['customer_name'] . "</td>";
  echo "<td>" . $row1['customer_phone'] . "</td>";
  echo "<td>" . $row1['customer_email'] . "</td>";
  echo "<td><a href='editFormcustomers.php?customer_id=".$row1['customer_id']."'>Edit</a></td>";
echo "<td><a href='deletecustomers.php?customer_id=".$row1['customer_id']."'>x</a></td><tr>";
  }


echo "</tr>";

echo "</table>";

mysql_close($con);

?>

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("delivery", $con);

$result = mysql_query("SELECT * FROM address");



echo "<table border='19'>
<tr>
<th>Address ID</th>
<th>Address </th>
<th>Action</th>
</tr>";

while($row2 = mysql_fetch_array($result))

  {
  echo "<tr>";
  echo "<td>" . $row2['address_id'] . "</td>";
  echo "<td>" . $row2['line_1'] . "</td>";
  echo "<td><a href='editFormaddress.php?address_id=".$row2['address_id']."'>Edit</a></td>";
echo "<td><a href='deleteaddress.php?address_id=".$row2['address_id']."'>x</a></td><tr>";
  }


echo "</tr>";

echo "</table>";

mysql_close($con);

?>

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("delivery", $con);

$result = mysql_query("SELECT * FROM package");



echo "<table border='19'>
<tr>
<th>Packasge ID</th>
<th> Customer ID</th>
<th>Employee ID</th>
<th>Details</th>
<th>Action</th>
</tr>";

while($row3 = mysql_fetch_array($result))

  {
  echo "<tr>";
  echo "<td>" . $row3['package_id'] . "</td>";
  echo "<td>" . $row3['customer_id'] . "</td>";
  echo "<td>" . $row3['order_taken_by_employee_id'] . "</td>";
  echo "<td>" . $row3['package_details'] . "</td>";
    echo "<td><a href='editFormpackage.php?package_id=".$row3['package_id']."'>Edit</a></td>";
echo "<td><a href='deletepackage.php?package_id=".$row3['package_id']."'>x</a></td><tr>";


  }


echo "</tr>";

echo "</table>";

mysql_close($con);

?>

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("delivery", $con);

$result = mysql_query("SELECT * FROM employee");



echo "<table border='19'>
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Phone Number</th>
<th>Other Details</th>
<th>Action</th>
</tr>";

while($row4 = mysql_fetch_array($result))

  {
  echo "<tr>";
  echo "<td>" . $row4['employee_id'] . "</td>";
  echo "<td>" . $row4['employee_name'] . "</td>";
  echo "<td>" . $row4['employee_phone'] . "</td>";
  echo "<td>" . $row4['other_employee_details'] . "</td>";
    echo "<td><a href='editFormemployee.php?employee_id=".$row4['employee_id']."'>Edit</a></td>";
echo "<td><a href='deleteemployee.php?employee_id=".$row4['employee_id']."'>x</a></td><tr>";

  }


echo "</tr>";

echo "</table>";


mysql_close($con);

?>


  <div 



    <div class="clear"></div>
    </div> <!--end of center_content-->

    <div class="footer">
AB Delivery 
</div>

</div>


</body>
</html>

2 个答案:

答案 0 :(得分:0)

更新脚本中的where子句引用了包ID,但是在表单中没有设置包ID。

$result=mysql_query("UPDATE package SET customer_id ='$customer_id',order_taken_by_employee_id='$order_taken_by_employee_id', package_details='$package_details' where package_id='$package_id'");

在我们找到解决方案之前,我必须指出您极易受到SQL注入攻击,因为在使用它直接访问数据库之前,您没有清理表单输入。检查一下,以帮助您在http://us2.php.net/manual/en/function.mysql-real-escape-string.php

找到正确的轨道

下面的解决方案可行,但在POST中传递包ID在技术上也可能被黑客攻击。在服务器或数据库会话中传递ID是最安全的(Most secure way to pass variables between two pages with PHP),但是下面的解决方案将帮助您解决问题。

您正在从GET查询参数设置包ID,但是一旦您的表单提交,它将提交到表单action参数中定义的URL,如您所见,它仍然不包含包ID。

要解决此问题,您应该将package_id添加为隐藏的表单变量,如下所示:

<form method="post" action="http://localhost/delivery2/editFormpackage.php">
 Customer ID:<input type="text" name="customer_id" value="<?php echo     $query2['customer_id']; ?>" /><br />
Order Taken by Employee ID:<input type="text" name="order_taken_by_employee_id" value="<?php echo $query2['order_taken_by_employee_id']; ?>" /><br /><br />
Package Details:<input type="text" name="package_details" value="<?php echo $query2['package_details']; ?>" /><br /><br />
<br />
<input type="hidden" name="package_id" value="<?php echo $query2['package_id'];?>" />
<input type="submit" name="submit" value="update" />
</form>

然后在$ _POST超全局中的package_id中设置package_id,如下所示:

if(isset($_POST['submit']))
{
$package_id = $_POST['package_id'];
$customer_id=$_POST['customer_id'];
$order_taken_by_employee_id=$_POST['order_taken_by_employee_id'];
$package_details=$_POST['package_details'];
$result=mysql_query("UPDATE package SET customer_id ='$customer_id',order_taken_by_employee_id='$order_taken_by_employee_id', package_details='$package_details' where package_id='$package_id'");
// rest of your code

答案 1 :(得分:0)

这是正确的代码:

 <form method="post" action="http://localhost/delivery2/editFormpackage.php">
         Customer ID:<input type="text" name="customer_id" value="<?php echo     $query2['customer_id']; ?>" /><br />
        Order Taken by Employee ID:<input type="text" name="order_taken_by_employee_id" value="<?php echo $query2['order_taken_by_employee_id']; ?>" /><br /><br />
        Package Details:<input type="text" name="package_details" value="<?php echo $query2['package_details']; ?>" /><br /><br />
        <br />
        <input type="hidden" name="package_id" value="<?php echo $query2['package_id'];?>" />
        <input type="submit" name="submit" value="update" />
        </form>