-1

I don't know PHP very well because I am android developer so please help me. I have just trying to enter XML data into database using PHP. I have see other examples but don't get what can I do in my case.

XML link : http://ca.sharmatutorial.com/ws.asmx/GET_Question_ByDate?dtDate=2016-03-17

PHP Code: Here what can I define for retrieve and set data in database. I know below code not correct. please correct my for each loop for opt object and other also please check my sql query.

<!doctype HTML> 
  <html>
  <head>
  <?php  
  header('Content-Type: application/xml; charset=utf-8');
  $mysqli = new mysqli ( 'localhost', 'mabhim92', '9993115300', 'gcm_chat');
    ?>
 </head>
 <body>
<?php
$xml = simplexml_load_file("http://ca.sharmatutorial.com/ws.asmx/GET_Question_ByDate?dtDate=2016-03-17");

 $nodes = new SimpleXMLElement($xml, null, true)
     or die("cannot create");

foreach ($nodes->children() as $child)
{
$Ques_id= $child->id;
$Question= $child->text;
$Option_1= $child->opt;
$Option_2= $child->opt;
$Option_3= $child->opt;
$Option_4= $child->opt;
$Answer= $child->opt->ans;
$date= $child->date;
));
$sql = "INSERT INTO feeds (Ques_id, Question, Option_1, Option_2, Option_3, Option_4, Answer, date) VALUES('". $Ques_id."','". $Question."','". $Option_1."','". $Option_2."','". $Option_3."','". $Option_4."','". $Answer."','". $date."')";
mysql_query($sql);
 }
  ?>
 </body>
 </html>

My database field:

1 Ques_id = id

2 Question = text

3 Option_1 = opt (first obj in opts value)

4 Option_2 = opt (second obj in opts value)

5 Option_3 = opt (third obj in opts value)

6 Option_4 = opt (fourth obj in opts value)

7 Answer = (which opt is ans=1)

8 date (timestamp) = date

Kiran Maheshwari
  • 138
  • 1
  • 2
  • 9

1 Answers1

0

You have used mysqli syntax which is good. But at query execution time, you have used old mysql_* syntax which is wrong.

See this link:- MySQL vs MySQLi when using PHP

Suggestions:-

1) You should check connection error as well as query error also.

2) If your query has outer double quotes("") then ther is no need of concatation. You can just add single quote around your php variable.

3) It is good practise to add primary key in your DB table.

4) Write connection object in body tag instead of header tag.

5) Always ON your error reporting in developement mode

<!doctype HTML> 
<html>
<head>    
</head>
<body>
<?php     
  header('Content-Type: application/xml; charset=utf-8');
  $mysqli = new mysqli ( 'localhost', 'mab***', '99931***', 'gcm**');

    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

$xml = simplexml_load_file("http://ca.sharmatutorial.com/ws.asmx/GET_Question_ByDate?dtDate=2016-03-17");

$nodes = new SimpleXMLElement($xml, null, true) or die("cannot create");

foreach ($nodes->children() as $child)
{
    $Ques_id= $child->id;
    $Question= $child->text;
    $Option_1= $child->opt;
    $Option_2= $child->opt;
    $Option_3= $child->opt;
    $Option_4= $child->opt;
    $Answer= $child->opt->ans;
    $date= $child->date;
//)); Typo error. No need for this line
$sql = "INSERT INTO feeds (Ques_id, Question, Option_1, Option_2, Option_3, Option_4, Answer, date) VALUES('$Ques_id','$Question','$Option_1','$Option_2','$Option_3','$Option_4','$Answer','$date')";
$res = $mysqli->query($sql);

    if (!$res) {
       printf("Errormessage: %s\n", $mysqli->error);
    }
}

$mysqli->close(); // close connection
  ?>
</body>
</html>

Hope it will help you :-)

Community
  • 1
  • 1
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
  • no its not my answer I want opt in loop because its multiple in opt node please see I need to parse and insert http://ca.sharmatutorial.com/ws.asmx/GET_Question_ByDate?dtDate=2016-03-17 this link – Kiran Maheshwari Apr 06 '16 at 05:43