0

Trying to POST to PHP to update database.

If I do a single parameter it works fine but if I try two parameters it is not working.

How do I post with two parameters?

VisualBasic Code

        Dim dateTime As DateTime = ReturnAsDateTime(thedate, thetime)
        Dim dateformat2 As String = "M/d/yyyy HH:mm"
        Dim dateformat3 As String = dateTime.ToString(dateformat2)
        Dim StudentNOString As String = thestudent.Number.ToString()
        Dim upString As String = "future01=" & dateformat3 & "StudentNumber=" & StudentNOString


        System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
        Dim request As WebRequest = WebRequest.Create("https://example.com/tofilePHPfile.php")
        request.Method = "POST"
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(upString)
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = byteArray.Length
        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()
        Dim response As WebResponse = request.GetResponse()
        dataStream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        reader.Close()
        dataStream.Close()
        response.Close()
        MsgBox(responseFromServer)

PHP Code

<?php

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}


if (isset ($_POST["future01"]) && isset ($_POST["StudentNumber"])){
    $future01 = $_POST["future01"];
    $StudentNumber = $_POST["StudentNumber"];
} else {
    $future01 = "testfortestfortest";
    $StudentNumber = "1";
    echo "not working";
}

$stmt = $conn->prepare("UPDATE pushtable SET future01=? WHERE StudentNumber=?");

$stmt->bind_param('ss', $future01,$StudentNumber);

$stmt->execute();

echo $future01;
//echo $StudentNumber;
echo "New records created successfully";

$stmt->close();
$conn->close();
?>

There is something wrong with this line I think as the PHP is not posting it correctly.

   Dim upString As String = "future01=" & dateformat3 & "StudentNumber=" & StudentNOString

I have tried dozens of different ways and tried to search for my problem but I cannot seem to find an answer. The PHP works perfectly well from IOS objective-c code. So, I think my problem is in the Visual Basic Code.

future01 and StudentNumber are not being recognized individually when POST occurs.

These are some of the pages I have looked at:

How to send and recieve data from Visual Basic to PHP?

https://www.informit.com/articles/article.aspx?p=131236&seqNum=7

POST Request using VBScript

Multiple key/value pairs in HTTP POST where key is the same name

How can i send multiple variables from vb.net to php via post method?

https://en.wikipedia.org/wiki/POST_(HTTP)#Use_for_submitting_web_forms

John
  • 5
  • 6
  • What kind of database is it? MS Sql Server, Oracle, MySql? – Mary Sep 29 '21 at 05:06
  • I'm not a web dev, but I can clearly see you don't have any delimiter in your parameters: `Dim upString As String = "future01=" & dateformat3 & "StudentNumber=" & StudentNOString` This will produce a results such as `future01=9/29/2021 10:00StudentNumber=123` – HardCode Sep 29 '21 at 16:16
  • @HardCode - Sorry for the late reply. I didn't receive any notifications from StackOverflow so I thought no one had replied. Yes, that was the issue! Thank you for helping out. – John Nov 09 '21 at 06:54

1 Answers1

0

You are missing a delimiter between your parameters. This code...

Dim upString As String = "future01=" & dateformat3 & "StudentNumber=" & StudentNOString

...will produce output like this:

future01=9/29/2021 10:00StudentNumber=123

HardCode
  • 6,497
  • 4
  • 31
  • 54