douhu2898 2015-03-24 15:02
浏览 51
已采纳

尝试将js中的字符串写入mySql数据库时获取%%

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);
?>
</div>
  • 写回答

2条回答 默认 最新

  • dongshan1396 2015-03-24 15:24
    关注

    It is a good practice to put a condition check before INSERT

    $data = "%".$_GET['input']."%"; //get data from javascript 
    if (empty($_GET['input'])) {
        echo 'name input cannot be blank';
    }else {
    $sql = "INSERT INTO drafts (picks) VALUES ('$data')"; }
    

    Before the insert check what you are getting in $_GET['input']. Try to print $_GET['input'] first, put a condition check, if its null (as it is in your case), do not execute the INSERT statement

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教