Jump to content

insert statement


westminster86

Recommended Posts

hey guys. just a little minor problem i got. The inser statemenst i have in the loop dnt seem to be inserting into my table, apart from one, the last one. any ideas why> do i need to insert a comma or something at the end.

Below is my insert satements in the for each loop. bmw, z4 m roadster, 23.3mpg is only being inserted...

 

INSERT INTO cars VALUES('Alfa Romeo', 'Alfa GT', '32.5mpg')INSERT INTO cars VALUES('Alfa Romeo', 'Alfa Brera', '24.6mpg')INSERT INTO cars VALUES('Alfa Romeo', 'Alfa Spider', '24.6mpg')INSERT INTO cars VALUES('Aston Martin', 'DBS', '17.3mpg')INSERT INTO cars VALUES('Aston Martin', 'DB9', '17.8mpg')INSERT INTO cars VALUES('Aston Martin', 'V8 Vantage', '18.8mpg')INSERT INTO cars VALUES('Audi', 'Audi Q7', '25.4mpg')INSERT INTO cars VALUES('Audi', 'R8', '19.3mpg')INSERT INTO cars VALUES('Audi', 'TT Roadster', '29.7mpg')INSERT INTO cars VALUES('Bentley', 'Continental GT', '17.0mpg')INSERT INTO cars VALUES('Bentley', 'Continental Flying Spur', '16.5mpg')INSERT INTO cars VALUES('Bentley', 'Continental GTC', '16.5mpg')INSERT INTO cars VALUES('BMW', 'M6 Coupe', '19.1mpg')INSERT INTO cars VALUES('BMW', 'M6 Convertible', '18.6mpg')INSERT INTO cars VALUES('BMW', 'Z4 M Roadster', '23.3mpg')

 

 

 

 

<?php
  ini_set('soap.wsdl_cache_enabled', 'Off');

  $soap = new SoapClient('info.wsdl');  // wsdl code shows where the SOAP server code is expected to be found
  
  try {
    $avalue = $soap->getinfo();  // call the getinfo method in the soap object
    } catch (SoapFault $ex) {
    echo $ex->faultstring;
  }

  //print $avalue;

  $dbhandle = sqlite_popen("", 0666, $err_msg);
  if(!$dbhandle) die("Could not open the database");

  $query = "CREATE TABLE cars(manufacturer VARCHAR(30), model VARCHAR(30), mpg VARCHAR(30))";
  if(@!sqlite_query($dbhandle, $query)) {
  }

  $sxe = simplexml_load_string($avalue);

  foreach ($sxe->carmodel as $onecarmodel)
  {
    $makername = $onecarmodel->makername;
    $modelname = $onecarmodel->modelname;
    $mpg = $onecarmodel->mpg;
    
    $query = "INSERT INTO cars VALUES('$makername', '$modelname', '$mpg')";
    echo $query;
  }

  if(@!sqlite_query($dbhandle, $query)) {  
  }

  $query = sqlite_query($dbhandle, 'SELECT * FROM cars');
  $result = sqlite_fetch_all($query, SQLITE_ASSOC);

  foreach ($result as $arow) {
    //echo '<br> Manufacturer: ' . $arow['manufacturer'] . ' Model: ' . $arow['model'] . ' MPG: ' . $arow['mpg'];
  }
  sqlite_close($dbhandle);
?>

Link to comment
Share on other sites

you have to execute each $query inside the loop, but you don't. you only execute the last one.

 

  foreach ($sxe->carmodel as $onecarmodel)
  {
    $makername = $onecarmodel->makername;
    $modelname = $onecarmodel->modelname;
    $mpg = $onecarmodel->mpg;
    
    $query = "INSERT INTO cars VALUES('$makername', '$modelname', '$mpg')"; // You set $query
    echo $query;

     // but you never execute query in this loop, so $query gets dumped and reset to the next query
  }

  // Finally, outside the loop, you execute 1 query, the last one.
  if(@!sqlite_query($dbhandle, $query)) {  
  }

 

the solution is to move the query inside the loop so it is executed once for every $query instead of just once for the last one.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.