Click here to Skip to main content
15,886,661 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
my c# code is
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            HttpWebRequest httpWReq =
    (HttpWebRequest)WebRequest.Create("http://localhost/m.php");

ASCIIEncoding encoding = new ASCIIEncoding();
string postData =textBox1.Text;
byte[] data = encoding.GetBytes(postData);

httpWReq.Method = "POST";
httpWReq.ContentLength = data.Length;

using (Stream newStream = httpWReq.GetRequestStream())
{
    newStream.Write("postData");
}
        }
    }
}

My php code is
PHP
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("data", $con);

$sql="INSERT INTO Persons (Name)
VALUES
('$_POST[httpWReq]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
?>

There is mistake in php code in this line can any one solve this mistake??
PHP
$sql="INSERT INTO Persons (Name)
VALUES
('$_POST[httpWReq]')";
Posted
Updated 26-Jul-12 8:38am
v3

It's actually $_POST['httpWReq']. You need to quote the post parameter name, not the post as a whole
 
Share this answer
 
You have several mistakes, among which:
1) with newStream.Write("postData");}, you write the postData string to the stream, not the variable
2) you don't construct the request, you put some data in the body, but there are no fields
3) there is no ContentType specified
4) you can read the post body even without fields in php, but yo need to treat is as raw data (google for it)
5) httpWReq is in your c# code, and has no meaning in php, the $_POST array will contain the fields of your body, but you have none - to get such a field, you need to put it in the stream as name-value pair
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900