在MySql中插入phonegap数据

时间:2013-06-11 03:37:03

标签: php android mysql jquery cordova

我开始将我的简单Ajax-MySql-PHP程序部署到Ajax-MySql-PHP-PhoneGap中,但问题是它没有插入。

这是我的testjs.html

  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Insert title here</title>

  <script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script>

  <script type="text/javascript" src="js/jquery-1.10.1.js"></script>
  <script type="text/javascript" src="js/jquery-1.10.1.min.js"></script>

  <script>
  document.addEventListener("deviceready", onDeviceReady, false);


  function onDeviceReady() {
console.log("Hello ");
    $("#button").click(function(evt){
    var name = $("#name").val();
    var message = $("#message").val();
    var sendData = { "name": name, "message": message };

    $.ajax({
        type: "POST",
        url: "http://localhost/webs/main/ajax/process.php",
        data: sendData,
        success: function(data) {
            log.console(data);
             $("#info").html(data);

        }

    });

     alert("Hello ");
     alert($("#test").val());
     alert($("#name").val());
     alert($("#message").val());
     return false;


  });
  }

  </script>

  </head>
  <body>
  <form >
  name : <input type="text" name="name" id="name"/>
           </br>
   message : <input type="text" name="message" id="message" />
           </br>
  <input type="text" id="test" name="test" />
  <input type="button" id="button" value="Ajax Submit">
 <div id="info"></div>   <span id="msg"></span>
 </form>
 </body>
 </html>

这是我的process.php

 <?php

$host = "localhost";
$user = "root";
$pass = "";
$dbname = "comments";
$dsn = "mysql:host=$host; dbname=$dbname";

$pdo = new PDO($dsn, $user, $pass);

$name=$_POST["name"];
$message=$_POST["message"];
try{
$sql = "INSERT INTO comment2 (name,message) VALUES ('$name' , '$message')";
$pdo->exec($sql);
  echo "Your comment has been sent";
 } catch (PDOException $e) {
die('Opps' .$e->getMessage());
 }


?>

这是我的LogCat

  06-11 18:38:24.652: E/Trace(790): error opening trace file: No such file or  
  directory (2)
  06-11 18:38:25.021: I/CordovaLog(790): Changing log level to DEBUG(3)
  06-11 18:38:25.021: I/CordovaLog(790): Found preference for useBrowserHistory=true
  06-11 18:38:25.091: D/CordovaLog(790): Found preference for useBrowserHistory=true
  06-11 18:38:25.091: I/CordovaLog(790): Found preference for exit-on-suspend=false
  06-11 18:38:25.091: D/CordovaLog(790): Found preference for exit-on-suspend=false
  06-11 18:38:25.091: D/DroidGap(790): DroidGap.onCreate()
  06-11 18:38:25.241: D/dalvikvm(790): GC_FOR_ALLOC freed 70K, 8% free 2480K/2672K, 
  paused 41ms, total 52ms
  06-11 18:38:25.251: I/dalvikvm-heap(790): Grow heap (frag case) to 3.144MB for  
  635812-byte allocation
  06-11 18:38:25.342: D/dalvikvm(790): GC_FOR_ALLOC freed 1K, 6% free 3099K/3296K, 
  paused 94ms, total 94ms
  06-11 18:38:25.441: D/dalvikvm(790): GC_CONCURRENT freed <1K, 6% free 3110K/3296K,  
  paused 4ms+11ms, total 97ms
  06-11 18:38:25.601: D/CordovaWebView(790): CordovaWebView is running on device made  
  by: unknown
  06-11 18:38:25.635: D/JsMessageQueue(790): Set native->JS mode to 2
  06-11 18:38:25.642: D/DroidGap(790): DroidGap.init()
  06-11 18:38:25.692: D/CordovaWebView(790): >>> loadUrl(file:///android_asset
 /www/testjs.html)
  06-11 18:38:25.702: D/PluginManager(790): init()
  06-11 18:38:25.753: D/CordovaWebView(790): >>> loadUrlNow()
  06-11 18:38:25.753: D/DroidGap(790): Resuming the App
  06-11 18:38:25.832: D/SoftKeyboardDetect(790): Ignore this event
  06-11 18:38:26.002: D/gralloc_goldfish(790): Emulator without GPU emulation detected.
  06-11 18:38:26.222: D/SoftKeyboardDetect(790): Ignore this event
  06-11 18:38:26.782: D/DroidGap(790): onMessage(onPageStarted,file:///android_asset
  /www/testjs.html)
  06-11 18:38:33.262: D/CordovaNetworkManager(790): Connection Type: 3g
  06-11 18:38:33.262: D/DroidGap(790): onMessage(networkconnection,3g)
  06-11 18:38:33.337: D/CordovaNetworkManager(790): Connection Type: 3g
  06-11 18:38:33.381: D/DroidGap(790): onMessage(spinner,stop)
  06-11 18:38:33.492: D/TilesManager(790): Starting TG #0, 0x2a259520
  06-11 18:38:33.582: D/Cordova(790): onPageFinished(file:///android_asset 
  /www/testjs.html)
  06-11 18:38:33.582: D/DroidGap(790): onMessage(onPageFinished,file:///android_asset
  /www/testjs.html)
  06-11 18:38:33.642: D/CordovaLog(790): Hello 
  06-11 18:38:33.642: I/Web Console(790): Hello  at file:///android_asset 
   /www/testjs.html:17
  06-11 18:38:38.382: D/SoftKeyboardDetect(790): Ignore this event
  06-11 18:38:39.522: D/SoftKeyboardDetect(790): Ignore this event
  06-11 18:38:40.302: D/SoftKeyboardDetect(790): Ignore this event
  06-11 18:38:46.661: D/dalvikvm(790): GC_CONCURRENT freed 253K, 11% free 3269K/3644K, 
  paused 6ms+40ms, total 102ms
  06-11 18:38:47.061: I/Choreographer(790): Skipped 46 frames!  The application may be 
  doing too much work on its main thread.
  06-11 18:38:49.091: I/Choreographer(790): Skipped 95 frames!  The application may    be    doing too much work on its main thread.
  06-11 18:38:50.651: I/Choreographer(790): Skipped 74 frames!  The application may be 
  doing too much work on its main thread.
  06-11 18:38:52.081: D/dalvikvm(790): GC_CONCURRENT freed 24K, 5% free 3654K/3812K, 
  paused 70ms+86ms, total 191ms
  06-11 18:38:52.552: I/Choreographer(790): Skipped 85 frames!  The application may be 
  doing too much work on its main thread.

显示所有警报意味着onDeviceReady()正在运行。在我部署到phoneGap之前,此代码也在Web上工作。我在这里错过了什么吗?谢谢。

1 个答案:

答案 0 :(得分:0)

正如我之前评论的,自从您部署了应用程序以来,您无法通过localhost引用名为process.php的Php页面。您必须指向托管该Php页面的服务器的地址。所以你的代码看起来像这样:

    $.ajax({
    type: "POST",
    url: "http://<INSERT YOUR SERVER IP ADDRESS OR DOMAIN HERE>/webs/main/ajax/process.php",
    data: sendData,
    success: function(data) {
        log.console(data);
         $("#info").html(data);

    }
   });

当然假设您的服务器中有相同的文件夹/目录结构。

另外,请注意@Marc B关于SQL Injection的评论。确保在将查询发送到数据库之前清理用户输入。 Here is a good post关于如何使用Php。

清理mysql输入

希望这有帮助