2014-01-07 106 views
3

我的代碼有問題。這段代碼應該顯示一個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> 
+1

運行查詢時是否出現錯誤?嘗試通過mysqli_error()來捕獲它' – Hameed

+0

當我點擊提交按鈕 – user3152011

+2

時,它不會在html表中更新* sidenote:*您的代碼受到SQL注入攻擊,因爲您直接允許將POST值插入到您的查詢。 – Raptor

回答

2

@ user3152011你有超過1名員工,如果讓你的輸入都回來了,因爲所有的空白,除非你想,因爲你定義具有相同名稱的多個輸入更新的最後一個員工的信息。試試var_dump($_POST)並參閱。

例如,如果您有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僱員= $我這是在你的情況1。你可能想通過使用一些你的其它領域,如echo "<input type="hidden" name="employee_id" value = '".$row['employee_id']."'>";當您提交表單這樣沿着EMPLOYEE_ID,你必須在$_POST['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> 
+0

非常感謝你 – user3152011

2

你必須更新語句是這樣的:

UPDATE Employ SET Firstname = $userfirst WHERE employee_id = $i 

大多數人的名稱是字母串。例如,考慮員工1的名稱是「Gordon」:

UPDATE Employ SET Firstname = Gordon WHERE employee_id = 1 

你能看到這個查詢出了什麼問題嗎?提示:SQL不會將「Gordon」識別爲任何內容。你需要使用單引號來分隔值:

UPDATE Employ SET Firstname = '$userfirst' WHERE employee_id = $i 

當你正在編寫代碼,不會變substituion和你有問題,第一步應該始終是後變量來打印出字符串代換。

+0

沒有修復它 – user3152011

0

嗨,你可以使用下面的代碼,而不是重複每個字段

$query=mysql_query("UPDATE Employ SET Firstname = '$userfirst',Lastname = '$userlast',Meetings = '$usermeetings',Sales = '$usersales',Comments = '$usercomments'WHERE employee_id ='$i'") or die(mysql_error) 

希望相同的查詢這會爲你

0123工作
0

如何幫助自己:

有太多的方式在你的代碼事情做一個簡單的測試,或者坦白地說有任何人在這裏花時間來幫你吧。

複製上面的代碼並將其設置爲現在的一側。然後拿出你的代碼並拿出絕對不需要的東西。把它減少到大約3行代碼。

1) Just a connect and display any error message. 
2) Create a simple query string with one field and one table. 
3) And then run that one line and get the error message back. 

順便說一句,你可以編輯你的MySQL使用MySQL查詢瀏覽器?如果不是先排序的話。

你必須學會​​將問題分解成一半,並簡化不工作時的事情。你不能指望其他人爲你做這件事。這是您作爲程序員的工作,通過一次測試一些小東西來學習細化細節。然後,當你獲得小工作的時候,你可以將它們加在一起構建大事。我已經採用了1000行代碼長的程序,並將它們縮短爲10行,以便有時發現問題(不經常,謝天謝地,但有時)。例如,多年前我有一個內存中隨機字節間歇性更改的錯誤。這是一個相當大的項目,我起初並不知道在哪裏尋找它。但通過去除事物以簡化系統的過程,我能夠一點一點地瞭解它來自哪裏。事實證明,這是一個以隨機方式一秒鐘被計時器踩踏的寄存器。

提示:我經常在記事本++中使用撤消/重做功能進行這種裁剪,因爲一旦我發現我的問題,很容易將文本恢復到原來的狀態。首先,我標記並複製修復程序。然後撤消。然後在最後粘貼修復程序。很容易將1000行剪切成10行,然後在完成時將行數恢復到1000行。有時我會前進或後退多達30或100次編輯。它比評論刪除它們更容易,然後取消刪除。