我的代碼有問題。這段代碼應該顯示一個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>
運行查詢時是否出現錯誤?嘗試通過mysqli_error()來捕獲它' – Hameed
當我點擊提交按鈕 – user3152011
時,它不會在html表中更新* sidenote:*您的代碼受到SQL注入攻擊,因爲您直接允許將POST值插入到您的查詢。 – Raptor