PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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>

大佬总结

以上是大佬教程为你收集整理的php-允许用户在mysql中进行编辑全部内容,希望文章能够帮你解决php-允许用户在mysql中进行编辑所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: