php-允许用户在mysql中进行编辑

我的代码有问题.该代码应该显示一个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],依此类推.

另外,如果您希望用户编辑字段名称(而不是打印出值,然后使用回显符“< td>”进行空白输入.$row [‘Firstname’].“<输入类型='文本字段'名称) =''first'>“.”< / td>)通过使用echo“< td>< input type ='textfield'name ='first'value ='”将该值直接放入文本字​​段. ['Firstname'].“'>”.“< / td&gt ;,它将更加友好.并且由于将使用数据库中的值填充值,因此您无需检查是否为空,因此如果提交了UPDATE,则始终可以运行该UPDATE,如果没有任何更改,则将使用现有数据进行更新.没变. 而且我不确定为什么要运行$query =“ Employ中的SELECT employee_id”;第二次. 现在,您似乎正在硬编码更新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注入

您可以尝试以下代码:

<!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>
上一篇:Struts 2 配置Action详解_java - JAVA


下一篇:php-jqgrid编辑操作如何将变量发送到服务器?