I'm trying to create a simple interface to write values into a mySql database using js, php and html. When I hardcoded the value I wanted to write everything went fine, but now I want the retrieve the value from my input (.input) and use write that, and the value that gets written into my database is always %%. Can anyone please point out what I'm doing wrong? Thanks
$('#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);
?>