PDA

View Full Version : im going to shoot myself



eskimo
02 Oct 2009, 09:11 AM
please help me fix this so i dont have to shoot myself
thanks

basically i am writing a script to insert info from a text file into my database
the listings in the text file look like this:

Kia Picanto 1.0 5dr
£6495

Fiat Panda 1.1 Active ECO 5dr
£7095

etc...

my database has 4 records:
id (auto inc)
make
model
price

id does not get a value from the script, but auto increments
make gets its value from the first word in the line of text (ie, Kia or Fiat etc...)
model is the text up until (and not including) the £
price is supposed to be everything after the £


that is where the problem comes in
the id, make and model are getting written to the database fine, but the price is not. i have tried making the price a varchar instead of an int but it makes no difference.
it seems the else if loop is never actually running

Here is the code:



<?php

include ('Connections/textcon.php');

$handle = @fopen("xyz.txt", "r"); // Open file form read.

if ($handle) {
while (!feof($handle)) // Loop til end of file.
{

$theData = fgets($handle, 4096); // Read a line
if (strlen($theData) > 2) {
if (strrpos($theData, "£") === false ) {
$carmake = substr($theData, 0, strpos($theData, " ") ); // goes from character position 0 to the first occurance of a space
$carmodel = substr($theData, strpos($theData, " " , strlen($theData) )); // goes from first occurance of a space to end

$queryinsert = "INSERT INTO carsuk.cars (makeid, model)
VALUES ('$carmake','$carmodel')";
$result= mysql_query($queryinsert) or die ("error in query: $query. " . mysql_error());


}



else if (strrpos($theData, "£") === true) {
$lengthy = strlen($theData);
echo "hello";
$carprice = substr($theData, 1,$lengthy );
$getlastrowiinserted_queryn=sprintf("SELECT count(*) as rowid FROM carsuk.cars");

$getlastrowiinserted= mysql_query($getlastrowiinserted_queryn, carsuk.cars) or die(mysql_error());
while ($line=mysql_fetch_array($getlastrowiinserted))
{
$rowid= $line['rowid'];
}
mysql_query("UPDATE carsuk.cars SET price= '$carprice' WHERE rowid = '$rowid' ");
}
}


}
}
fclose($handle);


?>