简体   繁体   English

我如何发布并获取一个PHP变量

[英]How can I post and get a php variable

I used the following php code to get the file extension of an uploaded file: 我使用以下php代码获取上传文件的文件扩展名:

$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

But now I want to post this variable to an other php file which saves all the data in a database. 但是现在我想将此变量发布到另一个php文件中,该文件将所有数据保存在数据库中。

if (isset($_POST["submit"])) {
  $insertSQL = sprintf("INSERT INTO sounds (id, title, artist, category, subcategory, keywords, upload, format, size, download, rating, ip, date) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['id'], "text"),
                       GetSQLValueString($_POST['title'], "text"),
                       GetSQLValueString($_POST['artist'], "text"),
                       GetSQLValueString($_POST['categoryID'], "text"),
                       GetSQLValueString($_POST['subcategoryID'], "text"),
                       GetSQLValueString($_POST['keywords'], "text"),
                       GetSQLValueString($_FILES['upload']['name'], "text"),
                       GetSQLValueString($_FILES['upload']['name'], "text"),
                       GetSQLValueString($_FILES['upload']['size'], "text"),
                       GetSQLValueString($_POST['download'], "text"),
                       GetSQLValueString($_POST['rating'], "text"),
                       GetSQLValueString($_POST['ip'], "text"),
                       GetSQLValueString($_POST['date'], "text"));

I know it is probably a stupid question. 我知道这可能是一个愚蠢的问题。 But how can I post and get this variable? 但是如何发布并获取此变量?

$filename = $_FILES['upload']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

//to insert value into DataBase //将值插入数据库

GetSQLValueString($ext, "text")

Why would you want to do that? 你为什么想这么做? You don't need to do a POST to save to a database. 您无需执行POST即可保存到数据库。 Just rewrite the other PHP code to be a function and call that function instead. 只需将其他PHP代码重写为一个函数,然后调用该函数即可。

You should only do a POST if you wish to communicate between two machines (such as between a visitor of your website to your server or your server to another server). 仅当您希望在两台计算机之间进行通信时(例如,网站的访问者与服务器之间或服务器与另一台服务器之间进行通信),才应该执行POST。

Have you considered using $_GET ? 您是否考虑过使用$ _GET? It's a lot easier. 这要容易得多。 Be careful of using $_REQUEST however (I would say it's best practice not to use it). 但是请小心使用$ _REQUEST(我想说最好不要使用它)。

If you want to POST you could consider using cURL. 如果要发布,可以考虑使用cURL。 The PHP Manual has plenty of examples so I won't put any extra ones here. PHP手册中有很多示例,因此在此不再赘述。

Alternatively it might even be easier to use jQuery to AJAX post. 另外,使用jQuery到AJAX发布甚至更容易。 With just three lines of code you can POST your variables and get a response. 仅需三行代码,您就可以发布变量并获得响应。 Check out the [http://api.jquery.com/jQuery.post/](jQuery manual) which also has examples. 查看[http://api.jquery.com/jQuery.post/](jQuery手册),其中也包含示例。 The syntax you would probably use a starting point is: 您可能会使用起点的语法是:

$.post("test.php", { name: "John", time: "2pm" },
   function(data) {
   alert("Data Loaded: " + data);
 });

Personally I would try the AJAX post first... 我个人将首先尝试AJAX发布...

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

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