使用php中的编辑和删除图像删除和更新行

时间:2015-03-22 08:53:03

标签: php mysql

这是我的branch.php页面,当用户点击php中的编辑和删除图像时,我想要删除和更新行,直接在表中删除或更新该行。但它不能正常工作正在更新或删除。

<?php
$a1=mysql_connect('localhost','root1','oec@123');
$b1=mysql_select_db('rms',$a1);
?> 
  <!doctype html>
  <html>
  <head>
  <meta charset="utf-8">
    <title>branch</title>
     <link href="styles/branch.css" rel="stylesheet" type="text/css">
     </head>

      <body>

   <div id="wrapper">
   <header id="top">
   <h1>Reporter Management System</h1>
    <nav id="mainnav">
    <ul>
    <li><a href="admin.php">Home</a></li>
    <li><a href="branch.php">Branch</a></li>
    <li><a href="branchadmin.php">Branch Admin</a></li>
    <li><a href="reporter.php">Reporters</a></li>
    <li><a href="accountant.php">Accountant</a></li>
    <li><a href="setting.php">Settings</a></li>
    <li><a href="">Log out</a></li>
    </ul>
    </nav>
    </header>
    <div id="hero">
    <img src="News2.jpg" width="1000" height="320"  alt=""/>  </div>
    <form  role="form"  action ="searchbranch.php" method="post">
    <p><label for="Search">Search Branch :</label>
    <input class="textbox" type="Search" placeholder="Name" name="Search">
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="submit"  class="classname" value="Search" >

       </form>
      <form role= "form" action="createbranch.php" method="post">
       <input type="submit"  class="classname" value="Add Branch"                       onClick="href 'createbranch.php' " ></p>
       </form>
        <section class="page container">
        <div>
       <div class="span12">
       <div class="box pattern ">
       <div class="box-header">
        <i class="icon-list"></i>

        </div>
        <table id="sample-table" class="table table-hover table-bordered  tablesorter">
          <thead>
         <tr>
         <th>Sl no</th>
         <th>Branch</th>
         <th>Description</th>
         <th>Action</th>
          </tr>
          <?php
          $w='SELECT Sl_no, branch, description from create_branch;';
          $recordset=mysql_query($w,$a1) or die(mysql_error());

          $totalrows=mysql_num_rows($recordset);
          $row_recordset=mysql_fetch_assoc($recordset);
          if($totalrows>0)
          {
           do
          {
         $r=$row_recordset['Sl_no'];
         $d=$row_recordset['branch'];
         $d1=$row_recordset['description'];

         ?>
        </thead>
        <tbody id='tb-content'>
         <tr>
         <td><?php echo $r ;?></td>
         <td><?php echo $d ;?></td>
         <td><?php echo $d1 ;?></td>
         <td> <?php echo  '
         <a  href="edit_branch.php?no='.$row_recordset['Sl_no'].'">'?>
        <img src="pencil.png" />
        <?php   '</a>';? >&nbsp;|&nbsp;<?php echo' 
        <a href="branch_delete.php?no1='.$row_recordset['branch'].'">'?>
        <img src="cross.png" /> <?php '</a>';?>  
         </td>
          </tr>
          <?php    
         }while($row_recordset=mysql_fetch_assoc($recordset));
         }
         mysql_free_result($recordset);
          mysql_close($a1);
          ?>

          </tbody>
           </table>
           </div>
           </div>
           </div>
           </section>
            </body>
              <footer>
           <p>&nbsp;</p><hr>
           <center>&copy;Copyright 2015 RepoterManagnmentSystem
           </center> 
           </footer>
            </div>
            </body>
            </html>

这是branch_delete.php

  <?php
  $cn=mysql_connect('localhost','root1',"oec@123");
  $db_selected=mysql_select_db('rms',$cn);
  $bn=$_REQUEST['no1'];
  $sql='DELETE     au.Sl_no,au.name,au.company_name,au.email_id,au.password,au.confirm_password,au.group,au.branch,au.phone,au.wordcount,au.rating,cb.Sl_no,cb.branch,cb.description from admin_user  au,create_branch  cb where cb.branch="'.$bn.'" AND au.branch=cb.branch ;';
    $s1=mysql_query($sql,$cn);
    mysql_close($cn);
    header('Location: branch.php');
     ?> 

这是update_branch.php

   <?php
   $a1=mysql_connect('localhost','root1','oec@123');
   $b1=mysql_select_db('rms',$a1);
   $bn=$_REQUEST['bname'];
   $sn=$_REQUEST['Sl_no'];
   $d=$_REQUEST['descp'];
   $w= 'SELECT branch,description FROM create_branch;';
   $recordset=mysql_query($w,$a1) or die(mysql_error());
   $totalrows=mysql_num_rows($recordset);
   $row_recordset=mysql_fetch_assoc($recordset);
   if($totalrows>0)
   {
   do
     {
   $r=$row_recordset['branch'];
    $s=$row_recordset['description'];   
   if($bn==$r and $d==$s)
   {
    ?>

    <!doctype html>
    <html>
     <head>
      <meta charset="utf-8">
      <title>Untitled Document</title>
     <link href="styles/createbranch.css" rel="stylesheet" type="text/css">         
     </head>

       <body>

      <div id="wrapper">
       <header id="top">
       <h1>Reporter Management System</h1>
        <nav id="mainnav">
        <ul>
        <li><a href="index.html">Home</a></li>
        <li><a href="">Branch</a></li>
        <li><a href="">Branch Admin</a></li>
        <li><a href="">Reporters</a></li>
        <li><a href="">Accountant</a></li>
        <li><a href="">Settings</a></li>
        <li><a href="">Log out</a></li>
        </ul>
        </nav>
        </header>
        <div id="hero">
         <img src="News2.jpg" width="1000" height="320"  alt=""/>
          </div>

            <form  method="post" action="edit_branch.php">
          <article id="main">

        <?php echo '<h2>branch already exists</h2>' ; ?>

         <h2>Create Branch</h2><hr>
         <p><label for="branch name">Branch Name:</label><br><br>
         <input class="textbox" type="text" placeholder=" <?php echo $bn ;?>" name="bname"></p><br>
         <p><label for="Description">Description:</label><br><br>
         <input class="textbox" type="text" placeholder="<?php echo $d ;?>" name="descp"></p><br>
        <input type="submit"  class="classname" value="Update Branch" ></p>           <br>
      </article>
      </form>
      <footer>
      <p>&nbsp;</p><hr>
      <center> &copy; Copyright 2015 ReporterManagementSystem</center>           </footer>
      </div>
      </body>
      </html>
      <?php 

     exit(0);
         }
          }
     while($row_recordset=mysql_fetch_assoc($recordset));

     $w1='UPDATE create_branch set branch="'.$bn.'" and description="'.$d.'" where Sl_no="'.$sn.'";';
     $x=mysql_query($w1,$a1) or die(mysql_error());

     ?>
        <!doctype html>
        <html>
        <head>
        <meta charset="utf-8">
        <title>Untitled Document</title>

        </head>

        <body>
        <div id="wrapper">
        <header id="top">
        <h1>Reporter Management System</h1>
        <nav id="mainnav">
        <ul>
        <li><a href="branchadmin.php">Home</a></li>
        <li><a href="reporters.php">Reporters</a></li>
        <li><a href="content1.php">News Content</a></li>
        <li><a href="CreateUser.php">Add user</a></li>
        <li><a href="">Log Out</a></li>

        </ul>
        </nav>
        </header>
        <img src="News2.jpg" width="1000" height="320"  alt=""/>




        <?php
         echo "<br>";

       if($x=="true")
        {

        header('Location: branch.php');


        }
        else
        {
        echo "fail to update branch";
        }

        echo"<br>";

        }
        mysql_free_result($recordset);
        mysql_close($a1);
        ?>

1 个答案:

答案 0 :(得分:0)

sql查询错误构建。 更新SQL查询应该如下所示

UPDATE table_name SET
       column1=value1,column2=value2,...
     WHERE some_column=some_value;

您的更新:

UPDATE create_branch SET 
       branch="'.$bn.'" ,
       description="'.$d.'"
    WHERE Sl_no="'.$sn.'"

SQL查询 - 删除:

DELETE FROM table_name
WHERE some_column=some_value;

您的删除:

DELETE FROM create_branch
        WHERE cb.branch="'.$bn.'"

阅读有关FOREIGN KEY约束的MySQL documentation

在两个表加入外键的情况下,删除或修改第一个结果中的记录将是第二个表中记录的适当操作。

实施例: MySQL Foreign Key On Delete