传递变量和PHP

时间:2017-02-02 18:10:05

标签: php jquery mysql variables

我试图浏览网站,但无法找到适合我的问题的答案,或者我做错了方法,但我实际上是为我做了一个证券交易系统最后一年的项目,我遇到了这个问题。 首先,我通过这个传递变量:

<a href='#' id='<?php echo "". $row['id'] . ""?>' class="sell">

到jquery:

$(function(){
  $(".sell").click(function(){
    var element = $(this);
    var userid = element.attr("id");
    var info = 'id=' + userid;

    if (confirm("Do you really want to sell?")){
      $.ajax({
        url: 'sellstock.php',
        type: 'POST',
        data: info,
        success: function(){
          alert('Sold, Please refresh to see your balance');
        }
      });

      $(this).parent().parent().fadeOut(300, function({
        $(this).remove();
      });
    };

    return false;
  });
});

jquery调用sellstock.php

<?php
include('session_user.php');

$stock_id = $_POST['id'];

$sql_getinfo = "SELECT *
                FROM transaction_user
                WHERE id='$stock_id'";

$row_info = mysqli_fetch_array($sql_getinfo, MYSQLI_ASSOC);

$company_id = $row_info['company_id'];
$company_shares = $row_info['number_of_shares_bought'];
$company_price = $row_info['Price'];
$company_total = $row_info['Price_Value'];

$updatedbalance = $balance + $company_total;

$sql_stockinfo = "SELECT Shares_for_sale
                  FROM company_listed
                  WHERE id='$company_id'";

$row_stock = mysqli_fetch_array($sql_stockinfo, MYSQLI_ASSOC);
$shares = $row_stock['Shares_for_sale'];


$updatedshares = $company_shares + $shares;

$updatebalance = "UPDATE balance SET current_balance = '$updatedbalance' where username ='$login_session'";
$resultbalance = mysqli_query($db , $updatebalance);

$updateshare = "UPDATE company_listed SET Shares_for_sale = '$updatedshares' where id= '$stock_id'";
$resultshare = mysqli_query($db, $updateshare);


$sqldelete = "DELETE FROM transaction_user WHERE id='$stock_id'";
$result = mysqli_query($db, $sqldelete);

header("location: Dashboardd.php");
?>

我想要做的是删除股票(来自给定的ID),但在此之前,它应该更新用户的余额并更新公司的shares_for_sale

$sqldelete = "DELETE FROM transaction_user WHERE id='$stock_id'"; $result = mysqli_query($db, $sqldelete);

$sqldelete行实际上正在删除该交易,但我无法获得有关公司和用户余额的任何更新。更新查询正在运行,因为当我对它们进行硬编码时,它正在运行,但问题在于$stock_id,它不适用于$sql_getinfo,因为这不起作用$sql_stockinfo将不起作用,也$updateshare

$login_session调用$balancesession_user.php并且工作正常。

表:https://scontent-mrs1-1.xx.fbcdn.net/v/t35.0-12/16491661_10154975964354500_1753389524_o.jpg?oh=583059fbe0e2b8663acceedf0f1485c6&oe=58964E22

2 个答案:

答案 0 :(得分:0)

需要注意的要点:

在查询中使用变量之前检查变量的好习惯

TypeError: list indices must be integers or slices, not dict

无需添加额外报价

if(isset($_POST['id']))
{
  $stock_id = $_POST['id'];
}
else {
 //Show error message or redirect to the homepage. whatever you prefer
}

在查询中的变量周围仔细使用引号

<a href='#' id ='<?php echo $row['id']; ?>' class="sell">

答案 1 :(得分:0)

解决

const comp =  this.props.isLoggedIn ? 
    <Text style = { styles.welcome }>
        Welcome to Demo App!
    </Text>
    :
    <img style = { styles.logoImage } src = { images.logo } alt = "Demo App Logo" />


return (
  <View style = { styles.container }>
    {comp}
  </View>
);

$sql_getinfo = "SELECT * FROM transaction_user WHERE id='$stock_id'"; $result = mysqli_query($db, $sql_getinfo);