0

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

Chris Travers
  • 25,424
  • 6
  • 65
  • 182
MichaelP
  • 181
  • 5
  • 22
  • 1
    If any of the 4 fields didn't fit into the destination column, how would you know? What stands out in the php code is the lack of error checking and logging. – Daniel Vérité Oct 10 '13 at 12:34
  • Hi Daniel, thanks for the comment. What is, according to you, the most efficient way to error check? – MichaelP Oct 10 '13 at 12:43
  • 1
    when the return value of `pg_query_params` is null, output `pg_last_error()` on an error channel. If your project doesn't already have an error logging strategy, just use php's `error_log()`. If you have access to postgres log files, you may peek into them too. – Daniel Vérité Oct 10 '13 at 13:11
  • Got the 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
  • so you've found the problem. _michael_ cannot go into a column that's supposed to contain an integer number. – Daniel Vérité Oct 10 '13 at 13:51
  • Problem is, I don't really know where that integer declaration comes from. Database states the column as 'text' and the HTML form does too. – MichaelP Oct 10 '13 at 14:00
  • It' not just one column. All 4 columns and 4 values must correspond. – Daniel Vérité Oct 10 '13 at 14:08
  • Alright; ClientID = bigserial; name = text; ipaddress = cidr; status = integer. Are these data types correct? Now I'm kind of thinking of changing the bigserial.. – MichaelP Oct 10 '13 at 14:13
  • 1
    possible duplicate of [Having difficulties with pg\_query(); in php](http://stackoverflow.com/questions/19293045/having-difficulties-with-pg-query-in-php) – Clodoaldo Neto Oct 10 '13 at 14:53
  • you are inserting a string to an integer field – mamdouh alramadan Jan 22 '14 at 19:13

1 Answers1

0
invalid input syntax for integer: "michael"

It means that column has type integer, but you try insert string

FallDi
  • 660
  • 7
  • 21