我正在尝试创建一个简单的界面,以使用 js、php 和 html 将值写入 mySql 数据库。当我硬编码我想写的值时,一切都很好,但现在我想从我的输入 (.input) 中检索值并使用写入,写入我的数据库的值始终是 %%。谁能指出我做错了什么?谢谢
$('#write').click(function() {
writeTable();
})
// handles the click event, sends the query
function writeTable() {
$.ajax({
url: 'write.php',
type: 'get', //data type post/get
data: {
name: $('input#input').val()
},
complete: function(response) {
$('.output').html(response.responseText);
},
error: function() {
$('.output').html('Bummer: there was an error!');
}
});
return false;
}
.button {
border: 1px solid black;
width: 100px;
display: inline-block;
}
.output {
height: 400px;
width: 400px;
border: 1px solid black;
overflow: auto;
}
<!doctype HTML>
<head>
<meta charset="UTF-8">
<link type="text/css" rel="stylesheet" href="index.css">
</head>
<body>
<br>
<form>File Name:
<input type="text" name="input" placeholder="Type something">
</form>
<br>
<div class="button" id="write">Write</div>
<div class="button" id="search">Search</div>
<br>
<br>
<div class="output"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="index.js"></script>
</body>
PHP:
<?php
// Attempt MySQL server connection *
$link = mysqli_connect("localhost", "topdecka_admin", "kR4lJm1H4!", "topdecka_MTGO");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$data = "%".$_GET['name']."%"; //get data from javascript
// Attempt insert query execution
$sql = "INSERT INTO drafts (picks) VALUES ('$data')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
最佳答案
查看这些行:
$data = "%".$_GET['name']."%"; //get data from javascript
您确定要在 get 变量前后加上“%”符号吗?试试这个:
$data = $_GET['name']; //get data from javascript
关于javascript - 尝试将字符串从 js 写入 mySql 数据库时得到 %%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29236003/