0
void SignUp()
{
  MYSQL* conn; 
  MYSQL_ROW row;
  MYSQL_RES* res;
  int nQueryState = 0;
  int nId         = 0;
  char szName[45];
  char szPassWord[45];
  char szQuestion[45];
  char szPhone[45];

  cout<<"Enter the Id";
  cin>>nId;

  cout<<"Enter the Name";
  cin>>szName;

  cout<<"Enter the Password";
  cin>>szPassWord;

  cout<<"Enter the Question";
  cin>>szQuestion;

  cout<<"Enter the Phone";
  cin>>szPhone;

 conn = mysql_init(0);
 conn =mysql_real_connect(conn,"localhost","root","12356","hari",0,NULL,0);
nQueryState = mysql_query(conn,"insert into  userdetails values(nId,szName,szPassword,szQuestion,szPhone)");

if(0!=nQueryState)
{
    cout<<"\n\nConnection not Established";
}
}

I want to enter details into table named userdetails. It give me an output such that connection not esatablished.please go through it and help me. I'm using codeblock ide and mySql5.1

Norbert
  • 2,741
  • 8
  • 56
  • 111
hari s g
  • 53
  • 2
  • 13
  • 2
    You should add some error checking, like checking if `mysql_init` or `mysql_real_connect` doesn't return a null pointer. – Some programmer dude Nov 23 '16 at 08:09
  • Insert does not specify column names of `userdetails` table, which might be a problem in some cases. Check if `mysql_query` reports an error and print it. – karastojko Nov 23 '16 at 08:49

1 Answers1

0

Try as follows:

MYSQL mysql,*connection;
MYSQL_RES result;
MYSQL_ROW row;


mysql_init(&mysql);
connection =mysql_real_connect(&mysql,"localhost","root","12356","hari",0,NULL,0);

if (connection==NULL)
{
    cout<<mysql_error(&mysql)<<endl;
}
else{
    nQueryState = mysql_query(&mysql,"insert into  userdetails values('"+nId+"','"+szName+"','"+szPassword+"','"+szQuestion+"','"+szPhone+"')");

  if (nQueryState !=0) {
    cout << mysql_error(connection) << endl;
    return 1;
  }
}

mysql_close(&mysql);

Refer:http://www.codeproject.com/Questions/337901/How-to-insert-data-in-database-using-Cplusplus

Chandana Kumara
  • 2,485
  • 1
  • 22
  • 25
  • Here it shows an error such that szname is not a filed.Actually the field name is name.I want to insert a user input that stores in szName.Value stored in szname into database.Plz help. Thanks in advance – hari s g Nov 23 '16 at 11:22
  • I changed insert query. Pls check it. – Chandana Kumara Nov 23 '16 at 11:36
  • I changed the Insert Query.But It shows be a syntax error such that : "invalid operands of types 'const char*' and 'const char [4]' to binary 'operator+'|".Plz help me to avoid this error.Thank in advance – hari s g Nov 24 '16 at 03:49
  • Pls refer this:http://stackoverflow.com/questions/23936246/error-invalid-operands-of-types-const-char-35-and-const-char-2-to-binar – Chandana Kumara Nov 24 '16 at 06:02