2013-10-10 83 views
0

有以下困难。将数据从jQuery post处理到pg_query()时遇到困难;

我目前正在创建一个表单。此表单由HTML制作并由jQuery控制。

表格代码如下;

<div id="form"> 
     <form> 
      <label>ID :</label> 
      <input type="text" id="clientid" /><br /><br /> 
      <label>Name :</label> 
      <input type="text" id="name" /><br /><br /> 
      <label>IP Address :</label> 
      <input type="text" id="ipaddress"/><br /><br /> 
      <label>Status :</label> 
      <input type ="text" id ="status" /> 
      <input type="button" id="button" value="Insert" /><br /><br /><br /> 
      <label id="response"></label> 
     </form> 

现在,这个表单会提取用户数据,并被下面的jQuery脚本处理;

// Start jQuery script 
// jQuery for dynamic adding without complete page reloads 
//Wait for document readiness 

$('document').ready(function() { 
    // Define submit button and action 
    $('#button').click(function() { 

     // Assign variable 
     if ($('#clientid').val() == "") { 
      alert("Enter Client ID"); 
      return false; 
     } else { 
      var clientid = $('#name').val(); 
     } 

     // Assign variable 
     if ($('#name').val() == ""){ 
      alert("Enter Client full name"); 
      return false; 
     } else { 
      var name =$('#name').val();       
     } 

     // Assign variable 
     if ($('#ipaddress').val() == "") { 
      alert("Enter Client owned IP address"); 
      return false; 
     } else { 
      var ipaddress = $('#ipaddress').val(); 
     } 

     // Assign variable 
     if ($('#status').val() == "") { 
      alert("Enter client status"); 
      return false; 
     } else { 
      var status = $('#status').val(); 
     } 

     // When variables are known, continue processing and POST'ing 
     // Posting to seperate PHP file to complete 
     jQuery.post("processing/addC.php", { 
      clientid: clientid, 
      name: name, 
      ipaddress: ipaddress, 
      status: status 
     }, 

     function(data, textStatus) { 
      if (data == 1) { 
       $('#response').html("Insert successful!"); 
       $('#response').css('color', 'green'); 
      } else { 
       $('#response').html("Insertion failure. Please try again or restart."); 
       $('#response').css('color', 'red'); 
      } 
     }); 
    }); 
}); 

这段代码很明显地将变量通过POST传递给addC.php。

addC.php包含以下代码:

<?php 

// Get current connection 
include 'dbconnect.php'; 

$clientid = $_POST['clientid']; 
$name = $_POST['name']; 
$ipaddress = $_POST['ipaddress']; 
$status = $_POST['status']; 

$query = pg_query_params(
    $dbconnection, 
    'INSERT INTO clients(clientid, name, ipaddress,status) VALUES ($1, $2, $3, $4);', 
    array($clientid, $name, $ipaddress, $status) 
);       

if(pg_affected_rows($query)>0){ 
    echo "1"; 
}else{ 
    echo "2"; 
} 

?> 

这段代码的期望的结果是,如果语句返回1,因此jQuery的可以创建一个漂亮的绿色消息说数据库插入正确的去了。

现在,我验证了pg_query();语法是正确的,这个代码本身肯定有问题。这里似乎是什么问题?

编辑:

跟随误差; 警告:pg_query_params():查询失败:错误:整数无效输入语法:"迈克尔"在/Applications/XAMPP/xamppfiles/htdocs/LoginHQ/processing/addC.php上线

+1

如果有4个区域的不适合到目标列,你怎么知道?在PHP代码中突出的是缺少错误检查和日志记录。 –

+0

嗨丹尼尔,感谢您的评论。据你所知,最有效的错误检查方法是什么?当“pg_query_params”的返回值为空时,在错误通道上输出“pg_last_error()”时为 – MichaelP

+1

。如果你的项目还没有错误记录策略,只需使用php的'error_log()'。如果你有权访问postgres日志文件,你也可以查看它们。 –

回答

0
invalid input syntax for integer: "michael" 

这意味着该列有整型,但你尝试插入串