你好,溢出者!
我是新来问这个问题的, 但我在这里读了很多书,这对我很有帮助。 这次虽然遇到了问题, 我似乎找不到线索或解决方案。
于是出现了以下情况:
我有一个前端,人们可以通过单击按钮来更改要使用的设计。
这确实运作得很好。但它应该将主题名保存到数据库中。
它说可以,但事实并非如此 - 如果使用 JS/JQuery 在网站上触发保存。
不过,如果我用 PHP 文件手动触发它,它就会发生:
./lib/savetheme.php?user=用户名&主题=主题名
如果我通过 JS 触发它,它会通过 php,这确实呼应了所需的 TRUE。 但它不保存到数据库。它不会抛出任何错误(它会死掉 - 但它回显 true..)。
所以代码来了...
common.php
<?php
function m($str) {
$str = mysql_escape_string($str);
return "'".$str."'";
}
?>
db-connect-data.php
<?php
$dbname = "dbname";
$dbuser = "dbuser";
$dbpw = "dbpw";
$dbhost = "localhost";
$dbport = "3306";
?>
db-connect.php
<?php
if (!mysql_connect($dbhost.":".$dbport, $dbuser, $dbpw)) {
die();
}
if (!mysql_select_db($dbname)) {
die();
}
?>
保存主题.php
require "./common.php";
require "./db-connect-data.php";
require "./db-connect.php";
if(isset($_GET)) {
$user = "";
$theme = "";
if($_GET['user'] != "") {
$user = $_GET['user'];
}
if($_GET['theme'] != "") {
$theme = $_GET['theme'];
}
$sql = "UPDATE users SET
theme = ".m($theme)."
WHERE username = ".m($user);
if(mysql_query($sql)) {
echo "TRUE";
} else {
echo mysql_error();
}
}
?>
profile.php
<?php
foreach (glob("./themes/*",GLOB_ONLYDIR) as $file) {
$output = str_replace("./themes/", "", $file);
echo '<button class="buttons themebuttons" id="'
.$output.'">'.$output.'</button>';
}
?>
<div class="ui-widget" id="savethemeok">
<br>
<div class="ui-state-green ui-corner-all">
<p>
Theme saved successfully!
</p>
</div>
</div>
<div class="ui-widget" id="savethemeerror">
<br>
<div class="ui-state-red ui-corner-all">
<p>
Error! Theme could not be saved!
</p>
</div>
</div>
main.js
$(document).ready(function() {
$(".buttons").button();
$(".themebuttons").click(function() {
var themename = $(this).attr("id");
var themepath = "./themes/" + themename + "/jquery-ui.css";
console.log("Saving new theme...");
$.get( "./lib/savetheme.php" , {
user: $("#loggeduser").attr("value"),
theme: "'" + themename + "'",
}, function(status) {
if(status == "TRUE") {
console.log("New theme: " + themename);
$("#theme").attr({href: themepath});
$("#savethemeok").show();
window.setTimeout(function() {
$("#savethemeok").fadeOut();
}, 2500);
} else {
console.log("Error: " + status);
$("#savethemeerror").show();
window.setTimeout(function() {
$("#savethemeerror").fadeOut();
}, 2500);
}
});
});
});
还有更多代码,我希望我能得到所有相关代码, 但我认为这不会再受到监督 如果我复制几乎 1000 行代码而不涉及这个特定问题。 如果需要的话我会做的。
我对任何建议感到高兴和感激,因为我已经在这个问题上投入了一些时间,而且似乎我不知道为什么当我通过 JS 触发它时它不保存到数据库但工作得很好使用 php 手动触发它...
如果我错过了某处关于此问题的帖子,我很抱歉。 如果有,请告诉我。但我没有找到任何东西。 话又说回来,也许我太笨了,没有去寻找它, 我确实很难用语言表达我的问题......
先谢谢大家了!
最佳答案
问题已解决。
我已将用户变量更改为另一个用户 - 所以它自然不会保存到我正在查看的用户。
很抱歉提醒您。
感谢您的观看 - 并向罗宾·威廉姆斯致敬:CARPE DIEM。
关于javascript - 无法使用 JQuery 触发的 PHP 写入 MySQL-DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25284887/