質問

I have never asked anything on one of these before, I could usually think of or find a way that was posted for ideas. I tried ways I thought of, tried using a CASE example that looked like it should work and no go. It won't update in any case.

OK here is what I am trying to do:

$Mysqlinfo="INSERT INTO `$PNum` (P_IDNum, P_Name, Raw_Time, Total_Time, T_Mode)
         VALUES ('$PersId', '$_POST[PersonaName]', '$Stats_Vals[1]',  '$Stats_Vals[2]', '$Stats_Vals[5]')
         ON DUPLICATE KEY UPDATE Car_Model='$Stats_Vals[1]', Total_Time='$Stats_Vals[5]', Raw_Time='$Stats_Vals[7]', T_Mode='$Stats_Vals[2]' (there was originally the "; at end here)
         ***WHERE Raw_Time > '$Stats_Vals[7]'";***

(There were more names there but I removed some so it was not sooo loong, so don't mind so much the $Stats_Vals numbers as the structure).

The thing works without the WHERE at the end except it always will INSERT or UPDATE, I know Where does not work with ON DUPLICATE KEY unfortunately so what is an easy equivalent? It has to chk for the Val and do NOTHING if the condition is NOT True.

Oh yeah it is formatted for use in a PHP script. :0)

Thanks much for any help!

Edit - Here is most of the PHP/sql code without the condition I am trying to achieve, it is called by an application:

<?php
hostname and
database info here
Variables, $_POST... etc.

 $link = mysql_connect($hostname, $username, $password);
        if (!$link) {
                die('Connection failed: ' . mysql_error());
        }
        else{
             echo "Connection to Server successful!" . PHP_EOL;  <for testing from web
}

        $db_selected = mysql_select_db($database, $link);
        if (!$db_selected) {
            die ('Can\'t select database: ' . mysql_error());
        }
        else {
            echo "Database successfully selected!". PHP_EOL;

$Mysqlinfo="INSERT INTO `$PNum` (P_IDNum, P_Name, Raw_Time, Total_Time, T_Mode)
         VALUES ('$PersId', '$_POST[PersonaName]', '$Stats_Vals[1]',  '$Stats_Vals[2]', '$Stats_Vals[5]')
         ON DUPLICATE KEY UPDATE Car_Model='$Stats_Vals[1]', Total_Time='$Stats_Vals[5]', Raw_Time='$Stats_Vals[7]', T_Mode='$Stats_Vals[2]'";

    if (!mysql_query($Mysqlinfo,$link))
              {
               mysql_close($link);
              die('Error: ' . mysql_error());
              }
}             
mysql_close($link);
?>

it works except for not following the condition of only updating if Raw_Time is less. Thanks again!

役に立ちましたか?

解決

If you want to make the update conditional, then I'm afraid you can't do it with INSERT...ON DUPLICATE KEY UPDATE.... You'll have to do it in two or more queries. And in order to make it atomic, you'll have to use LOCK TABLES:

$primarykey = 'whatever';

query("LOCK TABLES mytable WRITE;");

$count = query("SELECT COUNT(*) FROM mytable WHERE id=?;", $primarykey);

if($count>0) // the ID already exists
{
    $time = query("SELECT Raw_time FROM mytable WHERE id=?;", $primarykey);
    if($time>$Stats_Vals[7])
        query("UPDATE mytable SET ... WHERE id=?;", $primarykey);
}
else
    query("INSERT INTO mytable ...");

query("UNLOCK TABLES;");

A couple of notes:

  1. I'm calling some made up function query here because I don't know what method you're using to execute queries. I've also abbreviated some of the queries because I'm too lazy to copy all your code. You'll need to adjust the code according to your needs.

  2. I've also used ? for parameters in the queries - this is good practice to prevent SQL injection.

  3. The LOCK TABLES statement is there to ensure that no other process can delete the record you're working with between the time you check for the record's existence (SELECT COUNT(*)...) and the time you update. If that were to happen, your code will cause an error.

他のヒント

There is no single-query solution, you either have to retrieve the value of the Raw_Time column for the appropriate record (if exists) and evaluate that in your PHP script; or create a stored procedure for the task.

By the way, look out for security issues, like SQL Injection, when inserting values in your query given by the users.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top