如何使用php将值插入数据库

时间:2014-01-17 07:42:39

标签: php sql sql-update

我试图将行插入数据库但是没有成功。首先检查是否存在行,如果我想要更新其他我想要插入。表中watch_type列的值可以是'price_gain'或'price_loss'。任何人都可以发现问题。我是php的新手。

<?php
    $maximumprice=100;
    $minimumprice=85;
    $maximumaction='action sell';
    $minimumaction='action max';
    $maximumshares=0;
    $minimumshares=123;
    $user_id=1;
    $symbol='ARM';
    $price=95;

    //connect to database
    $user="***";
    $pswd = "*****";
    $db = "******";
    $host = "****";

    $conn=mysql_connect($host,$user,$pswd)or die("unable to open database");
    mysql_select_db($db);

    if($maximumprice!=0.0){
            $amount=$maximumprice-$price;
            $maximum_update_query="Update watchlist SET price_at_creation='$price',amount='$amount',actions='$maximumaction',number_of_shares='$maximumshares' WHERE user_id='$user_id' AND watch_type='price_gain' AND stockN$
            $maximum_query="Insert INTO watchlist(user_id,stockName,price_at_creation,watch_type,amount,status,actions,number_of_shares) Values($user_id,'$symbol',$price,'price_gain',$amount,'pending','$maximumaction',$max$
            $maximum_select_query="Select count(1)  from watchlist where user_id='$user_id' AND stockName='$symbol' AND watch_type='price_gain'";

            if(mysql_fetch_array(mysql_query($maximum_select_query))>0){
                    mysql_query($maximum_update_query) or die(mysql_error());
            }
            else{
            mysql_query($maximum_query)or die(mysql_error());
            }
    }

    if($minimumprice!=0.0){
            $minimum_amount=$minimumprice-$price;
            $minimum_query="Insert INTO watchlist(user_id,stockName,price_at_creation,watch_type,amount,status,actions,number_of_shares) VALUES($user_id,$symbol,$price,'price_loss',$minimum_amount,'pending','$minimumaction$
            $minimum_update_query="Update watchlist SET price_at_creation='$price',amount='$minimum_amount',actions='$minimumaction',number_of_shares='$minimumshares' WHERE user_id='$user_id' AND watch_type='price_loss' AN$
            $minimum_select_query="Select count(1)  from watchlist where user_id='$user_id' AND stockName='$symbol' AND watch_type='price_loss'";

            if(mysql_fetch_array(mysql_query($minimum_select_query))>0){
                    mysql_query($minimum_update_query) or die(mysql_error());
            }
            else{
            mysql_query($minimum_query)or die(mysql_error());
            }       
    }
    mysql_close();

&GT;

0 个答案:

没有答案