0

以下の難点があります。

現在フォームを作成中です。このフォームは 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";
}

?>

このコードの望ましい結果は、if ステートメントが 1 を返すことです。そのため、jQuery は、データベースの挿入が正しく行われたことを示す素敵な緑色のメッセージを作成できます。

さて、pg_query(); を検証したので、構文を正しくするには、このコード自体に何か問題があるに違いありません。ここで何が問題になっているようですか?

編集:

次のエラー; 警告: pg_query_params(): クエリが失敗しました: エラー: 整数の入力構文が無効です: "michael" in /Applications/XAMPP/xamppfiles/htdocs/LoginHQ/processing/addC.php18

4

1 に答える 1