Jump to content

Nickmadd

Members
  • Posts

    25
  • Joined

  • Last visited

Everything posted by Nickmadd

  1. Hey guys having a bit of bother here, I don't understand why my PHP script isn't working. So I will first show the code and then go over the issue I am having: include('select-stock.php'); include('db-affinity/header-main.php'); ?> <?php $carId = $_GET['id']; ?> <?php try { $carview = $db->prepare("SELECT Make, Model, Colour, FuelType, Year, Mileage, Bodytype, Doors, Variant, EngineSize, Price, Transmission, PictureRefs, ServiceHistory, PreviousOwners, Options, FourWheelDrive, FullRegistration FROM import WHERE FullRegistration = $carId"); } catch (Exception $e) { echo "Error."; exit; } $cardata = $carview->fetch(PDO::FETCH_ASSOC) ?> <div class="container"> <div class="row"> <div class="col-md-12 col-sm-12"> <?php echo "$carId"; ?> <?php echo mysql_errno($carview) ?> <?php echo '<ul class="overwrite-btstrp-ul other-specs-ul h4-style"> <li>Mileage: '.number_format($cardata["Mileage"]).'</li> <li>Engine size: '.$cardata["EngineSize"].'cc</li> </ul>' ?> </div> </div> </div> <?php include('db-affinity/footer.php') ?> So basically what I am trying to achieve from this code is giving my page dynamic content based on if the the ?id= of a URL matches a row of my 'FullRegistration' column. So for example if I have a URL like this "www.cars.com/carview.php?id=NG61CWJ" I then want my script check if there is a row that has that value in the 'FullRegistration' column of my table and then echo out the results of certain columns of that row like this example currently in my code: <?php echo '<ul class="overwrite-btstrp-ul other-specs-ul h4-style"> <li>Mileage: '.number_format($cardata["Mileage"]).'</li> <li>Engine size: '.$cardata["EngineSize"].'cc</li> </ul>' ?> In theory FROM import WHERE FullRegistration = $carId should make this happen however for some reason on my server when I use the script above I get nil results returned instead of the results of the row that matches the GET id I get: Mileage: 0 Engine size: cc I am aware my code is insecure at the moment however it isn't an issue at this moment in time. Any ideas why I might be getting nil results returned, my other queries to this table have worked flawlessly however I am having bother with this one, can you see anything in this code that might cause this issue? Here is the database connection file that is included at the top of the code block just in case this could be a bit of a problem: <?php include('database.php'); try { $results = $db->query("SELECT Make, Model, Colour, FuelType, Year, Mileage, Bodytype, Doors, Variant, EngineSize, Price, Transmission, PictureRefs, ServiceHistory, PreviousOwners, Options, FourWheelDrive, FullRegistration FROM import ORDER BY Make ASC"); } catch (Exception $e) { echo "Error."; exit; } ///carousel-vehicle results try { $fourresults = $db->query("SELECT Make, Model, Colour, FuelType, Year, Mileage, Bodytype, Doors, Variant, EngineSize, Price, Transmission, PictureRefs, ServiceHistory, PreviousOwners, Options, FourWheelDrive FROM import ORDER BY Make LIMIT 0, 4"); } catch (Exception $e) { echo "Error."; exit; } try { $fourresultsone = $db->query("SELECT Make, Model, Colour, FuelType, Year, Mileage, Bodytype, Doors, Variant, EngineSize, Price, Transmission, PictureRefs, ServiceHistory, PreviousOwners, Options, FourWheelDrive FROM import ORDER BY Make LIMIT 4, 4"); } catch (Exception $e) { echo "Error."; exit; } try { $fourresultstwo = $db->query("SELECT Make, Model, Colour, FuelType, Year, Mileage, Bodytype, Doors, Variant, EngineSize, Price, Transmission, PictureRefs, ServiceHistory, PreviousOwners, Options, FourWheelDrive FROM import ORDER BY Make LIMIT 8, 4"); } catch (Exception $e) { echo "Error."; exit; } try { $makeFilter = $db->query("SELECT DISTINCT Make FROM import ORDER BY Make ASC"); } catch (Exception $e) { echo "Error."; exit; } try { $modelFilter = $db->query("SELECT DISTINCT Model FROM import ORDER BY Make ASC"); } catch (Exception $e) { echo "Error."; exit; } ?> All of these queries are working flawlessly on the live site so the db connection is obviously working.
  2. I only need simple ID's for each row such as 1, 2, 3 etc. The reason I am doing this is because I will be giving each one of the rows in my table it's own page. To do this I need to give them there own ID's, thus needing a column that has an ID for each row, I cant add the ID's to the CSV file as it is from a third party.
  3. Hey guys, I'm a little confused to how I can do this. I am basically wanting to give my first column a 'NOT NULL AUTO_INCREMENT' and give each row it's own 'id'. The issue I am having is that the script I am using truncates the whole SQL table with a CSV file that is cron'd daily to update data. I am currently using this script: <?php $databasehost = "localhost"; $databasename = ""; $databasetable = ""; $databaseusername=""; $databasepassword = ""; $fieldseparator = ","; $lineseparator = "\n"; $enclosedbyquote = '"'; $csvfile = "db-core/feed/csv/csv.csv"; if(!file_exists($csvfile)) { die("File not found. Make sure you specified the correct path."); } try { $pdo = new PDO("mysql:host=$databasehost;dbname=$databasename", $databaseusername, $databasepassword, array( PDO::MYSQL_ATTR_LOCAL_INFILE => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ) ); } catch (PDOException $e) { die("database connection failed: ".$e->getMessage()); } $pdo->exec("TRUNCATE TABLE `$databasetable`"); $affectedRows = $pdo->exec(" LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." REPLACE INTO TABLE `$databasetable` FIELDS OPTIONALLY ENCLOSED BY ".$pdo->quote($enclosedbyquote)." TERMINATED BY ".$pdo->quote($fieldseparator)." LINES TERMINATED BY ".$pdo->quote($lineseparator)." IGNORE 1 LINES"); echo "Loaded a total of $affectedRows records from this csv file.\n"; ?> Is it possible to amend this script to ignore my first column and truncate all of the data in the table apart from the first column? I could then give all of the rows in the first column their own ID's any idea how I could do this? I am still very nooby so please go easy on me
  4. Yes I do need to relate the Make column with the Model column, I am unsure how this can be done. Why would I need three tables though?
  5. Hey guys seem to have tricky question here, I am just wondering how I could do the following: I have an SQL table that contains these columns: As you can see there is a 'Make' column and a 'Model' column and each row displays what make it is. I now want to associate the 'Makes' with the 'Models' in another table just like this example: BMW AUDI VOLVO FORD TOYOTA X5 All Road XC90 Focus Supra 3 Series A1 C30 Mustang Yaris 5 Series Galaxy So for example if the first table has a row that contains the make 'AUDI' within the 'Make' column I then want it to take the value of the 'Model' and list it into my other table under the 'AUDI' column, any ideas how I can do this with PHP? Thanks!
  6. Hey guys I am a little confused, I have a listing page that displays all of my rows in my SQL table. An example of what I am developing can be seen here: http://www.drivencarsales.co.uk/used-cars.php So as you can see I have developed a listing page that fetches all of the table rows, each row is equivalent to one vehicle. I now want to let users filter the results using the form to the left, I was going to use AJAX originally however I feel as if it would take way to long to learn how to develop it that way. Here is the code setup I am using to achieve the example I have shown: <?php include('database.php'); try { $results = $db->query("SELECT Make, Model, Colour, FuelType, Year, Mileage, Bodytype, Doors, Variant, EngineSize, Price, Transmission, PictureRefs, ServiceHistory, PreviousOwners, Options, FourWheelDrive FROM import ORDER BY Make ASC"); } catch (Exception $e) { echo "Error."; exit; } try { $filterres = $db->query("SELECT DISTINCT Make FROM import ORDER BY Make ASC"); } catch (Exception $e) { echo "Error."; exit; } ?> As you can see the first block of code has two SQL selectors, the $results is used to fetch the whole table and list all vehicles. The second block is used to display the 'Make' column for the form. This block of code is the actual form: <form> <select class="form-control select-box" name=""> <option value="make-any">Make (Any)</option> <?php while($make = $filterres->fetch(PDO::FETCH_ASSOC)) { echo ' <option value="">'.$make["Make"].'</option> '; } ?> </select> <button href="#" class="btn btn-block car-search-button btn-lg btn-success"><span class="glyphicon car-search-g glyphicon-search"></span> Search cars </button> </form> As you can see this block is using a while loop to display all of the 'Make's' in the 'Make' column and uses a DISTINCT clause so that it doesn't show identical options. Here is the block that lists the results to the page: <?php while($row = $results->fetch(PDO::FETCH_ASSOC)) { echo ' <div class="listing-container"> <a href="carpage.php"><h3 class="model-listing-title clearfix">'.$row["Make"].' '.$row["Model"].' '.$row["Variant"].'</h3></a> <h3 class="price-listing">£'.number_format($row['Price']).'</h3> </div> <div class="listing-container-spec"> <img src="'.(explode(',', $row["PictureRefs"])[0]).'" class="stock-img-finder"/> <div class="ul-listing-container"> <ul class="overwrite-btstrp-ul"> <li class="diesel-svg list-svg">'.$row["FuelType"].'</li> <li class="saloon-svg list-svg">'.$row["Bodytype"].'</li> <li class="gear-svg list-svg">'.$row["Transmission"].'</li> <li class="color-svg list-svg">'.$row["Colour"].'</li> </ul> </div> <ul class="overwrite-btstrp-ul other-specs-ul h4-style"> <li>Mileage: '.number_format($row["Mileage"]).'</li> <li>Engine size: '.$row["EngineSize"].'cc</li> </ul> <button href="#" class="btn h4-style checked-btn hover-listing-btn"><span class="glyphicon glyphicon-ok"></span> History checked </button> <button href="#" class="btn h4-style more-details-btn hover-listing-btn tst-mre-btn"><span class="glyphicon glyphicon-list"></span> More details </button> <button href="#" class="btn h4-style test-drive-btn hover-listing-btn tst-mre-btn"><span class="test-drive-glyph"></span> Test drive </button> <h4 class="h4-style listing-photos-count"><span class="glyphicon glyphicon-camera"></span> 5 More photos</h4> </div> '; } ?> So down to my question... How can I filter these results displayed in the listing block using the select element, when a user selects a 'Make' from the select element I want them to be able to submit the form and return all rows in the SQL table containing the same 'Make' string and hide other rows that are false. Any ideas how I can achieve this or any easier ways? Thanks
  7. Hey guys, I am just wondering what method is used to give my PHP loop lists there own page based on the same template for each one. A perfect example of this is eBay, if you look at the listing pages they then link to the actual product pages. I am just wondering how I can do this? Here is my code setup: <?php while($row = $results->fetch(PDO::FETCH_ASSOC)) { echo ' <div class="listing-container"> <h3 class="model-listing-title clearfix">'.$row["Make"].' '.$row["Model"].' '.$row["Variant"].'</h3> <h3 class="price-listing">£'.number_format($row['Price']).'</h3> </div> <div class="listing-container-spec"> <img src="'.(explode(',', $row["PictureRefs"])[0]).'" class="stock-img-finder"/> <div class="ul-listing-container"> <ul class="overwrite-btstrp-ul"> <li class="diesel-svg list-svg">'.$row["FuelType"].'</li> <li class="saloon-svg list-svg">'.$row["Bodytype"].'</li> <li class="gear-svg list-svg">'.$row["Transmission"].'</li> <li class="color-svg list-svg">'.$row["Colour"].'</li> </ul> </div> <ul class="overwrite-btstrp-ul other-specs-ul h4-style"> <li>Mileage: '.number_format($row["Mileage"]).'</li> <li>Engine size: '.$row["EngineSize"].'cc</li> </ul> <button href="#" class="btn h4-style checked-btn hover-listing-btn"><span class="glyphicon glyphicon-ok"></span> History checked </button> <button href="#" class="btn h4-style more-details-btn hover-listing-btn tst-mre-btn"><span class="glyphicon glyphicon-list"></span> More details </button> <button href="#" class="btn h4-style test-drive-btn hover-listing-btn tst-mre-btn"><span class="test-drive-glyph"></span> Test drive </button> <h4 class="h4-style listing-photos-count"><span class="glyphicon glyphicon-camera"></span> 5 More photos</h4> </div> '; } ?> </div> So as you can see I am using PDO to fetch this data from my database and sort the results to list every row of the SQL table into the HTML template I have made and is then looped. My question is how can I give each row in my SQL table it's own page just like the eBay example I mentioned? Does anyone know of any useful tutorials to achieve this or any examples of how this is done? Thanks
  8. Hello sorry for being a bit vague, basically I am wanting to have to loops on my page just like this: while($row = $results->fetch(PDO::FETCH_ASSOC)) { echo ' <li>Mileage: '.number_format($row["Mileage"]).'</li> <li>Engine size: '.$row["EngineSize"].'cc</li> ';} ?> while($row = $results->fetch(PDO::FETCH_ASSOC)) { echo ' <li>Mileage: '.number_format($row["Mileage"]).'</li> <li>Engine size: '.$row["EngineSize"].'cc</li> ';} ?> I want the first block to loop the first 4 rows in the table, I then want the second block to to show the next 4 rows after the first so it isn't displaying the same strings as the first block. I know this might seem a little strange however I need to do this, I hate to be cheeky but could you show me an example of a counter? I have searched Goole but cant find a solution, I'm still really nooby when it comes to PHP. Thanks
  9. How can I limit the amount of time my while loop to only show the first 4 rows of my SQL table? I am using this code to loop my whole table: while($row = $results->fetch(PDO::FETCH_ASSOC)) { echo ' <li>Mileage: '.number_format($row["Mileage"]).'</li> <li>Engine size: '.$row["EngineSize"].'cc</li> ';} ?> I only want to loop through the first 4 rows of my SQL table, I then want to duplicate the same code but start at the 5th row until the 8th row of the table, how can I do this? Thanks, Nick
  10. Hey guys I am having a bit of a hard time understanding how I can fetch data from my SQL table using AJAX and jQuery, I am sorry if this isn't the right place to post this however I usually get great support here. I am trying to recreate this filter on the right of the page: http://www.autotrader.co.uk/search/used/cars/ So basically I have this code to display the whole MySQL table into a list: <?php while($row = $results->fetch(PDO::FETCH_ASSOC)) { echo ' <div class="listing-container"> <h3 class="model-listing-title clearfix">'.$row["Make"].' '.$row["Model"].' '.$row["Variant"].'</h3> <h3 class="price-listing">£'.number_format($row['Price']).'</h3> </div> <div class="listing-container-spec"> <img src="'.(explode(',', $row["PictureRefs"])[0]).'" class="stock-img-finder"/> <div class="ul-listing-container"> <ul class="overwrite-btstrp-ul"> <li class="diesel-svg list-svg">'.$row["FuelType"].'</li> <li class="saloon-svg list-svg">'.$row["Bodytype"].'</li> <li class="gear-svg list-svg">'.$row["Transmission"].'</li> <li class="color-svg list-svg">'.$row["Colour"].'</li> </ul> </div> <ul class="overwrite-btstrp-ul other-specs-ul h4-style"> <li>Mileage: '.number_format($row["Mileage"]).'</li> <li>Engine size: '.$row["EngineSize"].'cc</li> </ul> <button href="#" class="btn h4-style checked-btn hover-listing-btn"><span class="glyphicon glyphicon-ok"></span> History checked </button> <button href="#" class="btn h4-style more-details-btn hover-listing-btn tst-mre-btn"><span class="glyphicon glyphicon-list"></span> More details </button> <button href="#" class="btn h4-style test-drive-btn hover-listing-btn tst-mre-btn"><span class="test-drive-glyph"></span> Test drive </button> <h4 class="h4-style listing-photos-count"><span class="glyphicon glyphicon-camera"></span> 5 More photos</h4> </div> '; } ?> As you can see I am using a while loop to display the whole contents of the table into lists, this includes vehicle data. I also have this bit of HTML that I will be using as a form to filter the results: <form class="car-finder-container dflt-container"> <h2 class="h2-finder">Car finder</h2> <ul class="toggle-view"> <li class="li-toggle"> <h4 class="h4-finder-toggle">Make<span class="glyphicon glyphicon-plus glyph-plus-toggle"></span></h4> <div class="panel"> <select class="form-control select-box"> <option value="make-any">Make (Any)</option> <option value="two">Two</option> <option value="three">Three</option> <option value="four">Four</option> <option value="five">Five</option> </select> <select class="form-control last-select select-box"> <option value="model-any">Model (Any)</option> <option value="two">Two</option> <option value="three">Three</option> <option value="four">Four</option> <option value="five">Five</option> </select> </div> </li> <li class="li-toggle"> <h4 class="h4-finder-toggle">Body type<span class="glyphicon glyphicon-plus glyph-plus-toggle"></span></h4> <div class="panel"> <input id="four-by-four-checkbox" class="float-checkbox" type="checkbox"/> <label for="four-by-four-checkbox" class="label-checkbox">4x4</label> <input id="convertible-checkbox" class="float-checkbox" type="checkbox"/> <label for="convertible-checkbox" class="label-checkbox">Convertible</label> <input id="coupe-checkbox" class="float-checkbox" type="checkbox"/> <label for="coupe-checkbox" class="label-checkbox">Coupe</label> </div> </li> <li class="li-toggle"> <h4 class="h4-finder-toggle">Transmission<span class="glyphicon glyphicon-plus glyph-plus-toggle"></span></h4> <div class="panel"> <input id="automatic-checkbox" class="float-checkbox" type="checkbox"/> <label for="automatic-checkbox" class="label-checkbox">Automatic</label> <input id="manual-checkbox" class="float-checkbox" type="checkbox"/> <label for="manual-checkbox" class="label-checkbox">Manual</label> <input id="semi-auto-checkbox" class="float-checkbox" type="checkbox"/> <label for="semi-auto-checkbox" class="label-checkbox">Semi automatic</label> </div> </li> </ul> <button href="#" class="btn btn-block car-search-button btn-lg btn-success"><span class="glyphicon car-search-g glyphicon-search"></span> Search cars </button> <h4 class="h4-finder"><a href="#">Try our Smart Search </a><span class="glyphicon info-car-search-g glyphicon-info-sign"></span></h4> </form> I have noticed that JS and Ajax give a great user experience when it comes to filtering search results, in this HTML template I have added some select elements and check boxes and I am now really stuck. Does anybody know how I can make exceptions to what SQL results are returned to the front end or know of any tutorials? Thanks
  11. Hey guys I'm having an issue trying to get my PHP to display the first url of my exploded string and non of the others. The SQL table column contains strings like this: http://www.imgurl.com/image.jpg,http://www.imgurl.com/image2.jpg,http://www.imgurl.com/image3.jpg,http://www.imgurl.com/image4.jpg I am currently using this code to display the table column onto my front end: <img src="'.implode('"/><img src="',explode(',', $row["PictureRefs"])[0]).'"/> However I am getting a parse error due to the fact I have added [0] to the code to try and display only the first exploded part of the string. Any idea how I can only display the first exploded part of the string? Thanks
  12. I don't edit the CSV file, I've been stuck on this issue all day. Here is the CSV file if anyone can take a quick look? http://www.drivencarsales.co.uk/db-core/feed/csv/DrivenCarSales.csv It would be much appreciated,
  13. Hey guys, I'm having a nightmare at the moment. I am trying to import a CSV file into a MySQL table, some of the data is interested properly into the rows however some of the CSV row do not get imported. Here is my PHP script: <?php $databasehost = "localhost"; $databasename = "name"; $databasetable = "import"; $databaseusername="username"; $databasepassword = "password"; $fieldseparator = ","; $lineseparator = "\n"; $enclosedbyquote = '"'; $csvfile = "db-core/feed/csv/test.csv"; if(!file_exists($csvfile)) { die("File not found. Make sure you specified the correct path."); } try { $pdo = new PDO("mysql:host=$databasehost;dbname=$databasename", $databaseusername, $databasepassword, array( PDO::MYSQL_ATTR_LOCAL_INFILE => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ) ); } catch (PDOException $e) { die("database connection failed: ".$e->getMessage()); } $pdo->exec("TRUNCATE TABLE `$databasetable`"); $affectedRows = $pdo->exec(" LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." REPLACE INTO TABLE `$databasetable` FIELDS OPTIONALLY ENCLOSED BY ".$pdo->quote($enclosedbyquote)." TERMINATED BY ".$pdo->quote($fieldseparator)." LINES TERMINATED BY ".$pdo->quote($lineseparator)." IGNORE 1 LINES"); echo "Loaded a total of $affectedRows records from this csv file.\n"; ?> As you can see the script is importing the CSV into the table however only 40 of the rows are added to the SQL table when there are actually 80 rows in the CSV. I am also telling the script to IGNORE 1 LINES if that has anything to do with the issue. Here is one of the rows from the CSV file: "AETA83919","AETV34458584","SD57VUP","BLACK","Diesel","2007","76778","Hatchback","3","VAUXHALL","CORSA","1.3 CDTi [90] SXi 3dr [AC]","1200","3989","","Manual","http://images.autoexposure.co.uk/AETA83919/AETV34458584_1b.jpg","0","3","","0","2007 57 reg VAUXHALL CORSA 1.3 CDTi [90] SXi 3dr [AC], Manual, Hatchback, Diesel, 76,778 miles. BLACK, Comprehensive Warranty Included In Price. HPI Checked! Pre Delivery Health Check!, 5 seats, LOVELY VAUXHALL CORSA SXI 1.3CDTI SXI WITH AIR CONDITIONING!! VERY WELL CARED FOR AND MAINTAINED AND JUST SERVICED BY DRIVEN CAR SALES LTD AT 76K!! GREAT COMBINATION OF PERFORMANCE AND ECONOMY!! VIEWING AND TEST DRIVING IS RECOMMENDED!! EACH CAR COMES WITH A MININIUM 9 MONTHS MOT. FULL MECHANICAL HEALTH CHECK GUARANTEES CAR IS PREPARED TO THE HIGHEST STANDARD. COMPREHENSIVE RAC WARRANTY INCLUDED. DRIVEN CAR SALES WELCOME ANY INSPECTION. BUY WITH CONFIDENCE AND REASSURANCE AS DRIVEN CAR SALES LTD ARE OFFICIAL MEMBERS OF RETAIL MOTOR INDUSTRY FEDERATION. VIEW OUR REVIEWS ON TRUSTPILOT. 3,989","","0","1","","UK","0","","0","0","","","","" As far as I can see everything looks fine with the row, all of the rows in the CSV end like this. One thing I have noticed is it looks like the rows that are missing are in fact being placed in the last column of every other row here is an example: So as you can see it looks like one row is being inserted into the last column of some rows and I am so confused to why this might be happening? Any ideas?
  14. Hey guys, I want to be able to filter PDO and MySQL queries that are returned to my front end using JavaScript or a plus would be jQuery. First I will explain what I am actually doing... I am developing an ecommerce styled listing page, all of the data in my database is displayed into the front end and users can view vehicles that are in that table. I currently have all vehicles in a list using PDO just like this example: http://vans.autotrader.co.uk/search? As you can see the filtering module on the left of that example uses JavaScript to display the options available. My question is how is the JS having an affect on what the PHP is returning from the database there must be some sort of connection between JS and PHP but I am struggling to understand how this is possible?
  15. I am actually listing vehicles however I said brands to try and create a scenario that most PHP devs would be familiar with sorry about that, it's for my family's dealership so manufacturers and models are constantly in stock and out of stock. So by the looks of things it seems I will need a table for models as well which I could see being an absolute nightmare because there are 100's of models of cars ha. Would you say the best bet is to create a table that contains all of the manufacturers and then create table's named after each manufacturer and include all of the model's that the manufacturer supplies? Thanks, Nick.
  16. Thanks for the reply! I am a front end developer so most of this is new to me so I am sorry if I fail to understand. So let's say I create a table with all of the manufacturers that I could possibly have in the store at one time, how would I stop all of these manufacturers from displaying if they are not in stock? Obviously I don't want manufacturers displaying as an option in the select element if I don't have them in stock? Thanks, Nick.
  17. Hey guys I'm really confused at the minute, I have products in my database (It's MySQL) I want to display attributes from the products in a filtering form. I currently have select elements in my template and I want to display all current manufacturers that are listed in the manufacturers column of my database. How can I display the manufacturers once in the select element without displaying them multiple times and in alphabetical order? For example I might have 4 products that are Sony but I don't want the brand to display 4 times. Any resources or examples on how to do this? Thanks
  18. How can I make my PHP script non accessible to users? I am wanting to cron the script and don't want anybody to spam the script as it would cause performance issues if it was constantly spammed by a user. Can I put the PHP file anywhere that only the server can run it? Thanks any info would be awesome.
  19. Okay so that should work shouldn't it? The CSV value's are enclosed by a quotation marks?
  20. Right so I have tried adding this: <?php $databasehost = "localhost"; $databasename = "dealerby_vehimport"; $databasetable = "import"; $databaseusername="dealerby_veh"; $databasepassword = "Millston31"; $fieldseparator = ","; $lineseparator = "\n"; $enclosedbyquote = '"'; $csvfile = "test.csv"; if(!file_exists($csvfile)) { die("File not found. Make sure you specified the correct path."); } try { $pdo = new PDO("mysql:host=$databasehost;dbname=$databasename", $databaseusername, $databasepassword, array( PDO::MYSQL_ATTR_LOCAL_INFILE => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ) ); } catch (PDOException $e) { die("database connection failed: ".$e->getMessage()); } $pdo->exec("TRUNCATE TABLE `$databasetable`"); $affectedRows = $pdo->exec(" LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." REPLACE INTO TABLE `$databasetable` ENCLOSED BY ".$pdo->quote($enclosedbyquote)." FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)." LINES TERMINATED BY ".$pdo->quote($lineseparator)." IGNORE 1 LINES"); echo "Loaded a total of $affectedRows records from this csv file.\n"; ?> I am now getting an error when the script is ran, I have added: $enclosedbyquote = '"'; And ENCLOSED BY ".$pdo->quote($enclosedbyquote)." However this results in an error any idea why? Thanks
  21. So far I have: $affectedRows = $pdo->exec("LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." REPLACE INTO TABLE `$databasetable` ENCLOSED BY ".$pdo->quote($fieldseparator)." FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)." LINES TERMINATED BY ".$pdo->quote($lineseparator)." IGNORE 1 LINES"); Does the ".$pdo->quote($fieldseparator)." part of the script need changing? I'm guessing it does but what would insert? Sorry I am still very nooby.
  22. Hey guys! So I have this script to import a CSV file into my database: <?php $databasehost = "localhost"; $databasename = "import"; $databasetable = "import"; $databaseusername="username"; $databasepassword = "password"; $fieldseparator = ","; $lineseparator = "\n"; $csvfile = "test.csv"; if(!file_exists($csvfile)) { die("File not found. Make sure you specified the correct path."); } try { $pdo = new PDO("mysql:host=$databasehost;dbname=$databasename", $databaseusername, $databasepassword, array( PDO::MYSQL_ATTR_LOCAL_INFILE => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ) ); } catch (PDOException $e) { die("database connection failed: ".$e->getMessage()); } $pdo->exec("TRUNCATE TABLE `$databasetable`"); $affectedRows = $pdo->exec(" LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." REPLACE INTO TABLE `$databasetable` FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)." LINES TERMINATED BY ".$pdo->quote($lineseparator)." IGNORE 1 LINES"); echo "Loaded a total of $affectedRows records from this csv file.\n"; ?> So that script basically imports a csv replacing all of the other data. The issue I am having is that the data inside one of the columns contains commas, they are wrapped in quotations. Here is an example: ,"1 Registered Keeper, Full Service History, Central Locking, Electric Windows, Electric Mirrors, ABS, Traction Control, Climate Control, Power Steering, Drivers Airbag, Passenger Airbag, Side Airbags, Cruise Control, Alarm, Immobiliser, Half Leather Interior, Alloy Wheels", How could I make the script determine the text within those quotation marks as a whole value instead of separating them into individual columns? Thanks any help on this would be great!
  23. Thanks that is working great! I didn't know if I needed to specify the table seen as it is specified at the start of the script, can I be really cheeky and ask how I would go by skipping the first row? Would this work? $pdo->exec("IGNORE 1 LINES `$databasetable`");
  24. Hi I have tried adding this: <?php $databasehost = "localhost"; $databasename = "import"; $databasetable = "import"; $databaseusername="import"; $databasepassword = "password"; $fieldseparator = ","; $lineseparator = "\n"; $csvfile = "test.csv"; if(!file_exists($csvfile)) { die("File not found. Make sure you specified the correct path."); } try { $pdo = new PDO("mysql:host=$databasehost;dbname=$databasename", $databaseusername, $databasepassword, array( PDO::MYSQL_ATTR_LOCAL_INFILE => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ) ); } catch (PDOException $e) { die("database connection failed: ".$e->getMessage()); } $pdo->exec("TRUNCATE TABLE;"); $affectedRows = $pdo->exec(" LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." REPLACE INTO TABLE `$databasetable` FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)." LINES TERMINATED BY ".$pdo->quote($lineseparator)); echo "Loaded a total of $affectedRows records from this csv file.\n"; ?> As you can see I have tried adding $pdo->exec("TRUNCATE TABLE;"); Which I am guessing is completely wrong as I am getting an error ha, I am still new to PHP. Any idea how what I am doing wrong or how many things I am doing wrong?
  25. Hey guys, so I have this script: <?php $databasehost = "localhost"; $databasename = "import"; $databasetable = "import"; $databaseusername="import"; $databasepassword = "password"; $fieldseparator = ","; $lineseparator = "\n"; $csvfile = "test.csv"; if(!file_exists($csvfile)) { die("File not found. Make sure you specified the correct path."); } try { $pdo = new PDO("mysql:host=$databasehost;dbname=$databasename", $databaseusername, $databasepassword, array( PDO::MYSQL_ATTR_LOCAL_INFILE => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ) ); } catch (PDOException $e) { die("database connection failed: ".$e->getMessage()); } $affectedRows = $pdo->exec(" LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." REPLACE INTO TABLE `$databasetable` FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)." LINES TERMINATED BY ".$pdo->quote($lineseparator)); echo "Loaded a total of $affectedRows records from this csv file.\n"; ?> I need this script to replace all data in the MYSQL table with the data in the CSV file. Even known it has REPLACE INTO TABLE running it doesn't actually replace all of the data it just adds new rows to the table. The obvious option is to run a TRUNCATE at the start of the script to wipe all of the data in the table and then replace it with the CSV the problem I am having is successfully implementing it into the script, any idea how this can be done I have been surfing the net for a while and cant find anything on this topic?
×
×
  • 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.