2014-04-08 1 views
0

내가 3 개 VARCHAR 필드 간단한 테이블을 읽고 지금까지이 코드를 가지고 업데이트하는 업데이트 SQL 문을 작성하는 :방법은 여러 레코드

<?php 
//db connection code... 

// select database 
mysql_select_db($db) or die ("Unable to select database!"); 

// create query 
$query = "SELECT * FROM Sheet1"; 

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

// see if any rows were returned 
if (mysql_num_rows($result) > 0) { 
// yes 

// see if any rows were returned 
if (mysql_num_rows($result) > 0) { 
// yes 
// print them one after another 
echo "<html><body><table cellpadding=10 border=1>"; 
while($row = mysql_fetch_assoc($result)) { 
    echo "<tr>"; 
    echo "<td>".$row['stickerID']."</td>"; 
    echo "<td>" .$row['stickerName']."</td>"; 
    echo "<td>".$row['stickerSection']."</td>"; 
    echo "<td>"?> 
      <form name="some form" action="editform.php" method="post"> 
      <input type="checkbox" name="<?php echo $row['stickerID'] ?>" value=" <?php echo $row['stickerStatus'] ?> "> 
      <?php "</td>"; 
    echo "</tr>"; 
} 
echo "</table></body></html>"; 
echo " " ?> 
      <input type="submit" name="editWish" value="Edit"> 
      </form> 
      <?php " "; 
} else { 
// no 
// print status message 
echo "No rows found!"; 
} 

// free result set memory 
mysql_free_result($result); 

// close connection 
mysql_close($connection); 
?> 

데이터베이스는 4 개 필드 3 VARCHAR 및 1 개 INT와있다 현재 값은 0입니다. 페이지 소스 코드를 확인하고 각 확인란 이름이 stickerID인지 확인했습니다. 이제 나는 이것을 만들어야하는 editform.php에 게시 할 것입니다. 임 궁금 무엇을 계정으로 양식에있는 사용자가 선택한 각 새 값을 계정으로 걸릴 수 있도록 SQL 업데이트를 작성해야합니까?

이것은 제 생각이지만 모든 확인란마다 어떻게해야합니까?

editform.php

<?php 

//update multiple records 

//UPDATE user_items SET stickerStatus = $_POST["stickerStatus"] WHERE stickerID = $_POST["stickerID"]; 

?> 
+0

과 부울 거짓 aswell을 의미 할 수있다, 테이블의 중간에하고있는

태그는 무엇인가? 나에게 유효한 html로 보이지 않는다. ;) – Erwin

답변

1

첫 번째 질문 : mysql_fetch_assoc() 대신 mysql_fetch_row()를 사용합니다. 그것은 열거 된 배열 대신 연결 배열을 반환합니다.

두 번째 질문 : HTML formsform handling에서 읽으십시오.

코멘트에서 질문에 대한 답변 :

// The <form> tag should only be echoed once. 
echo '<form name="some form" action="editform.php" method="post">'; 
while($row = mysql_fetch_assoc($result)) { 
    echo "<tr>"; 
    echo "<td>".$row['stickerID']."</td>"; 
    echo "<td>" .$row['stickerName']."</td>"; 
    echo "<td>".$row['stickerSection']."</td>"; 
    echo "<td>"?> 
      <input type="hidden" name="status_<?php echo $row['stickerID"; ?>" value="0"> 
      <input type="checkbox" name="status_<?php echo $row['stickerID'] ?>" value="<?php echo $row['stickerStatus'] ?> "> 
      <?php "</td>"; 
    echo "</tr>"; 
} 
// You need a submit button to send the form 
echo '<input type="submit">'; 
// Close the <form> tag 
echo '</form>'; 

확인란을 반드시 지정된 입력 이름에 대한 값을 서버로 전송하게 같은 이름을 가진 숨겨진 입력을 사용. 체크되지 않은 체크 박스의 값은 전송되지 않습니다. 이 경우 숨겨진 입력이 사용됩니다.

다음과 같이 editform.php에 제출 된 값을 얻을 수 있습니다 :

<?php 
foreach ($_POST as $field => $value) { 
    if (strpos($field, 'status_')) { 
     // Using (int) makes sure it's cast to an integer, preventing SQL injections 
     $stickerID = (int) str_replace('status_', '', $field); 
     // Again, preventing SQL injections. If the status could be a string, then use mysql_real_escape_string() 
     $stickerStatus = (int) $value; 

     // Do something with the results 
    } 
} 
+0

ok assoc 행을 바꿨다. 이제 빈 테이블이 생겼다. – marciokoko

+0

'$ row' 키가 숫자 필드에서 실제 필드 이름으로 변경되었으므로 나머지 스크립트를 적절히 업데이트해야한다. 그게 _ 결국 당신의 첫 번째 질문 이었어요. – Plenka

+0

Ok thx! 그래도 확인란을 삽입합니까? 내가 시도한 것으로 내 질문 코드를 업데이트하고 빈 페이지가 나타납니다. – marciokoko

0

print_r($row) 

이 행 배열이 거기에서 건설 작업 정확하게 방법을 찾아 마십시오. 당신의 비교 연산자에 대한

는,이 경우 값과 데이터 유형이 일치 모두가 아니라 단지 값보다 true를 돌려줍니다
$row[3] === 0 

대신

$row[3] == 0 

를 사용합니다.

0은 숫자 값이 관련이없는 참고 0