这是PHP中的一小段代码。
<?php include_once("includes/connection.php"); ?>
<?php include_once("includes/functions.php"); ?>
<?php
if(!isset($_POST['menu_name']) or !isset($_POST['position']) or !isset($_POST['visible'])){
echo "You are supposed to fill all entries<br/><br/><a href='new_subject.php'>go back</a>";
exit;
}
echo "<pre>";
var_dump($_POST);
echo "</pre>":
$menu_name = $_POST['menu_name'];
$position = (int)$_POST['position'];
$visible = $_POST['visible'];
$menu_name = mysql_real_escape_string($menu_name);
$subject_count = mysql_num_rows(get_all_subjects());
//$subject_set_desc = mysql_query("SELECT position FROM subjects ORDER by position DESC",$connection);
for($change = $position;$change<=$subject_count;$change++){
//get row with position $change
//increase it's position by 1
//continue till all rows end OR loop ends
/*UPDATE subjects SET position = $change+1 WHERE position = $change*/
$new_position = $change+1;
echo "{$change} ".gettype($change)." {$new_position} ".gettype($new_position)."<br/> ";
mysql_query("UPDATE subjects SET position = {$new_position} WHERE position = {$change}",$connection);
echo "UPDATE subjects SET position = {$new_position} WHERE position = {$change}"."<br/>";
//confirm_query($cheack);
}
$query = "INSERT INTO subjects (menu_name,position,visible) VALUES ('{$menu_name}',{$position},{$visible})";
if(mysql_query($query,$connection)){
//header("location:content.php");
exit;
} else{
echo "<b>failed to insert new subject</b> <br/>".mysql_error();
}
?>
<?php mysql_close($connection); ?>
我希望您只关注for
循环和该循环中的数据库查询。我通过post
此页面发送以下数据
$_POST['subject'] = 'new_subject'; //string type <br/>
$_POST['position'] = 1 //int type <br/>
$_POST['visible'] = 1 //tinyint type <br/>
$_POST['submit'] = 'Add Subject' // string type just submisssion <br/>
正如您在我的表单处理文件中看到的那样
在进行这些查询之前我的数据库
查询后我的数据库(在代码循环中)。
我认为表中所有行的位置都应该增加 1,但它们都改为 5 .. 在这里我想为那些位置大于或等于的人增加 1$_POST['position']..<br/>
我尝试回显我的查询并且所有这些都显示查询是正确的,但是在执行后对表的更改不像我那样(我想将每个位置增加 1)..