作者DeepBlueX (空白的)
看板PHP
標題[請益] 請問動態新增失敗的原因
時間Sat Jun 15 16:38:55 2013
第一支
<?php session_start(); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>upower</title>
請輸入要查詢的說明書
<br>
<br>
<form name="form" method="post" action="WorkContent_Insert.php">
<input type="text" name="work_num" /> <br>
<br>
<input type="submit" name="Submit" value="查詢">
</form>
第二支
<?php require_once('../Link/UPower.php'); ?>
<?php
include("../Link/UPower.php");
$work_num = $_POST['work_num'];
if($_POST['yes']=="確認")
{
if(isset($_POST['work_num']))
{
for ($i=1;$i<=$count;$i++)
{
mysql_select_db($database, $link);
$sqlIns = sprintf("INSERT INTO b_work_content (work_num, work_content, k1, k2, s1, s2, a1, a2) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
$_POST['work_num'],'"'.$_POST['work_content_'.$i.''].'"','"'.$_POST['k1_'.$i.''].'"','"'.$_POST['k2_'.$i.''].'"','"'.$_POST['s1'.$i.''].'"','"'.$_POST['s2'.$i.''].'"','"'.$_POST['a1'.$i.''].'"','"'.$_POST['a2'.$i.''].'"');
$sqlI = mysql_query($sqlIns, $link) or die(mysql_error());
}
}
if($sqlI)
{
echo '新增成功';
echo '<meta http-equiv=REFRESH CONTENT=0;url=WorkContent_search.php>';
}
else
{
echo '新增失敗!';
echo '<meta http-equiv=REFRESH CONTENT=0;url=WorkContent_search.php?department_name=$department_name&position_name=$position_name&work_num=$work_num>';
}
}
mysql_select_db($database, $link);
$sql_1 = "SELECT * FROM b_work_description where work_num = $work_num ";
$result_1 = mysql_query($sql_1);
$result = mysql_query($sql_1,$link);
$count = mysql_num_rows($result);
echo $count;
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>工作內容 - 新增</title>
</head>
<body>
<form method="post" action="WorkContent_Insert.php">
<input type="hidden" name="count" id="count" value="<?php echo $count; ?>"/>
<table border="1">
<tr>
<td> 工作任務(執掌)</td>
<td>知識(K)</td>
<td >技能(s)</td>
<td>工作動力/行為(A)</td></tr>
<?
for($i=1;$i<=$count;$i++){
while($row_1 = mysql_fetch_row($result_1))
{
?>
<tr>
<td rowspan=2>
<? echo $row_1[5];?>
<input type="hidden" name="work_content_[<?echo $i ;?>]" id="work_content" value="<?php echo $row_1[5]; ?>"/>
</td>
<td><select name="k1_[<?echo $i ;?>]" >
<?php
$sql_2 = "select * from b_function_definition";
$result_2 = mysql_query($sql_2,$link);
while ($row_2 = mysql_fetch_row($result_2)):
?>
<option value="<?php echo $row_2[2]?>"><?php echo $row_2[2]?></option>
<?php endwhile?>
<?
$sql_3 = "select * from b_work_content_input";
$result_3 = mysql_query($sql_3,$link);
While ($row_3 = mysql_fetch_row($result_3)):
?>
<option value="<?php echo $row_3[1]?>"><?php echo $row_3[1]?></option>
<?php endwhile?>
</select>
<input type="text" name="k1_input_[<?echo $i ;?>]" id="k1_input"/></td>
<td><select name="k2_[<?echo $i ;?>]" >
<?php
$sql_2 = "select * from b_function_definition";
$result_2 = mysql_query($sql_2,$link);
while ($row_2 = mysql_fetch_row($result_2)):
?>
<option value="<?php echo $row_2[2]?>"><?php echo $row_2[2]?></option>
<?php endwhile?>
<?
$sql_3 = "select * from b_work_content_input";
$result_3 = mysql_query($sql_3,$link);
While ($row_3 = mysql_fetch_row($result_3)):
?>
<option value="<?php echo $row_3[1]?>"><?php echo $row_3[1]?></option>
<?php endwhile?>
</select>
<input type="text" name="k2_input_[<?echo $i ;?>]" id="k2_input"/></td>
<td><select name="s1_[<?echo $i ;?>]" >
<?php
$sql_2 = "select * from b_function_definition";
$result_2 = mysql_query($sql_2,$link);
while ($row_2 = mysql_fetch_row($result_2)):
?>
<option value="<?php echo $row_2[2]?>"><?php echo $row_2[2]?></option>
<?php endwhile?>
<?
$sql_3 = "select * from b_work_content_input";
$result_3 = mysql_query($sql_3,$link);
While ($row_3 = mysql_fetch_row($result_3)):
?>
<option value="<?php echo $row_3[1]?>"><?php echo $row_3[1]?></option>
<?php endwhile?>
</select>
<input type="text" name="s1_input_[<?php echo $i; ?>]" id="s1_input"/></td></tr>
<tr>
<td><select name="s2_[<?echo $i ;?>]" >
<?php
$sql_2 = "select * from b_function_definition";
$result_2 = mysql_query($sql_2,$link);
while ($row_2 = mysql_fetch_row($result_2)):
?>
<option value="<?php echo $row_2[2]?>"><?php echo $row_2[2]?></option>
<?php endwhile?>
<?
$sql_3 = "select * from b_work_content_input";
$result_3 = mysql_query($sql_3,$link);
While ($row_3 = mysql_fetch_row($result_3)):
?>
<option value="<?php echo $row_3[1]?>"><?php echo $row_3[1]?></option>
<?php endwhile?>
</select>
<input type="text" name="s2_input_[<?php echo $i; ?>]" id="s2_input"/></td>
<td><select name="a1_[<?echo $i ;?>]" >
<?php
$sql_2 = "select * from b_function_definition";
$result_2 = mysql_query($sql_2,$link);
while ($row_2 = mysql_fetch_row($result_2)):
?>
<option value="<?php echo $row_2[2]?>"><?php echo $row_2[2]?></option>
<?php endwhile?>
<?
$sql_3 = "select * from b_work_content_input";
$result_3 = mysql_query($sql_3,$link);
While ($row_3 = mysql_fetch_row($result_3)):
?>
<option value="<?php echo $row_3[1]?>"><?php echo $row_3[1]?></option>
<?php endwhile?>
</select>
<input type="text" name="a1_input_[<?echo $i ;?>]" id="a1_input"/></td>
<td><select name="a2_[<?echo $i ;?>]" >
<?php
$sql_2 = "select * from b_function_definition";
$result_2 = mysql_query($sql_2,$link);
while ($row_2 = mysql_fetch_row($result_2)):
?>
<option value="<?php echo $row_2[2]?>"><?php echo $row_2[2]?></option>
<?php endwhile?>
<?
$sql_3 = "select * from b_work_content_input";
$result_3 = mysql_query($sql_3,$link);
While ($row_3 = mysql_fetch_row($result_3)):
?>
<option value="<?php echo $row_3[1]?>"><?php echo $row_3[1]?></option>
<?php endwhile?>
</select>
<input type="text" name="a2_input_[<?echo $i ;?>]" id="a2_input"/></td>
</tr>
<?
}
}
?>
</table>
<input type="submit" name="yes" id="yes" value="確認" />
<input type="reset" name="reset" id="reset" value="取消" /><br>
<a href="Work_Insert.php?department_name=<?php echo $department_name; ?>&position_name=<?php echo $position_name; ?>&work_num=<?php echo $work_num; ?>">回上一頁</a>
<a href="../Business.php">回首頁</a>
<br>
</form>
</body>
</html>
第一個問題:
請問為什麼新增一直失敗?
第二個問題:
我查看過我的for迴圈 他的i直都是在1
不管我資料庫有幾筆資料 是因我放錯地方嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.126.44.19
1F:推 bency:新增失敗是指怎樣的失敗?你預期出現什麼而網頁卻出現什麼? 06/15 17:16
2F:→ bency:2 你的$i一直是1那是因為你echo都是在while裡面跑 跟for一點 06/15 17:17
3F:→ bency:關係也沒有 再來題外化是建議不要直接拿$_POST去餵query 06/15 17:17
4F:→ DeepBlueX:新增失敗是指說 他部會跑我頁面的新增 他是會跑出我if e 06/15 18:54
5F:→ DeepBlueX:eles 的新增失敗 06/15 20:39
6F:推 PretenderY:我猜是因為按"確認"送出的時候,忘了送work_num的值, 06/16 10:27
7F:→ PretenderY:導致沒有$sqlI,所以怎麼跑都是進失敗的判斷,至於, 06/16 10:28
8F:→ PretenderY:第二個問題應該就跟1F說的一樣。 06/16 10:28
9F:推 bency:是說 $_POST['k1_'.$i.''] 這真的有東西嗎? 06/16 12:50
10F:→ bency:用var_dump($_POST) 看一下你收到了什麼東西吧 06/16 12:50
11F:→ bency:還有如果以後還會繼續碰php&mysql的話 建議開始改用mysqli吧 06/16 12:51
12F:→ bency:或是pdo, mysql_* 這種寫法官方已經不建議使用了 06/16 12:51