I am trying to insert values into a test database that I have created from a Winform Application. I have it working for the most part but there is always one of the keys missing when I send over the POST request.
I have a php file stored on lamp stack and the pertaining code to insert is below:
$CustId = mysqli_real_escape_string($con, $_POST['CustId']);
$LastName = mysqli_real_escape_string($con, $_POST['LastName']);
$FirstName = mysqli_real_escape_string($con, $_POST['FirstName']);
$DOB = mysqli_real_escape_string($con, $_POST['DOB']);
$CustPhone = mysqli_real_escape_string($con, $_POST['CustPhone']);
$LatestToken = mysqli_real_escape_string($con, $_POST['LatestToken']);
$AcntLast4 = mysqli_real_escape_string($con, $_POST['AcntLast4']);
$AuthKey = mysqli_real_escape_string($con, $_POST['AuthKey']);
$sql="INSERT INTO Customer (CustId, LastName, FirstName, DOB, CustPhone,
LatestToken, AcntLast4, AuthKey) VALUES ('$CustId', '$LastName',
'$FirstName', '$DOB', '$CustPhone', '$LatestToken', '$AcntLast4',
'$AuthKey')";
Now on the C# Winforms Side I have the following code as a button event. NOTE I have hardcoded the values because I didn't want to type them in the text box everytime
string URL = "http://localhost/InsertCustomer.php";
WebClient webClient = new WebClient();
NameValueCollection formData = new NameValueCollection();
formData["CustId"] = "avc7";
formData["LastName"] = "Tired";
formData["FirstName"] = "Iam";
formData["DOB"] = "2009-05-04";
formData["CustPhone"] = "54433454";
formData["LatestToken"] = "85858dgggd"; //We get this from the call
formData["AcntLast4"] = "9874"; //We get this from the call
formData["Authkey"] = "fake"; //This is stored in config file
byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
string responsefromserver = Encoding.UTF8.GetString(responseBytes);
richTextBox1.Text = responsefromserver;
webClient.Dispose();
Now when I fire this event it stores all of the values into the mysql table with the exception of "AuthKey".
There are 2 tables. One table is called Customer which is structured as follows:
CUSTOMER
CustID Primary Key Not null
LastName varchar
FirstName varchar
DOB Date
CustPhone varchar
LatestToken varchar
AuthKey Primary Key Not Null
AcntLast4
There is also a History table
HISTORY
HistoryNum Primary Not Null Auto Increment
TransAmt varchar
Transdate date
TransType varchar
AuthKey varchar
CustId varchar
When I try to insert into the customer table as shown above it does not fill in a value for "AuthKey" but it does for everything else.
And when I try to insert into the history table it does not fill "AuthKey" or "CustId" but it does for everything else.
Somewhere I think I am screwing up on keys. This database may not be modeled appropriately at this time but why is it behaving this way? I have been staring at it and trying several little changes to see if it fixes but I have had no success. This is my last resort. Thanks
One other note: When I do the queries simply in the terminal using mysql everything inserts just fine. So I am just not sure what is going on here
EDIT: Some things I have done to narrow down the problem.
- I know that the table is fine considering that I can type these insert queries straight from the mysql terminal and they behave just fine.
- I know that the NameValueCollection is in fact holding the correct data as I ran a loop and printed out the values and "AuthKey" value was there.