php - 没有选择正确的行进行编辑

标签 php jquery mysql

现在我正在尝试编辑我的数据库表的字段,但问题是当我单击它时选择第 1 行,当我第二次单击它时它选择第 2 行,依此类推,无论我选择的是哪一行。

为了便于理解;当我点击第 1 行时,它选择了第一行,但是当我再次点击第 1 行时,它选择了第 2 行并显示了要编辑的第 2 行数据,当我点击第 3 行时,它显示了要编辑的第 3 行数据和儿子。它处于一种循环中。

下面是我的代码

<!doctype HTML>
<head>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jq‌​uery-ui.css">
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <link rel="stylesheet" href="css/venview.css">
</head>
<body>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script>

    <input type="button" id="addbtn" value="Add">
    <?php




    include 'includes/head.php'; 
    include 'ven_connect.php';
    include "dashboard.php";


    if (isset($_GET["page"])) 
    { 
        $page  = (int) $_GET["page"]; 
    } 
    else 
    { 
        $page=1; 
    }; 
    $start_from = ($page-1) * 4; 

    $result = mysqli_query($conn , "SELECT * FROM vendor LIMIT $start_from, 4") or die (mysqli_error ($conn));

    echo "<table title='Vendors'>";
    echo '<tr>';
    echo    "<th>Sr</th>";
    echo    "<th>Edit</th>";
    echo    "<th>Delete</th>";
    echo    "<th>Name</th>";
    echo    "<th>PhoneNo</th>";
    echo    "<th>Email</th>";
    echo "</tr>";
    echo "<tr>";
    while($row = mysqli_fetch_array( $result )) {

        // display the contents of each row into a table
        echo "<tr>";
        echo '<td>' . $row['id'] . '</td>';
        echo '<td>' . '<img src="edit.png" style = "height:35px;margin-left :8px;" onclick = "edit()">' . '</td>';
        echo '<td>' . '<img src="delete.png" style = "height:35px;margin-left :8px;" onclick = "deleterecord('.$row['id'].')">' . '</td>';
        echo '<td>' . $row['Name'] . '</td>';
        echo '<td>' . $row['Number'] . '</td>';
        echo '<td>' . $row['email'] . '</td>';
        echo "</tr>"; 
    echo '<div id="dialog-confirm" style="display:none;">';
    echo'<form method="post"   action="edit_ven.php" class="ajax">';
    echo'<label for="id">ID</label>';
    echo'<input type="text" id="id" name="id" value= '.$row['id'].' readonly>  ';    
    echo'<label for="name">Name</label>';
    echo'<input type="name" id="name" name="name" value= '.$row['Name'].'>';
    echo'<label for="number">Number</label>';
    echo'<input type="number" id="number" name="number" value= '.$row['Number'].'>';
    echo'<label for="email">Email</label>';
    echo'<input type="email" id="email" name = "email" value= '.$row['email'].'>';

    echo'</form>';
    echo'</div>';
    } 
    echo "</tr>";

    echo "</table>";





    //Pagination!!  
    if($page > 1)
    {

        $prev= $page - 1;

        echo " <a href='{$_SERVER['PHP_SELF']}?page=$prev'>Prev</a> ";
    }
    $result = mysqli_query($conn , "SELECT * FROM vendor") or die (mysqli_error ($conn));
    $total_records = mysqli_num_rows($result); 
    $total_pages = ceil($total_records / 4); 

    $range = 3;


    for ($x = ($page - $range); $x <($page + $range); $x++) {

        if (($x > 0) && ($x <= $total_pages)) {

            if ($x == $page) {

                echo " [<b>$x</b>] ";

            } 

            else {

                echo " <a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a> ";
            } 
        } 
    } 
    if($page != $total_pages)
    {
        $nextpage=$page+1;
        echo " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>Next</a> ";
    }






    ?>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
    <script src="smoke.js"></script>
    <link rel="stylesheet" href="css/venview.css">
    <script type="text/javascript">
        function deleterecord ( id ) {
            smoke.confirm("Do you want to delete?", function(result){
                if(result)
                {
                    window.location.href = 'delete_ven.php?id=' + id;
                }
                else{
                    header("Location: ven_view.php");
                }});

        }

        function edit() {
            $('#dialog-confirm').dialog({
                modal: true,
                width: 400,
                height: 400,
                buttons: {
        update: function() {
          window.location.href = 'edit_ven.php?id=' + id;
        },
        Cancel: function() {
          $( this ).dialog( "close" );
        }
      }
            });
        }

    </script>


</body>

</html>

最佳答案

您应该发送一个 id 到编辑功能,然后发送 html 代码中的设置。

function edit(id)

关于php - 没有选择正确的行进行编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31229367/

相关文章:

javascript - 如何使用 Javascript 更改下拉组合框值

javascript - 最好看的 Web 标准 TreeView 是什么?

javascript - 如何确保页面的某些部分不受 CSS 更改的影响?

php - 如何在按钮单击时添加带有 mysql 表数据的下拉列表?

mysql - sql select with count 在里面

php - 结构\H、\V 和\N 是什么意思?

php - 在 iOS 上通过 ASIFormDataRequest 发送视频时出现部分上传错误

php - PHP OOP 中的未定义属性错误消息

php - 绝对路径中的mkdir()

mysql - 在 my.conf 中有自己的 [sections] 好吗?