2

我正在尝试使用 SQLite 数据库构建网页。一切正常,除了表存储在本地机器上,而不是主机服务器上。所以每个用户只能看到他们输入的数据,这是非常没用的!如何将 SQLite 指向服务器?

我认为这里会发生变化;

var createStatement = "CREATE TABLE IF NOT EXISTS  Contacts (id INTEGER PRIMARY KEY AUTOINCREMENT, firstName TEXT, lastName TEXT, phone TEXT)";
     var selectAllStatement = "SELECT * FROM  Contacts";
     var insertStatement = "INSERT INTO  Contacts (firstName, lastName, phone) VALUES (?, ?, ?)";
     var updateStatement = "UPDATE  Contacts SET firstName = ?, lastName = ?, phone = ? WHERE id = ?";
     var deleteStatement = "DELETE FROM  Contacts WHERE id=?";
     var dropStatement = "DROP TABLE  Contacts";

但是所有的代码都在下面列出......

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Best Test</title>
</head>
<body>


<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
$uploads_dir = 'uploads';
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
  if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
  echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
  if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
    echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],"uploads/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"]."<br>";
      $fname = $_FILES["file"]["name"];
      echo "fname = " . $fname;      


      }
    }
  }
else
  {
  echo "Invalid file";
  }

?>

    <div align="center">
     <input type="hidden" id="id"/>
     First name:<input type="text" id="firstName"/><br/>
     Last name:<input type="text" id="lastName"/><br/>

     <button onClick="resetForm()">Reset Form</button>
     <button onClick="updateRecord()">Update </button>
     <button onClick="preset()">Insert & Procede to Picture Selection</button>
     <button onClick="insertRecord()">Insert</button>
     <button onClick="dropTable()">Drop Table</button>
     <div id="results"></div>


    </div>



  </body>

    <script> 
 var results = document.getElementById('results'); 
 var id = document.getElementById('id');
 var firstName = document.getElementById('firstName');  
 var lastName = document.getElementById('lastName');  
 var phone = 6;

 var createStatement = "CREATE TABLE IF NOT EXISTS  Contacts (id INTEGER PRIMARY KEY AUTOINCREMENT, firstName TEXT, lastName TEXT, phone TEXT)";
 var selectAllStatement = "SELECT * FROM  Contacts";
 var insertStatement = "INSERT INTO  Contacts (firstName, lastName, phone) VALUES (?, ?, ?)";
 var updateStatement = "UPDATE  Contacts SET firstName = ?, lastName = ?, phone = ? WHERE id = ?";
 var deleteStatement = "DELETE FROM  Contacts WHERE id=?";
 var dropStatement = "DROP TABLE  Contacts";

 var db = openDatabase("AddressBook", "1.0", "Address Book", 200000);
 var dataset;
 createTable();

      function onError(tx, error) {
        alert(error.message);
      }

      function showRecords() {
        results.innerHTML = '';
        db.transaction(function(tx) {
          tx.executeSql(selectAllStatement, [], function(tx, result) {
            dataset = result.rows;
            for (var i = 0, item = null; i < dataset.length; i++) {
              item = dataset.item(i);
              results.innerHTML += 
                   '<li>' + item['lastName'] + ' , ' + item['firstName'] + ' , ' + item['phone'] +' <a href="#" onclick="loadRecord('+i+')">edit</a>  ' +  
 '<a href="#" onclick="deleteRecord('+item['id']+')">delete</a></li>';

}
          });
        });
      }

      function createTable() { 
        db.transaction(function(tx) {
          tx.executeSql(createStatement, [], showRecords, onError);
        });
      }

      function insertRecord() {
        db.transaction(function(tx) {
        tx.executeSql(insertStatement, [firstName.value, lastName.value, phone.value], loadAndReset, onError);
        });
      }

      function preset(){
         what ="/uploads/" + "<?php echo $fname; ?>";   
        alert("what = " + what);
        insertRecord2();
              }


      function insertRecord2() {
                alert("Function 2 Started");
                db.transaction(function(tx) {
                tx.executeSql(insertStatement, [firstName.value, lastName.value, what], loadAndReset, onError);
                alert("Function 2 Finished");
        });
      }
    function loadRecord(i) {
    var item = dataset.item(i); 
        firstName.value = item['firstName'];
        lastName.value = item['lastName'];
        phone.value = item['phone'];
        id.value = item['id']; 
      }

      function updateRecord() {
        db.transaction(function(tx) {
        tx.executeSql(updateStatement, [firstName.value, lastName.value, phone.value, id.value], loadAndReset, onError);
        }); 
      }

      function updateRecord2() {
        db.transaction(function(tx) {

          tx.executeSql(updateStatement, [firstName.value, lastName.value, phone.value, id.value], loadAndReset, onError);
        }); 
        fnPass();
      }

      function deleteRecord(id) {
        db.transaction(function(tx) {
          tx.executeSql(deleteStatement, [id], showRecords, onError);
        });
resetForm();
      }

      function dropTable() {
        db.transaction(function(tx) {
          tx.executeSql(dropStatement, [], showRecords, onError);
        });
resetForm();
      }

 function loadAndReset(){
 resetForm();
 showRecords();
 }

 function resetForm(){
firstName.value = '';
lastName.value = '';
phone.value = '';
id.value = ''; 
 }
    </script>



</body>


 Text to Pass: <input type='text' id='idTextToPass'/><br/>
    <input type='button' value='Pass' onclick='fnPass();'/>
</html>

谢谢

大学教师

4

1 回答 1

1

您实际上并没有直接使用 SQLite;您正在使用 HTML5 LocalStorage,其后备存储依赖于浏览器。如果您想使用服务器端数据库,那么您的数据库交互需要发生在服务器端,即在 PHP 代码中,而不是在客户端 Javascript 中。

于 2013-07-19T19:27:28.413 回答