从html输入读取值并将它们插入到mysql数据库中

时间:2014-05-26 21:57:29

标签: php html mysql

我的HTML代码如下。我想从这些输入和一个选择表单中读取值,从中创建一个SQL语句字符串,然后将其插入表中。这是一个新的谷歌地图标记顺便说一句。我不知道怎么做,请帮帮我。我知道我不能使用PHP,因为它事先被执行了,但我不知道其他任何方式。提前谢谢。

<br> Name <input type="text" id="name"> <br> 
      Adress <br> <input type="text" id="adress"><br> 
      Lat <br><input type="text" id="lat"> <br> 
      Long <br><input type="text" id="long"><br> 
      Type <br> 

      <select style="width: 153px" id="type">
      <option value="vjerski">Vjerski objekti</option>
      <option value="obrazovna">Obrazovne ustanove</option>
      <option value="drzavna">Administracija</option>
      <option value="kulturna">Kulturne ustanove</option>
      </select>

1 个答案:

答案 0 :(得分:0)

insertMarker.php

<?php

$db_conn = new mysqli('host', 'user', 'password', 'database');

$stmt = $db_conn->prepare(
  'INSERT INTO Markers (name, address, lat, long, type) VALUES (?, ?, ?, ?, ?)'
);
$stmt->bind_param('sssss',
  $_POST['name'],
  $_POST['address'],
  $_POST['lat'],
  $_POST['long'],
  $_POST['type']
);
if ( $stmt->execute() ) { echo 'Insert successful.' }
$stmt->close();

?>

insertMarker.js

function insertMarker() {
  var xmlhttp;
  if ( window.XMLHttpRequest ) { xmlhttp = new XMLHttpRequest(); }
  else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  xmlhttp.onreadystatechange = function() {
    if ( xmlhttp.readyState == 4 && xmlhttp.status == 200 ) {
      // alert(xmlhttp.responseText);
    }
  }
  xmlhttp.open('POST', 'insertMarker.php', true);
  xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  xmlhttp.send(
    'name='     + document.getElementById('name').value +
    '&address=' + document.getElementById('address').value +
    '&lat='     + document.getElementById('lat').value +
    '&long='    + document.getElementById('long').value +
    '&type='    + document.getElementById('type').value
  );
}

insertMarker.html

<script src="insertMarker.js"></script>

Name        <input type="text" id="name">   <br> 
Address <br><input type="text" id="address"><br> 
Lat     <br><input type="text" id="lat">    <br> 
Long    <br><input type="text" id="long">   <br> 
Type    <br> 

<select style="width: 153px" id="type">
  <option value="vjerski">Vjerski objekti</option>
  <option value="obrazovna">Obrazovne ustanove</option>
  <option value="drzavna">Administracija</option>
  <option value="kulturna">Kulturne ustanove</option>
</select>

<button onclick="insertMarker()">Insert Marker</button>