简体   繁体   中英

Using Select and Update in PHP query

I am new to php .. anyway I have an application that require to provide the vacation information from the database to the supervisor and then allow the supervisor to enter his approval of this vacation ,

To do this, I use 2 php pages:

  • one to retrieve the employee vacation request information
  • and the other to insert the supervisor approval into the database

I want the application to insert this approval value in the record of this employeeID that I retrieve his information in this application.

The retrieval of employee information is done correctly, but the insert doesn't work !

I try this query but it didn't work (it didn't return any results when I exceute the query):

<?php
if(! $conn)
{
    die('Could not connect: '.mysql_error());
}

mysql_select_db("a2202757_OURDATA",$conn);

$flag['code']=0;

if($r = mysql_query ("UPDATE request_vacation SET VacationApprove='".$_POST['Approval']."' WHERE IDEmployee=$id",$conn));

{
$flag['code']=1;

}
print(json_encode($flag));

mysql_close($conn);

?> 

What is the problem ?

I think your code formatting is somehow broken on stackexchange. Below is an example of how to use queries, because you code seems to be fairly broken and nonsense. Attention: Don't use that in a production environment because it includes some SQLInjection vulnerabilities.

$id = $_POST["IDEmployee"];
// this sets the VacationApprove clumn of request_vacation 
// to the $_POST['Approval'] variable where IDEmployee matches
// the previously selected $id
$insRes = mysql_query("UPDATE request_vacation SET VacationApprove = '".$_POST['Approval']."' WHERE IDEmployee = '".$id."'")

I don't know what you are trying to achieve. Please be more specific about your problem.

What happens is that you assign a variable ( = is an assignment for comparison you use == or ===) in an if statement.

When you want to check and assign at the same time use this:

if(false !== $r = $mysql_query("UPDATE .......") {
    echo 'Update successfull';
}
else {
     echo 'Update failed with error ' . mysql_error();
}

It is difficult to solve the problem if you do not provide any error message about the database connection. Please use the following code and using your host and username password to let php to print out the error message. It would be useful if you can provide further error information.

    $conn = mysql_connect('localhost','username','password');
    if(! $conn)
    {
        die('Could not connect: '.mysql_error());
    }

    $db_selected = mysql_select_db("a2202757_OURDATA",$conn);
    if (!$db_selected) {
        die ('Can\'t use foo : ' . mysql_error());
    }
    $flag['code']=0;
    $r = mysql_query ("UPDATE request_vacation SET VacationApprove='".$_POST['Approval']."' WHERE IDEmployee=$id",$conn);
    if (!$r) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }
    if($r != null);

    {
        $flag['code']=1;

    }
    print(json_encode($flag));

    mysql_close($conn);

Try this

<?php
if (isset ( $_POST ['Approval'] )/* && isset ( $_POST ['IDEmployee'] )*/) {
    mysql_connect ( 'server', 'username', 'password' ) or die ( mysql_error () );
    mysql_select_db ( 'a2202757_OURDATA' );

    // mysql_query ( "UPDATE request_vacation SET VacationApprove='" . $_POST ['Approval'] . "' WHERE IDEmployee= '" . $_POST ['IDEmployee'] . "'" ) or die ( mysql_error () );
    // Hey, What is $id, I leave it up to you.
    mysql_query ( "UPDATE request_vacation SET VacationApprove='" . $_POST ['Approval'] . "' WHERE IDEmployee= '" . $id . "'" ) or die ( mysql_error () );
    if (mysql_affected_rows () != - 1) {
        // some rows has affected
        echo mysql_affected_rows () . 'rows has affected';
    } else {
        // No row has affected
    }
} else {
    die ( 'Approval '/* . 'and/or IDEmployee'*/ . ' are not properly set' );
}
?>

and, please give some more detail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM