繁体   English   中英

用PHP插入MySQL选择查询,空变量?

[英]insert mysql select query with php, empty variable?

这是我要插入到MYSQL表中的查询

$query1 = "SELECT close FROM stocks WHERE the_date = '$the_date' AND ticker = '$ticker' ";
$result1 = mysql_query($query1) or die("Query failed : " . mysql_error());

我设法这样打印出来:

while($price = mysql_fetch_assoc($result1)){
  echo "Price: ".$price['close']."<br /></h2>";
     }

我试着用这个插入:

$query3 = "INSERT INTO transactions (trans_ID,the_date,client_ID,ticker,shares,price,buy)
           VALUES (\"$trans_ID\",\"$the_date\",client_ID,\"$ticker\", 
            $shares,$price,$buy)";

但是页面上的错误消息是这样的:“查询失败:您的SQL语法有错误;请查看与MySQL服务器版本相对应的手册,以在第3行的'1)附近使用正确的语法”奇怪的原因就是我的html / head标签所在。

所以我尝试这样做:

var_dump($price);

并得到了这个:bool(false)

所以我该怎么做才能插入$ price? 我查找了一些有关插入数组,内爆,序列化的内容,但是我似乎无法使其正常工作?

谢谢!

您的查询应该像

$query3 = "INSERT INTO transactions (trans_ID,the_date,client_ID,ticker,shares,price,buy) VALUES ('$trans_ID','$the_date','$client_ID','$ticker', '$shares','$price','$buy')";

但是也不要忘记对查询中的所有这些变量使用mysql_real_escape_string

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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