【发布时间】:2016-10-23 18:08:27
【问题描述】:
我有一个名为 positions 的表格。此表列出了管理员在 listOfPositions.php 中添加的不同职位的列表,如总裁、副总裁等。添加不同的职位后,他现在可以在该职位下添加不同的人.这就是我的问题所在。我将如何根据表格 positions 中的位置数为人员姓名自动增加输入名称?
我尝试使用 javascript,但它仅在 html 中增加,并且在我尝试保存时不会反映到 php。我当前的代码根据位置添加不同人的名字是ff:
<script type="text/javascript" src="http://code.jquery.com/jquery-git.js"></script>
<form action="post_officers.php" method="post"><br>
<center><select name="year">
<?php
for($i=date('Y'); $i>1999; $i=$i-2) {
$selected = '';
$year2 = $i-2;
if ($year == $i) $selected = ' selected="selected"';
echo ('<option value="'.$year2. "-" . $i .'" '.$selected.'> '.$year2.'-'.$i.'</option>'."\n");
}
?>
</select></center>
<?php
include_once('dbcontroller.php');
$sql = "SELECT * FROM positions ORDER BY pos_id ASC";
$result = mysqli_query($conn, $sql);
/* assign an onchange event handler */
while ($row = mysqli_fetch_array($result)) {
$position = $row['position'];
?>
<br><br>
<table id="options-table">
<tr>
<td><input type="file" name="file" /></td>
<td><input type="hidden" name="position" /><?php echo $position; ?></td>
<td><input type="text" name="name" /></td>
</tr>
</table>
<?php
}
?>
<input type="submit" name="submit" value="SAVE"/>
</form>
<script>
$("input[name='file']").each(function(ind) {
$(this).attr(ind + 1);
});
$("input[name='position']").each(function(ind) {
$(this).attr(ind + 1);
});
$("input[name='name']").each(function(ind) {
$(this).attr(ind + 1);
});
</script>
这是我的 php 代码:
<?php
include ('dbcontroller.php');
date_default_timezone_set('Asia/Manila');
$year = mysqli_real_escape_string($conn,$_POST['year']);
if(isset($_POST['submit'])) {
$result = mysqli_query($conn,"SELECT * FROM officers WHERE year = '$year'");
$num_rows = mysqli_num_rows($result);
if($num_rows>0){
echo "<script type='text/javascript'>alert('Year already exists.'); window.location.href='create_alumni_officers.php';</script>";
}
else {
for ($i = 1; $i <= 8; $i++) {
$name = mysqli_real_escape_string($conn,$_POST['name'.$i]);
$position = mysqli_real_escape_string($conn,$_POST['position'.$i]);
$file=(rand(1000,100000)."-".$_FILES['file'.$i]['name']);
$type=($_FILES['file'.$i]['type']);
$size=$_FILES['file'.$i]['size'];
$loc=($_FILES['file'.$i]['tmp_name']);
$new_size=$size/1024; // file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($loc, '../officers-avatars/'.$final_file)) {
echo "Page is loading, please wait...";
$result = mysqli_query($conn,"INSERT INTO officers VALUES (id, '$year', '$position', '$name', '$final_file', '$new_size', '$type')")
or die(mysqli_error($conn));
echo ("<script type='text/javascript'>window.location.href='alumni_officers.php';</script>");
}
}
}
}
?>
这根本行不通。有什么帮助吗?我希望你们能理解我想问的问题。
【问题讨论】:
-
我不明白你在这里真正想要什么,但可能会有所帮助。
-
是的,我知道。 @Jeff 我如何让这个 没有 1 在名称旁边,因为我希望它是自动递增的?
-
在 php、html 和 javascript 之间切换上下文变得相当混乱。就像一个建议玩具可能要考虑为 html 输出创建 php 对象。不过,我感觉您想要的是包含 name 属性的输入标记,该属性与从位置表中检索到的自动递增数字相对应。请确认我有准确的理解。
-
你*不是玩具该死的滑动类型
-
是的,你是对的,这就是我想要做的,但我不知道该怎么做? @杰夫
标签: php mysql mysqli auto-increment