daGrevis Posted May 16, 2010 Report Share Posted May 16, 2010 Sveiki... =) Problēma! Nepieciešams, lai uz datubāzi tiek nodotas attiecīgās vērtības tikai tad, ja tās ir "set'otas". Tad nu isSet(), bet lūk, PHP kas nepatīk manai sintaksei. =D īstenībā, tas kā uz ātro, pašam nepaīk - ļoti nepārskatāmi... =( mysql_query(' INSERT INTO `users` (`username`, `password`, `mail`, `time`"' if(isSet($_POST['gender'])) { '" , `gender` "' } if(isSet($_POST['country'])) { '" , `country` "' } '") VALUES ("' . mysql_real_escape_string($_POST['username']) . '", "' . mysql_real_escape_string(sha1($_POST['password'])) . '", "' . mysql_real_escape_string($_POST['mail']) . '", "' . time() . '", "' . mysql_real_escape_string($_POST['gender']) . '", "' . mysql_real_escape_string($_POST['country']) . '") ') or die(mysql_error()); P.S. Par otro rindiņu, t.i. sākot no "VALUES...", nemaz neskatieties - tur nesāku likt savus isSet(), jo kļūdu jau met iepriekšējā rindiņā. Parse error: syntax error, unexpected T_IF in C:\xampp\htdocs\bliss\do__register.php on line 83 Pats zinu, ka galīgi "freak'aini" izskatās. =D Kā būtu pareizi? =) Quote Link to comment Share on other sites More sharing options...
briedis Posted May 16, 2010 Report Share Posted May 16, 2010 Nafig tu bāz tos ifus kvērijā? tā tak nevar darīt! Iznes ifus ārā, saliec vajadzīgās vērtības mainīgajos, un mainīgos liec kvērijā. ..citādi kaut kāds murgs... Quote Link to comment Share on other sites More sharing options...
Code Posted May 16, 2010 Report Share Posted May 16, 2010 Izskatās tiešām drausmīgi. :D Cik zinu, tad IFoties, veidojot stringu, nemaz nevar - domāju, ka par to arī viņš lamājas. Ieteiktu veidot savādāk: Piemēram, saveido no sākuma divus mainīgos $fields un $values atkarībā no tā, kādu dati ir jāinserto, pēc tam uzbūvē to vaicājumu. mysql_query("INSERT INTO 'users' ($fields) VALUES ($values)"); Quote Link to comment Share on other sites More sharing options...
briedis Posted May 16, 2010 Report Share Posted May 16, 2010 Nevajag jaukt parastos ifus ar ternary ifiem: $a = true; $str = "A vertībā ir " . ($a ? "patiesa" : "nepatiesa") . "!"; Quote Link to comment Share on other sites More sharing options...
daGrevis Posted May 16, 2010 Author Report Share Posted May 16, 2010 Pag, bet nu jā, iznest jau tos IF'us nav problēma, bet, ja mainīgais nav "set'ots", tad nevar jau palikt tā... (ja $_POST['gender']) INSERT INTO `users` (`username`, `password`, `mail`, `time`, `gender`, `country`) VALUES ("' . mysql_real_escape_string($_POST['username']) . '", "' . mysql_real_escape_string(sha1($_POST['password'])) . '", "' . mysql_real_escape_string($_POST['mail']) . '", "' . time() . '", "' . mysql_real_escape_string($_POST['gender']) . '", "' . mysql_real_escape_string($_POST['country']) . '") Quote Link to comment Share on other sites More sharing options...
daGrevis Posted May 16, 2010 Author Report Share Posted May 16, 2010 Vienīgais, varbūt... Ne? if(!isSet($_POST['gender'])) { $_POST['gender'] = NULL; } Quote Link to comment Share on other sites More sharing options...
ohmygod Posted May 16, 2010 Report Share Posted May 16, 2010 $q="blaa blaa blaa ".c?('gender'):('')." blaa blaa blaa"; un viss darbosies bet tas ir nepārskatāmi. $vars = array('gender','mail','country'); $str1=''; $str2=''; foreach($vars as $var){ if(isSet($_POST[$var])){$str1.='gender';$str2="'.$_POST[$var].'";} } $q="INSERT INTO tabula (".join(', ',$str1),') VALUES ('.join(', ',$str2).');'; Quote Link to comment Share on other sites More sharing options...
briedis Posted May 16, 2010 Report Share Posted May 16, 2010 Kāpēc tad tu nevaru post mainīgos pārrakstīt parastajos mainīgajos? Quote Link to comment Share on other sites More sharing options...
daGrevis Posted May 16, 2010 Author Report Share Posted May 16, 2010 Ko tas dos? Tikai lieks resursa tēriņš. Šķiet... Quote Link to comment Share on other sites More sharing options...
briedis Posted May 16, 2010 Report Share Posted May 16, 2010 (edited) Ko tas dos? Tikai lieks resursa tēriņš. Šķiet... Resursu tēriņš :D :D :D <?php $name = mysql_real_escape_string($_POST['username']); $pass = mysql_real_escape_string(sha1($_POST['password'])); $mail = mysql_real_escape_string($_POST['mail']); $gender = isset($_POST['gender']) ? mysql_real_escape_string($_POST['gender']) : false; $country = isset($_POST['country']) ? mysql_real_escape_string($_POST['country']) : false; $query = " INSERT INTO users( username, password, mail, time" . ($gender ? ", gender" : "") . ($country ? ", country" : "") . " VALUES ( '$uname', '$pass', '$mail'," . time() . ($gender ? " , '$gender'" : "") . ($country ? " , '$country'" : ""); if(mysql_query($query)){ echo "Veiksmīgi pievienots!"; }else{ echo "Error: " . mysql_error(); } ?> Edited May 16, 2010 by briedis Quote Link to comment Share on other sites More sharing options...
daGrevis Posted May 16, 2010 Author Report Share Posted May 16, 2010 Cepums Tev, atkal! =) Quote Link to comment Share on other sites More sharing options...
briedis Posted May 16, 2010 Report Share Posted May 16, 2010 Cepums Tev, atkal! =) Nevajag vienkāršas lietas sarežģīt. Nevajag arī čupu ar dažādām darbībām samačkāt vienā blāķī. Raksti tā, lai ir viegli uztverams, uzreiz saprotams, kas tur īsti notiek. Nav tā, ka jo īsāks kods, jo labāks.... Quote Link to comment Share on other sites More sharing options...
daGrevis Posted May 16, 2010 Author Report Share Posted May 16, 2010 Ņemšu padomu vērā! =) Quote Link to comment Share on other sites More sharing options...
rATRIJS Posted May 16, 2010 Report Share Posted May 16, 2010 btw iesaku paskatīties tādu lietu kā parametrizētie kvēriji. Quote Link to comment Share on other sites More sharing options...
Kaklz Posted May 17, 2010 Report Share Posted May 17, 2010 Ja vien ar vienu insertu netaisies ievietot n ierakstus, tad iesaku padomāt par šāda pieraksta izmantošanu: INSERT INTO tablename SET field1=value1, field2=value2; No php puses būvējot tas būs daaaaudz vienkāršāk. 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.