A strange behaviour of response

Dear experts

I have following code block

this is connection string

[type or paste code here](https://lettered-manners.000webhostapp.com/cng_insert2.php?&date=)

and my php file is like this

<?php

date_default_timezone_set("Asia/Karachi");
$servername = "localhost";
$username = "id2064282_ahmad";
$password = "123456";
$database = "id2064282_cng";

$dbc = mysqli_connect($servername, $username, $password, $database);

if (!$dbc) {
    die("DATABASE CONNECTION FAILED:" . mysqli_error($dbc));
    exit();
}

$datex = $_GET["mydate"];
$p_ltr = mysqli_real_escape_string($dbc, $_GET["p_ltr"]);
$p_rat = mysqli_real_escape_string($dbc, $_GET["p_rat"]);
$p_amt = mysqli_real_escape_string($dbc, $_GET["p_amt"]);
 
$query2 = "select * from sales where ddate='$datex'";
 
 $result=mysqli_query($dbc, $query2);
 
 $rows_count = mysqli_num_rows($result);
	if ($rows_count > 0) {

     $query = "update sales set p_ltr='$p_ltr',p_rat='$p_rat',p_amt='$p_amt' where ddate='$datex'";

        if (mysqli_query($dbc, $query)) {
            echo "Records updated successfully";
             exit();
        } else {
            echo "ERROR: Could3 not able to execute" .$query." ".mysqli_error($dbc);
        }
        
 } else {
     
        $query = "INSERT INTO sales(ddate,p_ltr,p_rat,p_amt)
 VALUES ('$datex','$p_ltr','$p_rat','$p_amt')";
        if (mysqli_query($dbc, $query)) {
            echo "Records added successfully";
        } else {
            echo "ERROR: Could not able to execute" .$query." ".mysqli_error($dbc);
        }
    }


mysqli_close($dbc);
?>

When I FIRST TIME click SUBMIT button then this error message appears

and when I SECOND TIME click SUBMIT button then this response receives

As a result I have to click SUBMIT button TWICE every time to save a single row in table.

First time response is -1
second time response is 200

I am very much confuse where is problem? in blocks or in PHP file.

Sir I have tested this more than 20 times.
As a general SUBMIT button must work on every click.

Please

Show the event block that catches the web response to the update.

Why is the replacement slot empty?

1 Like

https://community.appinventor.mit.edu/t/insert-data-to-mysql/82604/11

The idea was to remove the entire block replacing spaces.

Never leave empty inputs in blocks because it may cause errors when compiling or using the app and you won't be able to find the cause. Remember all block inputs must be completed.

1 Like

Sir this time I have removed Replacement block like this

But

Unfortunately the problem is still exist.
I have to click SUBMIT button TWICE to save data.

Regards

Yes, that wasn't the cause of the error. I just noticed an empty block input that shouldn't be there.

You have not corrected the code that @TIMAI2 told you about in a previous topic.

cng_insert2.php?&date=

cng_insert2.php?date=

1 Like

Sir it was

https://lettered-manners.000webhostapp.com/cng_insert2.php?&date=

Now I have replaced it with

https://lettered-manners.000webhostapp.com/cng_insert2.php?date

But issue is still alive

May be I am doing wrong anywhere.

Regards

Please show your web2.GotText event block

There is no -1 http response code, so where is -1 coming from ?

[EDIT - the above is incorrect - see below]

use Do it to debug your blocks, see also tip 4 here App Inventor: How to Learn | Pura Vida Apps
see also Live Development, Testing, and Debugging Tools

Unbenannt2

EDIT: what happens, if you call that url in a browser`?
Unbenannt2

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

I tell a lie, there does appear to be an http status code of -1.

This is usually attributed to network or connectivity problems.

I can think of a workaround, where if you receive -1 then the data is submitted again,

image

but this is not really the answer, unless your first click just so happens to bring the server alive, then it is possible to send the data on the second click. Other than that, I am out of suggestions.

1 Like

Browser result

you forgot to provide a Do it result as suggested earlier so we can check the url

without you using Do it we only can guess...

EDIT: as I can see now you called the url without parameter... this does not help, does it?
obviously you have to send a complete url to the server...

this is an example of a complete url
Unbenannt2

Taifun

1 Like

Should the first parameter be "ddate" and NOT "date" ?

according to the php script it should be mydate... LOL...

$datex = $_GET["mydate"];
$p_ltr = mysqli_real_escape_string($dbc, $_GET["p_ltr"]);
$p_rat = mysqli_real_escape_string($dbc, $_GET["p_rat"]);
$p_amt = mysqli_real_escape_string($dbc, $_GET["p_amt"]);

EDIT: what is the &date variable supposed to do? as far as I can see, it is ignored in the php script...

Taifun

1 Like

In addition to all the other recommendations, it is wise to break up over-long text blocks into a JOIN of two shorter, fully visible text blocks.

You never know what is hiding in that '...' at the end of the block.

dsdswwee

<?php
date_default_timezone_set("Europe/Madrid");
$servername = "localhost";
$username = "id20680000_my_user";
$password = "contraseña";
$database = "id20680000_my_base";

$dbc = mysqli_connect($servername, $username, $password, $database);

if (!$dbc) {
    die("DATABASE CONNECTION FAILED:" . mysqli_error($dbc));
    exit();
}

$datex = $_GET["date"];
$p_ltr = mysqli_real_escape_string($dbc, $_GET["p_ltr"]);
$p_rat = mysqli_real_escape_string($dbc, $_GET["p_rat"]);
$p_amt = mysqli_real_escape_string($dbc, $_GET["p_amt"]);
 
$query2 = "select * from sales where ddate='$datex'";
 
 $result=mysqli_query($dbc, $query2);
 
 $rows_count = mysqli_num_rows($result);
	if ($rows_count > 0) {

     $query = "update sales set p_ltr='$p_ltr',p_rat='$p_rat',p_amt='$p_amt' where ddate='$datex'";

        if (mysqli_query($dbc, $query)) {
            echo "Records updated successfully";
             exit();
        } else {
            echo "ERROR: Could3 not able to execute" .$query." ".mysqli_error($dbc);
        }
        
 } else {
     
        $query = "INSERT INTO sales(ddate,p_ltr,p_rat,p_amt)
 VALUES ('$datex','$p_ltr','$p_rat','$p_amt')";
        if (mysqli_query($dbc, $query)) {
            echo "Records added successfully";
        } else {
            echo "ERROR: Could not able to execute" .$query." ".mysqli_error($dbc);
        }
    }
mysqli_close($dbc);
?>
2 Likes