Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP][MYSQL] Nie działająca część kodu.
Foksi
post
Post #1





Grupa: Zarejestrowani
Postów: 1
Pomógł: 0
Dołączył: 18.03.2009

Ostrzeżenie: (0%)
-----


Witam. Dopiero zaczynam naukę php. To jest pierwszy skrypt który odważyłem się zmodyfikować do swoich potrzeb ale okazało się że jeżeli przed moją modyfikacjądziałał poprawnie to po ingerencji.. nic się w działaniu nie zmieniło (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

nie wiem dlaczego moja część kodu nie działa. Gdzieś musi byćbłąd i to pewnie trywialny.
to co ja zmieniłem dałem poza kodem.

sktypt miał na celu dopisywać w tabeli chat godzine postu autora i treść. I tą funkcję sprawował doskonale (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
ze swojej strony chciałem dodać sprawdzanie czy dany użytkownik jest na liście a jeżeli nie to nie pozwolić zamieścić takiego postu.
stworzyłem więc tabelę "lista" i kolumnę użytkownik i tam wpisałem np siebie.. ale nadal mogą pisać wszyscy. Dlaczego? Co jest źle??
Kod
<?
  
   $name =  $_POST["n"]; //name from the form in index.html
   $text =  $_POST["c"];    //comment from the form in index.html
  
   //some weird conversion of the data inputed
   $name = str_replace("\'","'",$name);
   $name = str_replace("'","\'",$name);
   $text = str_replace("\'","'",$text);
   $text = str_replace("'","\'",$text);
   $text = str_replace("---"," - - ",$text);
   $name = str_replace("---"," - - ",$name);
  
   //the message is cut of after 500 letters
   if (strlen($text) > 500) {
       $text = substr($text,0,500);
   }
  
   //to allow for linebreaks a space is inserted every 50 letters
   $text = preg_replace("/([^\s]{50})/","$1 ",$text);
  
   //the name is shortened to 30 letters
   if (strlen($name) > 30) {
       $name = substr($name, 0,30);
   }
  
  
   //only if a name and a message have been provides the information is added to the db
   if ($name != '' && $text != '') {
       addData($name,$text); //adds new data to the database
       getID(50); //some database maintenance
   }
  
  
   //establishes the connection to the database
   function getDBConnection () {
       include('db.php'); //contains the given DB setup $db, $server, $user, $pass
       $conn = mysql_connect($server, $user, $pass);
       if (!$conn) {
           // echo "Connection to DB was not possible!";
           end;
       }
       if (!mysql_select_db($db, $conn)) {
           // echo "No DB with that name seems to exist at the server!";
           end;
       }
       return $conn;
   }

//w tej samej bazie stworzyłem wcześniej tabele lista
//na początku dałem te 2 linijki poniżej
// Check if the username exists
//$usernameinuse = mysql_query("SELECT * FROM lista WHERE uzytkownik = '$name'");
// $isusernameinuse = mysql_num_rows($usernameinuse);

//ale potem zmieniłem je na taki wpis jak tutaj
$sql = "SELECT * FROM lista WHERE uzytkownik = '$name'";
$conn = getDBConnection();
$results = mysql_query($sql, $conn);


//tego if'a chyba wogule nie wykonuje tylko idzie dalej nieważne pod jakim nickiem wystąpi post
// If username not exists then exit script
$isusernameinuse = mysql_num_rows($results);
if ($isusernameinuse == 0) {
exit;
}
Kod
  
   //adds new data to the database
   function addData($name,$text) {
       $sql = "INSERT INTO chat (time,name,text) VALUES (NOW(),'".$name."','".$text."')";
       $conn = getDBConnection();
       $results = mysql_query($sql, $conn);
       if (!$results || empty($results)) {
           //echo 'There was an error creating the entry';
           end;
       }
   }
  
   //returns the id of a message at a certain position
   function getID($position) {
       $sql =     "SELECT * FROM chat ORDER BY id DESC LIMIT ".$position.",1";
       $conn = getDBConnection();
       $results = mysql_query($sql, $conn);
       if (!$results || empty($results)) {
           //echo 'There was an error creating the entry';
           end;
       }
       while ($row = mysql_fetch_array($results)) {
           $id = $row[0]; //the result is converted from the db setup (see initDB.php)
       }
       if ($id) {
           deleteEntries($id); //deletes all message prior to a certain id
       }
   }
  
  
   //deletes all message prior to a certain id
   function deleteEntries($id) {
       $sql =     "DELETE FROM chat WHERE id < ".$id;
       $conn = getDBConnection();
       $results = mysql_query($sql, $conn);
       if (!$results || empty($results)) {
           //echo 'There was an error deletig the entries';
           end;
       }
   }
   ?>


nie wiem czy to jest czytelne i przejrzyste mam nadzieję że wyjaśniłem pój problem. Przypominam że w kodzie zawarłem orginalny kod php przed zmianą a moja zmiana jest opisana pomiędzy częściami i dokładnie w tamtym miejscu ją wstawiłem. proszę o pomoc.
Go to the top of the page
+Quote Post
Pilsener
post
Post #2





Grupa: Zarejestrowani
Postów: 1 590
Pomógł: 185
Dołączył: 19.04.2006
Skąd: Gdańsk

Ostrzeżenie: (0%)
-----


1. Używaj właściwego BBcode
2. Podawaj choć fragment, gdzie może wystąpić błąd i ucz się sam szukać, zaczynaj od zapytań:
  1. <?php
  2. function addData($name,$text) {
  3.       $sql = "INSERT INTO chat (time,name,text) VALUES (NOW(),'".$name."','".$text."')";
  4.       echo $sql; //zacznij od wydrukowania zapytań i sprawdzenia, czy są poprawne
  5.   }
  6. ?>

3. Poprawność zapytań sprawdzaj programem do ich tworzenia (front, navicat)
4. ZAWSZE sprawdzaj, czy mysql_query() nie zwraca false:
  1. <?php
  2. if(!mysql_query()){echo 'zle zapytanie, ostatni blad bazy: '.mysql_error();}
  3. ?>
- najczęściej zmienne nie dochodzą do zapytania lub jest jakaś literówka.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 17:44