代码的这一特定部分无法正常工作

时间:2017-01-05 06:47:37

标签: php mysql database select sql-insert

我的代码:

$Query = mysql_query("INSERT INTO employee SET firstname='".$firstname."',lastname='".$lastname."',gender='".$gender."',
        email='".$email."',mobile='".$mobile."',empid='".$empid."',address='".$address."',dob='".$dob."',photo='".$photo."',
        password='".$password."',sales_manager='".$sales_manager."',designation='".$designation."',reg_date='".$reg_date."',
        res_manager='".$res_manager."',lead_limit='".$lead_limit."',lead_cycle='".$lead_cycle."',qualification='".$qualification."',
        experience='".$experience."',target='".$target."',c_delete='N',status='Active'");
    }
    else
    {
        $Query = mysql_query("INSERT INTO employee SET firstname='".$firstname."',lastname='".$lastname."',gender='".$gender."',
        email='".$email."',mobile='".$mobile."',empid='".$empid."',address='".$address."',dob='".$dob."',
        password='".$password."',sales_manager='".$sales_manager."',designation='".$designation."',reg_date='".$reg_date."',
        res_manager='".$res_manager."',lead_limit='".$lead_limit."',lead_cycle='".$lead_cycle."',qualification='".$qualification."',
        experience='".$experience."',target='".$target."',c_delete='N',status='Active'");
}
//this is for photo..

//this is used for chatting...usr_roll='".$usr_roll."'
$qch = mysql_query('SELECT * FROM stud_data order by desc');
$qch_rs=mysql_fetch_array($qch);
if(isset($qch_rs['id']))
{

    $usr_roll=$qch_rs['usr_roll']+1;
     echo $usr_roll;
    $Query = mysql_query("INSERT INTO stud_data SET usr_name='".$firstname."',
    usr_roll='".$usr_roll."',usr_unique_id='".$empid."'");
    echo "HELLO";
     echo $Query;
}
//this is used for chatting...

if($Query)
{

    $_SESSION[MESSAGE_TEXT]="Employee Added Successfully ...";
    $_SESSION[MESSAGE_TYPE] = "msgsuccess";
    header("location:index.php?p=".$_REQUEST['p']."");
    exit();
}
else
{

    $_SESSION[MESSAGE_TEXT]="Record did not Insert...";
    $_SESSION[MESSAGE_TYPE] = "msgerror";
    header("location:index.php?p=".$_REQUEST['p']."");
    exit();
}
}

2 个答案:

答案 0 :(得分:1)

您的查询缺少按ID

的顺序
$qch = mysql_query('SELECT * FROM stud_data order by desc');

使用

$qch = mysql_query('SELECT * FROM stud_data order by id desc');

答案 1 :(得分:0)

根据您给定的代码,您应该使用变量来获取值,然后创建一个insert语句,例如,您使用的是2 insert语句,而不是您可以获取变量中的值,然后插入如下:

$firstname = $_POST['first_name];

等。通过这种方式,您不需要创建2个插入声明。

除此之外

$qch = mysql_query('SELECT * FROM stud_data order by desc');

每当您使用order by子句时,总是会在其中使用列名,因此请使用id或name来命名。

希望这有帮助。

由于 Randheer