Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

javascript - PHP函数未将Javascript变量传递到SQL数据库(PHP function isn't passing Javascript variable into SQL database)

I have a javascript function where I'm collecting the coordinates of markers from a google map, and attempting to pass them into an sql database using PHP.(我有一个javascript函数,我从Google地图收集标记的坐标,然后尝试使用PHP将其传递到sql数据库中。)

However, there is absolutely no data in my database.(但是,我的数据库中绝对没有数据。) This is my javascript:(这是我的javascript:) markers.forEach(function (entry) { console.log(entry.getPosition()); $.ajax({ url: "http://upload.php", type: "POST", data: "entry.getPosition()", }) }) And this is my php:(这是我的PHP:) mysql_select_db("coordinates", $con); mysql_query("insert into coordinates (coordinates) values ('".$_POST['data']."');"); mysql_close($con); where, for testing purposes, my database has a table named coordinates with a single row named coordinates.(在这里,出于测试目的,我的数据库中有一个名为坐标的表和一个名为坐标的行。)   ask by andromedutch translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This has syntax errors, firstly.(首先,这有语法错误。)

$.ajax({ url: "http://upload.php", type: "POST", data: "entry.getPosition()", }) You don't need the comma after data , and using "" around it is passing the string entry.getPosition() .(您不需要在data后加逗号,并在其周围使用""传递字符串 entry.getPosition() 。) Instead, you should be passing key-value pairs, and accessing them with their key from the $_POST array.(相反,您应该传递键值对,并使用$_POST数组中的键对对其进行访问。) And , since you're doing this for a lot of elements (in a forEach ), perhaps add them all to a single array first, so you only have one network request.(并且 ,由于您要对许多元素(在forEach )执行此操作,因此可能首先将它们全部添加到单个数组中,因此您只有一个网络请求。) Finally, I hope "http://upload.php" is just an example.(最后,我希望"http://upload.php"只是一个例子。) If this is supposed to be a link to a file called upload.php in the same directory on the server, then the http will mess it up.(如果这应该是服务器上同一目录中名为upload.php的文件的链接,则http会将其弄乱。) It will instead think it is a different website called upload.php .(相反,它将认为这是另一个名为upload.php网站。) Change it to a relative path.(将其更改为相对路径。) const dataList = markers.map(entry => entry.getPosition()); console.log(dataList); $.ajax({ url: "upload.php", type: "POST", data: { 'dataList': dataList } }); Then, since you have a list, you can loop through it in PHP:(然后,由于有了列表,因此可以在PHP中遍历它:) $arr = $_POST['dataList'] ?? NULL; if ($arr !== NULL) { mysql_select_db("coordinates", $con); $query = "INSERT INTO coordinates (coordinates) VALUES "; foreach($arr as $i => $v) { $query .= "(". $v .")"; if ($i < sizeof($arr) - 1) $query .= ","; } $query .= ";" mysql_query($query); mysql_close($con); } I didn't test this, since I don't have a PHP environment at work, but this should be a good starting place.(我没有进行测试,因为我没有PHP环境在工作,但这应该是一个很好的起点。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...