php - 允许用户在 mysql 中编辑

标签 php html mysql edit

我的代码有问题。这段代码应该显示一个 mysql 数据库并让用户编辑它,以便他们的编辑在 mysql 表中注册。但由于某种原因,查询不起作用,我无法获取它,以便用户可以编辑到 mysql 表中。

<!DOCTYPE HTML>
<html>
<head>
    <title><?php echo 'giggity'; ?></title>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', 'ankith12','Employees');
        if (mysqli_connect_errno())
    {
         echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

        $sql = "select * from Employ";
        $query = mysqli_query($con,$sql);
        echo "<table border ='1' style='height:90%;width:90%; position: absolute; top: 50; bottom:50; left: 0; right: 0;border:1px solid' align = 'center'>
            <tr>
            <th>Employee id</th>
            <th>Firstname</th>
            <th>Lastname</th>
            <th>Meetings Today</th>
            <th>Sales</th>
            <th>Comments</th> 
            </tr>";
            ?>
            <form method = 'Post'>
            <?php
$i = 1;
 while( $row = mysqli_fetch_array($query) )
{
    echo "<tr><td>". $row['employee_id'] . "<br><input type ='submit' name = 'Submit_$i' >". "</td>";
    echo "<td>". $row['Firstname']. "<input type = 'textfield' name = 'first' >"."</td>";
    echo "<td>". $row['Lastname']."<input type = 'textfield' name = 'second' >" . "</td>";
    echo "<td>". $row['Meetings']."<input type = 'textfield' name = 'third' >". "</td>";
    echo "<td>". $row['Sales']."<input type = 'textfield' name = 'fourth' >". "</td>";
    echo "<td>". $row['Comments']."<input type = 'textfield' name = 'fifth' >". "</td></tr>";
    $i++;
}
echo "</table>";
?>
<br>
<br> 
<!-- Submit<br><input type ='submit' name = 'Submit' > -->
</form>
<?php 

function alert($s){
    echo "<script type = 'text/javascript'>alert(\"$s\");</script>";
}

// $i = 1
$con = mysqli_connect('localhost', 'root', 'ankith12','Employees');
        if (mysqli_connect_errno())
    {
         echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
$query = "SELECT employee_id from Employ";
$qudey = mysqli_query($con,$query);
$rows= mysqli_fetch_assoc($qudey);
$dee = 1;
$easy = 0;
// $userfirst = $_POST['first'];
// $userlast =  $_POST['second'];
// $usermeetings =  $_POST['third'];
// $usersales =  $_POST['fourth'];
// $usercomments =  $_POST['fifth'];
foreach($rows as $i){
    //alert($_POST["Submit_$dee"]);
    if(isset($_POST["Submit_$dee"])) {
    //  alert("true");
        $i = 1;
        $userfirst = $_POST['first'];
        $userlast =  $_POST['second'];
        $usermeetings =  $_POST['third'];
        $usersales =  $_POST['fourth'];
        $usercomments =  $_POST['fifth'];
        alert($userfirst);
        if($userfirst !== ""){
            $QueryA = "UPDATE Employ SET Firstname = $userfirst WHERE employee_id = $i";
            mysqli_query($con,$QueryA);
            alert($QueryA);
        }
        if($userlast !== "")
        {
            $QueryB = "UPDATE Employ SET Lastname = $userlast WHERE employee_id = $i";
            mysqli_query($con,$QueryB);
        }
        if($usermeetings !== "")
        {
            $QueryC = "UPDATE Employ SET Meetings = $usermeetings WHERE employee_id = $i";
            mysqli_query($con,$QueryC);
        }
        if($usersales !== "")
        {
            $QueryD = "UPDATE Employ SET Sales = $usersales WHERE employee_id = $i";
            mysqli_query($con,$QueryD);
        }
        if($usersales !== "")
        {
            $QueryE = "UPDATE Employ SET Comments = $usercomments WHERE employee_id = $i";
            mysqli_query($con,$QueryE);
        }
        //echo 'done';
}
//  echo'done';
    $easy++;
    $dee = $dee + 1;
}
mysqli_close($con);
?>
</body>
</html>

最佳答案

@user3152011 您是否有超过 1 名员工,如果是这样,您的输入返回时全是空白,除非您尝试更新最后一名员工的信息,因为您正在定义多个具有相同名称的输入。尝试 var_dump($_POST)看看。

例如,现在如果您有 2 名员工,您将有 2 个同名输入,如 <input type = 'textfield' name = 'first' >所以当你提交时,如果你提交第一个员工你的 $_POST['first']将是空白的。

您可以输入 <form>在你的 while 循环中,这样每个人都是一个单独的形式,或者考虑使用像 <input type = 'textfield' name = 'first[]' > 这样的东西这样它们都以数组的形式返回,所以你有 $_POST['first'][0]$_POST['first'][1]等等。

此外,如果您希望用户编辑字段名称(而不是打印出值然后使用 echo "<td>". $row['Firstname']. "<input type = 'textfield' name = 'first' >"."</td> 进行空白输入),请使用 echo "<td><input type = 'textfield' name = 'first' value='". $row['Firstname']."'>"."</td> 将该值直接放入文本字​​段中,会友好很多。由于这些值将使用数据库中的值填充,因此您不必检查它是否为空,如果它已提交,您始终可以运行 UPDATE,如果没有任何变化,它只会使用现有数据更新它没有变化。

而且我不确定您为什么要运行 $query = "SELECT employee_id from Employ";第二次。

现在,看起来您正在硬编码更新 WHERE employee_id = $i,在您的情况下为 1。您可能希望使用类似 echo "<input type="hidden" name="employee_id" value = '".$row['employee_id']."'>"; 的方式将 employee_id 与所有其他字段一起传递这样,当您提交表单时,您将在 $_POST['employee_id'] 中获得一个可用的 employee_id。并更新该员工。

*** 并且不要忘记使用 http://ca1.php.net/mysqli_real_escape_string 保护自己免受 SQL 注入(inject)攻击

你可以试试下面的代码:

<!DOCTYPE HTML>
<html>
<head>
    <title><?php echo 'giggity'; ?></title>
</head>
<body>
<?php
function alert($s){
    echo "<script type = 'text/javascript'>alert(\"$s\");</script>";
}
    $con = mysqli_connect('localhost', 'root', 'ankith12','Employees');
        if (mysqli_connect_errno())
    {
         echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    //We'll try to update data first so that the query to display Employ is shown with fresh data
    if(isset($_POST["employee_id"])) {
        $useremployeeid = mysqli_real_escape_string($con,$_POST['employee_id']);
        $userfirst = mysqli_real_escape_string($con,$_POST['first']);
        $userlast =  mysqli_real_escape_string($con,$_POST['second']);
        $usermeetings =  mysqli_real_escape_string($con,$_POST['third']);
        $usersales =  mysqli_real_escape_string($con,$_POST['fourth']);
        $usercomments =  mysqli_real_escape_string($con,$_POST['fifth']);

        alert($userfirst);

        $QueryA = "UPDATE Employ SET Firstname = '$userfirst',
                                     Lastname = '$userlast',
                                     Meetings = '$usermeetings',
                                     Sales = '$usersales',
                                     Comments = '$usercomments'
                    WHERE employee_id = $useremployeeid";
        $query = mysqli_query($con,$QueryA);
        if (!$query){
             printf("Error: %s\n%s\n", mysqli_sqlstate($con),mysqli_error($con));
        }
    }

        $sql = "select * from Employ";
        $query = mysqli_query($con,$sql);
        if (!$query){
             printf("Error: %s\n%s\n", mysqli_sqlstate($con),mysqli_error($con));
        }
        echo "<table border ='1' style='height:90%;width:90%; position: absolute; top: 50; bottom:50; left: 0; right: 0;border:1px solid' align = 'center'>
            <tr>
            <th>Employee id</th>
            <th>Firstname</th>
            <th>Lastname</th>
            <th>Meetings Today</th>
            <th>Sales</th>
            <th>Comments</th> 
            </tr>";
$i = 1;
 while( $row = mysqli_fetch_array($query) )
{
    echo "<form method = 'Post'>";
    echo "<input type='hidden' name='employee_id' value='".$row['employee_id']."'>";
    echo "<tr><td>". $row['employee_id'] . "<br><input type ='submit' name = 'Submit_$i' >". "</td>";
    echo "<td><input type = 'textfield' name = 'first' value='". $row['Firstname']. "'>"."</td>";
    echo "<td><input type = 'textfield' name = 'second' value='". $row['Lastname']."'>" . "</td>";
    echo "<td><input type = 'textfield' name = 'third' value='". $row['Meetings']."'>". "</td>";
    echo "<td><input type = 'textfield' name = 'fourth' value='". $row['Sales']."'>". "</td>";
    echo "<td><input type = 'textfield' name = 'fifth' value='". $row['Comments']."'>". "</td></tr>";
    echo "</form>";
    $i++;
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

关于php - 允许用户在 mysql 中编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20963184/

相关文章:

mysql - Rake 0.9.2 与 rakedb 的问题 :migrate

c# - 如何在MySql查询中添加vb.net条件

php - 具有多个表的搜索结果,如查询 CodeIgniter

php - 在 echo 中使用 php include with

html - (CSS) 拉伸(stretch) div 但内容在中心

php - 如何通过流式传输文件使用 PHP、curl 和 HTTP POST 上传文件?

html - 我用纯 CSS 创建了一个下拉菜单,我希望它是下拉菜单而不是下拉菜单我不知道要添加或更改什么才能使它成为下拉菜单

html - 需要制作一个带有视频的整页 slider

php - Plupload 浏览按钮在 IE10 中不起作用

php - 使用 PHP 和 graph api 照片显示来自 facebook 的图像