用json将MySQL转换为pdo语句

时间:2015-03-31 02:03:06

标签: mysql json pdo

我遇到了使用PDO无法确定如何操作的问题。我试过但不断收到错误。我会继续努力解决这个问题。如果有人能指出我的写作方向将是一个很大的帮助

/**  Function to Add Product  **/

function add_product() {
    $data = json_decode(file_get_contents("php://input")); 
    $prod_name      = $data->prod_name;    
    $prod_desc      = $data->prod_desc;
    $prod_price     = $data->prod_price;
    $prod_quantity  = $data->prod_quantity;

    print_r($data);


    $qry = 'INSERT INTO product (prod_name,prod_desc,prod_price,prod_quantity) values ("' . $prod_name . '","' . $prod_desc . '",' .$prod_price . ','.$prod_quantity.')';
   $qry_res = mysql_query($qry);
    if ($qry_res) {

     $arr = array('msg' => "Product Added Successfully!!!", 'error' => '');
        $jsn = json_encode($arr);
        // print_r($jsn);
    } 
    else {
        $arr = array('msg' => "", 'error' => 'Error In inserting record');
        $jsn = json_encode($arr);
        // print_r($jsn);
    }
}


/**  Function to Get Product  **/

function get_product() {    
    $qry = mysql_query('SELECT * from product');
    $data = array();
    while($rows = mysql_fetch_array($qry))
    {
        $data[] = array(
                    "id"            => $rows['id'],
                    "prod_name"     => $rows['prod_name'],
                    "prod_desc"     => $rows['prod_desc'],
                    "prod_price"    => $rows['prod_price'],
                    "prod_quantity" => $rows['prod_quantity']
                    );
    }
    print_r(json_encode($data));
    return json_encode($data);  
}

我尝试了什么,我没有插入数据

/**  Function to Add Product  **/

function add_product() {
    $data = json_decode(file_get_contents("php://input")); 
    $prod_name      = $data->prod_name;    
    $prod_desc      = $data->prod_desc;
    $prod_price     = $data->prod_price;
    $prod_quantity  = $data->prod_quantity;

    print_r($data);


$qry = "INSERT INTO product (prod_name,prod_desc,prod_price,prod_quantity) VALUES (:prod_name,:prod_desc,:prod_price,:prod_quantity)"; 

$q = $conn->prepare($qry); 

$q->execute(array(':prod_name'=>$prod_name, 
                  ':prod_desc'=>$prod_desc,
                  ':prod_price'=>$prod_price,
                  ':prod_quantity'=>$prod_quantity,
)); 

 $qry_res = mssql_query($qry);
    if ($qry_res) {

     $arr = array('msg' => "Product Added Successfully!!!", 'error' => '');
        $jsn = json_encode($arr);
        // print_r($jsn);
    } 
    else {
        $arr = array('msg' => "", 'error' => 'Error In inserting record');
        $jsn = json_encode($arr);
        // print_r($jsn);
    }
}

db setup

    <?php

      /****** Database Details *********/

        $host      = "localhost"; 
        $user      = "root"; 
        $pass      = ""; 
        $database  = "shopping";
        $con       = mysql_connect($host,$user,$pass);

        if (!$con) {
            die('Could not connect: ' . mysql_error());
        }

        //echo 'Connected successfully'; 

        mysql_select_db($database,$con);  

        /*******************************/

?>

0 个答案:

没有答案