Having the following difficulty.
I'm currently creating a form. This form is made in HTML and controlled by jQuery.
The form code is as following;
<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>
Now, this form picks up user data, and gets processed by the following jQuery script;
// 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');
}
});
});
});
This code obviously passes the variables through a POST to addC.php.
addC.php contains the following code:
<?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";
}
?>
The desired result of this code is the if-statement returning a 1, so the jQuery can create a nice green message saying the database insertion went correct.
Now, as I validated the pg_query(); syntax to be correct, there must be something wrong in this code itself. What seems to be the problem here?
EDIT:
Following error;
Warning: pg_query_params(): Query failed: ERROR: invalid input syntax for integer: "michael" in /Applications/XAMPP/xamppfiles/htdocs/LoginHQ/processing/addC.php on line 18
– MichaelP Oct 10 '13 at 13:15