bubu Posted May 17, 2010 Report Share Posted May 17, 2010 Tas tik, ja tiks lietots MySQL. Bet normālajai INSERT sintaksei arī var daaaudz vienkāršāk veidot kveriju: $q = array("username" => "'$uname'", "password" => "'$pass'", ...); // eskeipošanu var iznest ārpusē, vai darīt vēlāk $query = "INSERT INTO users (" . implode(",", array_keys($q)) . ") VALUES (" . implode(",", array_values($q)) . ")"; Quote Link to comment Share on other sites More sharing options...
daGrevis Posted May 17, 2010 Author Report Share Posted May 17, 2010 Bubu, viņam kas nepatīk, nevar saprast kas... =( $SQL = array( 'username' => $username, 'password' => sha1($password), 'mail' => $mail, 'time' => time(), 'gender' => ($gender ? $gender : ''), 'country' => ($country ? $country : '') ); mysql_query(' INSERT INTO `users` (' . implode(',', array_keys($SQL)) . ') VALUES (' . implode(',', array_values($SQL)) . ') ') or die(mysql_error()); You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gmail.com,1274119340,male,lv)' at line 2 Quote Link to comment Share on other sites More sharing options...
briedis Posted May 17, 2010 Report Share Posted May 17, 2010 Bubu, viņam kas nepatīk, nevar saprast kas... =( $SQL = array( 'username' => $username, 'password' => sha1($password), 'mail' => $mail, 'time' => time(), 'gender' => ($gender ? $gender : ''), 'country' => ($country ? $country : '') ); mysql_query(' INSERT INTO `users` (' . implode(',', array_keys($SQL)) . ') VALUES (' . implode(',', array_values($SQL)) . ') ') or die(mysql_error()); Nez, ievietoajmās vērtības ir pēdiņās? Quote Link to comment Share on other sites More sharing options...
daGrevis Posted May 17, 2010 Author Report Share Posted May 17, 2010 Hmmm Es vispār uztvēru pareizi domu? $SQL = array( '`username`' => $username, '`password`' => sha1($password), '`mail`' => $mail, '`time`' => time(), '`gender`' => ($gender ? $gender : ''), '`country`' => ($country ? $country : '') ); mysql_query(' INSERT INTO `users` (' . implode(',', array_keys($SQL)) . ') VALUES (' . implode(',', array_values($SQL)) . ') ') or die(mysql_error()); Neiet. =( Quote Link to comment Share on other sites More sharing options...
bubu Posted May 17, 2010 Report Share Posted May 17, 2010 Vērtības, nevis atslēgas! Un pēdiņās (" vai ') nevis backtikos (`) Quote Link to comment Share on other sites More sharing options...
daGrevis Posted May 17, 2010 Author Report Share Posted May 17, 2010 (edited) E?? $SQL = array( 'username' => "$username", 'password' => "sha1($password)", 'mail' => "$mail", 'time' => "time()", 'gender' => "($gender ? $gender : '')", 'country' => "($country ? $country : '')" ); mysql_query(' INSERT INTO `users` (' . implode(',', array_keys($SQL)) . ') VALUES (' . implode(',', array_values($SQL)) . ') ') or die(mysql_error()); Nekā. =D Bet... Tā nu patiešām nav. Pārbaudīju, ja ir tas iepriekšējais variants, ar funkciju print_r(), dati masīvā ir ok! =) Problēma ir kur mysql_query(). Tik kur... Tagad ir tā http://paste.php.lv/5fe77d9c6e3cdc441d6d021b947ed5c8?lang=php . Mle... :> Edited May 17, 2010 by daGrevis Quote Link to comment Share on other sites More sharing options...
briedis Posted May 17, 2010 Report Share Posted May 17, 2010 tak izvadi uz ekrāna to rezultējošo kvēriju (kas tiek uzģenerēts ar tiem implodiem, kverīja TEKSTU, nevis rezultātu), un pameklē vai kaut kur netrūkst pēdiņas... Quote Link to comment Share on other sites More sharing options...
Val Posted May 17, 2010 Report Share Posted May 17, 2010 kautkā tā. nākamreiz izlasi, ko tev mēģina paskaidrot, nevis krāmē pēdiņas pa labi un pa kreisi. echo $q = sprintf("INSERT INTO `users` (%s) VALUES ('%s')", implode(', ', array_map('mysql_escape_string', array_keys($SQL))), implode("', '",array_map('mysql_escape_string', $SQL))); Quote Link to comment Share on other sites More sharing options...
daGrevis Posted May 19, 2010 Author Report Share Posted May 19, 2010 Pag, briedis. Es sapratu! =D Bet ļoti stulbi izskatās darīt šādi, vai ne?? $SQL = array( '`username`' => '\'' . $username . '\'', '`password`' => '\'' . sha1($password) . '\'', '`mail`' => '\'' . $mail . '\'', '`time`' => '\'' . time() . '\'', '`gender`' => '\'' . ($gender ? $gender : '') . '\'', '`country`' => '\'' . ($country ? $country : '') . '\'' ); Tad nu es gribēju automātiski pievienot ', bet kaut kas neiet... Mle :> forEach(array_values($SQL) as $x) { $x = '\'' . $x . '\''; } Quote Link to comment Share on other sites More sharing options...
briedis Posted May 19, 2010 Report Share Posted May 19, 2010 (edited) Pag, briedis. Es sapratu! =D Bet ļoti stulbi izskatās darīt šādi, vai ne?? Tad nu es gribēju automātiski pievienot [b]'[/b], bet kaut kas neiet... Mle :> [code]forEach(array_values($SQL) as $x) { $x = '\'' . $x . '\''; } Nu jā, $x ta tu modificē, bet x šinī gadījumā nav atkarīgs no masīva, tā ir vienkārši kārtējā vērtība izvilkta no masīva un ielikta mainīgajā $x. Variants pie katras iterācija saglabāt atpakaļ masīvā foreach($sql as $key => $value){ $value= ...; //modificējam $sql[$key] = $value; //saglabājam atpakaļ masīvā } vai arī masīva elementu ņemt kā referenci/norādi foreach($sql as &$value){ $value = "modificējam"; //modoficējam uzreiz masīvā, jo mainīgais ir kā norāde/reference } Edited May 19, 2010 by briedis Quote Link to comment Share on other sites More sharing options...
rATRIJS Posted May 19, 2010 Report Share Posted May 19, 2010 (edited) Labaak IMO izskataas shaadi: foreach($items as &$item) { $item = "'$item'"; } Nekaa ciiniiities ar eskeiposhanu un single quotes. Un vispaar (es jau kuro reizi saku) - parametrizeetie kveeriji FTW. Vai vismaz shaadi: $q = "SELECT stuff FROM the_stuff WHERE stuffing = '%s'"; $q = sprintf($q, 'the-stuffing-thing'); // SELECT stuff FROM the_stuff WHERE stuffing = 'the-stuffing-thing' Edited May 19, 2010 by rATRIJS 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.