简体   繁体   English

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

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

Here's the query I'm trying to insert into a table in MYSQL 这是我要插入到MYSQL表中的查询

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

I managed to print it out like this: 我设法这样打印出来:

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

And I tried inserting with this: 我试着用这个插入:

$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)";

But the error message on the page said this: "Query failed : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1)' at line 3" which is weird cause that's just where my html/head tags are. 但是页面上的错误消息是这样的:“查询失败:您的SQL语法有错误;请查看与MySQL服务器版本相对应的手册,以在第3行的'1)附近使用正确的语法”奇怪的原因就是我的html / head标签所在。

So the I tried to do this: 所以我尝试这样做:

var_dump($price);

and got this: bool(false) 并得到了这个:bool(false)

so how do i make it so I can insert $price?? 所以我该怎么做才能插入$ price? i looked up some stuff about inserting arrays, implodes, serializing, but I can't seem to get it to work? 我查找了一些有关插入数组,内爆,序列化的内容,但是我似乎无法使其正常工作?

thanks! 谢谢!

Your query should be like 您的查询应该像

$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')";

But also do'nt forget to use mysql_real_escape_string on all those variables in query. 但是也不要忘记对查询中的所有这些变量使用mysql_real_escape_string

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

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