gingerfish Posted March 19, 2015 Report Share Posted March 19, 2015 Labdien! Doma ir uztaisit vairakus dropdow, kas savienojas un saglabā datus iekš mysql. Viss strādā, tikai ir viena problēma, kad atsāju vienu teiblu, tad viss ir kārtībā, taču kad pievienoju jaunu, tā iekš dropdown dati atkārtojās. mysql_connect("localhost","root",""); mysql_select_db("test"); $sql = "SELECT * FROM o_make, o_location, o_btype"; //man liekas, ka errors ir sajaa rindinja $query = mysql_query($sql); while ( $results[] = mysql_fetch_object ( $query ) ); array_pop ( $results ); Pilns kods: P.s zinu, ka kodā ir bardaks, taču šobrīd nesaprašana par datubāzi. <?php session_start(); include_once('../includes/connection.php'); if (isset($_SESSION['logged_in'])) { if (isset($_POST['title'], $_POST['content'], $_POST['location'], $_POST['make'], $_POST['model'], $_POST['year'], $_POST['odometer'], $_POST['transmission'], $_POST['fuel'], $_POST['color'], $_POST['image'])) { $title = $_POST['title']; $content = nl2br($_POST['content']); $location = $_POST['location']; $make = $_POST['make']; $model = $_POST['model']; $btype = $_POST['btype']; $year = $_POST['year']; $odometer = $_POST['odometer']; $transmission = $_POST['transmission']; $fuel = $_POST['fuel']; $color = $_POST['color']; $image = $_POST['image']; if (empty($title) or empty($content)) { $error = 'All fields are required'; } else { $query = $pdo->prepare('INSERT INTO articles (article_title, article_content, article_timestamp, article_location, article_make, article_model, article_btype, article_year, article_odometer, article_transmission, article_fuel, article_color, article_image) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'); $query->bindValue(1, $title); $query->bindValue(2, $content); $query->bindValue(3, time()); $query->bindValue(4, $location); $query->bindValue(5, $make); $query->bindValue(6, $model); $query->bindValue(7, $btype); $query->bindValue(8, $year); $query->bindValue(9, $odometer); $query->bindValue(10, $transmission); $query->bindValue(11, $fuel); $query->bindValue(12, $color); $query->bindValue(13, $image); $query->execute(); header('Location: index.php'); } } ?> <html> <head> <title>ADS tutorial</title> <link rel="stylesheet" href="../assets/style.css" /> </head> <body> <div class="container"> <a href="index.php" id="logo">ADS</a> <br /> <h4>Add Article</h4> <?php if (isset($error)) { ?> <small style="color:#aa0000;"><?php echo $error; ?></small> <br /><br /> <?php } ?> <form action="add.php" method="post" autocomplete="off"> <input type="file" name="image" /><br /><br /> <input type="text" name="title" placeholder="Title" /><br /><br /> <select name="location"> <?php foreach ( $results as $option ) : ?> <option value="<?php echo $option->location; ?>"><?php echo $option->location; ?></option> <?php endforeach; ?> </select> <br /><br /> <select name="make"> <?php foreach ( $results as $option ) : ?> <option value="<?php echo $option->make; ?>"><?php echo $option->make; ?></option> <?php endforeach; ?> </select> <br /><br /> <input type="text" name="model" placeholder="Model" /><br /><br /> <select name="btype"> <?php foreach ( $results as $option ) : ?> <option value="<?php echo $option->btype; ?>"><?php echo $option->btype; ?></option> <?php endforeach; ?> </select> <br /><br /> <input type="text" name="year" placeholder="Year" /><br /><br /> <input type="text" name="odometer" placeholder="Odometer" /><br /><br /> <input type="text" name="transmission" placeholder="Transmission" /><br /><br /> <input type="text" name="fuel" placeholder="Fuel Type" /><br /><br /> <input type="text" name="color" placeholder="Type a color" /><br /><br /> <textarea rows="15" cols="40" placeholder="Content" name="content"></textarea><br /><br /> <input type="submit" value="Add Article" /> </form> </div> </body> </html> <?php } else { header('Location: index.php'); } ?> paldies jau iepriekš. Quote Link to comment Share on other sites More sharing options...
Sasa Posted March 19, 2015 Report Share Posted March 19, 2015 http://www.w3schools.com/sql/sql_distinct.asp Quote Link to comment Share on other sites More sharing options...
zeCode Posted March 19, 2015 Report Share Posted March 19, 2015 man liekas errors ir šaja rindiņā <?php foreach ( $results as $option ) : ?> saprastu ja butu $results[0] .. $results[1] ... $results[2] Quote Link to comment Share on other sites More sharing options...
waplet Posted March 19, 2015 Report Share Posted March 19, 2015 man liekas errors ir šaja rindiņā <?php foreach ( $results as $option ) : ?> saprastu ja butu $results[0] .. $results[1] ... $results[2] DIemžēl kļūdies. Foreach iterē katrai masīva vērtībai cauri un to piesķirts $option mainīgajā. Quote Link to comment Share on other sites More sharing options...
php newbie Posted March 19, 2015 Report Share Posted March 19, 2015 (edited) $sql = "SELECT * FROM o_make union SELECT * FROM o_location union SELECT * FROM o_btype"; Vispār taisi 3 atsevišķus selektus Edited March 19, 2015 by php newbie Quote Link to comment Share on other sites More sharing options...
gingerfish Posted March 19, 2015 Author Report Share Posted March 19, 2015 $sql = "SELECT * FROM o_make union SELECT * FROM o_location union SELECT * FROM o_btype"; Vispār taisi 3 atsevišķus selektus nostrādāja, tikai ir viena problēma- Viss salikās vienā dropdown Quote Link to comment Share on other sites More sharing options...
ieleja Posted March 19, 2015 Report Share Posted March 19, 2015 kā iesācējs iesācējam tomēr ieteiktu saprast ko dari, nevis darboties ar zinātnisko metodi "baksti-baksti-kamēr sabaksti kaut ko it kā pareizu" Quote Link to comment Share on other sites More sharing options...
zeCode Posted March 20, 2015 Report Share Posted March 20, 2015 DIemžēl kļūdies. Foreach iterē katrai masīva vērtībai cauri un to piesķirts $option mainīgajā. Šaja situācija es vairāk runāju kāpēc rodas dublikāti ja ir selekts, kas selekte visu 3 tabulu ierakstus, tas nozīme ka - rezultāts saturēs visu ierakstu kombinācijas ... teiksim ja tur ierakstu skaits ir 2,2,4 tas nozīme 2x2x4 = 16 un jo vairāk situācija sliktāka. Kaut gan teiksim vajag tikai 2 ierakstus (2x ieraksti kas atkārtojas 8 reizes katrs). Protams to var labot ar SELECT * -> DISTINCT location, DISTINCT make, DISTINCT btype Tas ir sviestains risinājums. Lai veido 3 dažādus sql katrai tabulai un saglabā 3 dažādos mainīgos un pēc tam iet ciklā. OK $results[0] .. $results[1] ... $results[2] tas nav korekti sapratu uzreiz kads pats ieteicu - bet teiksim $result_location, $result_make, result_btype Quote Link to comment Share on other sites More sharing options...
gingerfish Posted March 20, 2015 Author Report Share Posted March 20, 2015 (edited) Izskatās, ka īsti korekts un pareizs šis risinājums nav, taču nostrādāja <?php $sql = "SELECT make FROM o_make"; $result = mysql_query($sql); echo "<select name='make'>"; while ($row = mysql_fetch_array($result)) { echo "<option value='" . $row['make'] ."'>" . $row['make'] ."</option>"; } echo "</select>"; ?> Edited March 20, 2015 by gingerfish Quote Link to comment Share on other sites More sharing options...
jurchiks Posted March 21, 2015 Report Share Posted March 21, 2015 Are you kidding me? Quote Link to comment Share on other sites More sharing options...
gingerfish Posted March 21, 2015 Author Report Share Posted March 21, 2015 Are you kidding me? Varbūt labāk varēji ielikt kādu īsu paskaidrojumu, kādēļ tā nav pareizi. Man jau šķiet, ka tādēļ jau šis forums ir domāts, lai padalītos ar ieteikumiem. Quote Link to comment Share on other sites More sharing options...
jurchiks Posted March 21, 2015 Report Share Posted March 21, 2015 (edited) Tu gribi izvadīt 3 dropdownus - location, make, btype -, bet tā vietā, lai uzreiz kverijotu katra dropdowna datus atsevišķi (jo tā ir loģiski un vienkārši), tu mēģini kaut kāda velna pēc kverijot visus 3 NESAISTĪTOS dropdownus vienā kverijā? Kāpēc lieki tīšām sarežģīt sev dzīvi? echo '<select name="make">', '<option value="0">---</option>'; $makeStmt = $pdo->query('SELECT id, make FROM o_make ORDER BY make ASC'); foreach ($makeStmt as $make) { echo '<option value="', $make['id'], '">', htmlspecialchars($make['make']), '</option>'; } echo '</select>'; Same for the other two dropdowns. Also: http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059 Nekādu distinct nevajag, tā vietā tev vajag pārbaudīt pie inserta, vai tāds pats make jau eksistē datubāzē, un ja eksistē, tad neļaut insertot. Kā arī kā option value obligāti izmanto ID, nevis tekstu. Edited March 21, 2015 by jurchiks Quote Link to comment Share on other sites More sharing options...
gingerfish Posted March 21, 2015 Author Report Share Posted March 21, 2015 Tu gribi izvadīt 3 dropdownus - location, make, btype -, bet tā vietā, lai uzreiz kverijotu katra dropdowna datus atsevišķi (jo tā ir loģiski un vienkārši), tu mēģini kaut kāda velna pēc kverijot visus 3 NESAISTĪTOS dropdownus vienā kverijā? Kāpēc lieki tīšām sarežģīt sev dzīvi? echo '<select name="make">', '<option value="0">---</option>'; $makeStmt = $pdo->query('SELECT id, make FROM o_make ORDER BY make ASC'); foreach ($makeStmt as $make) { echo '<option value="', $make['id'], '">', htmlspecialchars($make['make']), '</option>'; } echo '</select>'; Same for the other two dropdowns. Also: http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059 Nekādu distinct nevajag, tā vietā tev vajag pārbaudīt pie inserta, vai tāds pats make jau eksistē datubāzē, un ja eksistē, tad neļaut insertot. Kā arī kā option value obligāti izmanto ID, nevis tekstu. Paldies par atbildi! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.