首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >逐行更新

逐行更新
EN

Stack Overflow用户
提问于 2015-08-24 17:17:46
回答 1查看 33关注 0票数 0

单击编辑按钮后,我正在尝试更新/插入我的数据库(列:备注)。但是,如果我更改了一行中的值,所有行的值都会更新。我尝试为insert和update sql添加WHERE条件(基于afnumber<-- on id),但无法使其工作。有什么需要帮忙的吗?

代码语言:javascript
代码运行次数:0
运行
复制
$conn = new PDO('mysql:host=localhost;dbname=jr', 'root', 'Js');
    $conn->exec("set names utf8");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $num_rows = $conn->query('SELECT COUNT(*) FROM employees')->fetchColumn(); 
    $pages = new Paginator($num_rows,9,array(15,3,6,9,12,25,50,100,250,'All'));
    echo $pages->display_pages();
    echo "<span class=\"\">".$pages->display_jump_menu().$pages->display_items_per_page()."</span>";
    $stmt = $conn->prepare("SELECT employees.afnumber,employees.name,employees.dateofemployment,employees.actualpost,employees.department FROM employees WHERE employees.status='Employed' AND (employees.afnumber LIKE '%$search%' OR employees.name LIKE '%$search%') ORDER BY employees.afnumber DESC LIMIT :start,:end");
    $stmt->bindParam(':start', $pages->limit_start, PDO::PARAM_INT);
    $stmt->bindParam(':end', $pages->limit_end, PDO::PARAM_INT);
    $stmt->execute();
    $result = $stmt->fetchAll();

    $ewhtable = "<table class='sortable'><tr><th>AFNumber</th><th>Employee Name</th><th>Years of Service</th><th>Actual Post</th><th>Department</th><th>Note</th><th>Deducted Hours</th></tr>\n";
    foreach($result as $row) {
        $years=explode("/", $row[2]);
        $years[2]=intval(date ('Y')) - $years[2];

$sql="SELECT note,deductedwh FROM editedworkhours WHERE afnumber='$row[0]'";

$var = "";
$varr = "";  

$stmt = $conn->prepare($sql);
$stmt->execute();
$result2 = $stmt->fetchAll();

foreach ($result2 AS $row2) {
  $var .= $row2['deductedwh'] . "\n";
    $varr .= $row2['note'] . "\n";
}
        $Id = $row[0];
        $ewhtable .= "<tr><td>$row[0]</td><td>$row[1]</td><td>$years[2]</td><td>$row[3]</td><td>$row[4]</td><td><form method='post'><input type='text' name='Note' value='$varr' style=' padding: 10px;border: solid 2px #c9c9c9; width:200px; height:2px;'><input type='submit' id='search' name='edit' alt='search' value=''></form></td><td>$var</td></tr>\n";
    }



    $ewhtable .= "</table>\n";
    echo $ewhtable;

    exportTable(str_replace("&","",$ewhtable),"EmployeeDeductedWorkHoursTable");
    echo $pages->display_pages();
    echo "<p class=\"paginate\">Page: $pages->current_page of $pages->num_pages</p>\n";

          if(isset($_POST['edit']))
    {   
        $note = $_POST['Note'];  

     $sql1="SELECT Note FROM editedworkhours"; 

if ($result=mysqli_query($con,$sql1))
  {

  $rowcount=mysqli_num_rows($result);
  }
    if($rowcount==0)
     {
 $sql="INSERT INTO editedworkhours (Note) VALUES ('$note')";
 $result = mysqli_query($con,$sql);
     }
     else
     {
 $sql2 = "UPDATE editedworkhours SET Note= '$note'";
 $result2 = mysqli_query($con,$sql2);
     }

    }
    echo "</div>";
EN

回答 1

Stack Overflow用户

发布于 2015-08-24 17:26:59

这段代码中有太多的错误,从缺乏错误报告开始,SQL注入的直接危险,混合不同的DB API,缺乏适当的SQL等等-太多了,不能在一篇文章中回答。

这个问题必须结束,因为范围太广了,你必须和课本一起坐几个小时。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32178579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档