Jump to content
php.lv forumi

PHP


keissfootball

Recommended Posts

  • Replies 43
  • Created
  • Last Reply

Top Posters In This Topic

// Part One - Initiate a mySQL Database Connection

// Database Connectivity Variables and other Variables

$DBhost = "localhost"; // Database Server

$DBuser = "user"; // Database User

$DBpass = "password"; // Database Pass

$DBName = "db_1"; // Database Name

$table = "viesu_gramata"; // Database Table

$numComments = 10; // Number of Comments per page

 

// Connect to mySQL Server

$DBConn = mysql_connect($DBhost,$DBuser,$DBpass) or die("Error in GuestBook Application: " . mysql_error());

// Select mySQL Database

mysql_select_db($DBName, $DBConn) or die("Error in GuestBook Application: " . mysql_error());

 

// Part Two - Choose what action to perform

$action = $_GET['action'];

 

switch($action) {

case 'read' :

// Fetch all comments from database table

$sql = 'comments' . $table . 'guestbook';

$allComments = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());

$numallComments = mysql_num_rows($allComments);

// Fetch page-wise comments from database table

$sql .= 'comments' . $_GET['NumLow'] . ', ' . $numComments;

$fewComments = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());

$numfewComments = mysql_num_rows($fewComments);

// Generate Output for Flash to Read

print '&totalEntries=' . $numallComments . '&';

print "<br>&entries=";

 

if($numallComments == 0) {

print "No entries in the guestbook, as yet..";

} else {

while ($array = mysql_fetch_array($fewComments)) {

$name = mysql_result($fewComments, $i, 'name');

$email = mysql_result($fewComments, $i, 'email');

$comments = mysql_result($fewComments, $i, 'comments');

$time = mysql_result($fewComments, $i, 'time');

 

print '<b>Name: </b>' . $name . '<br><b>Email: </b>' . $email . '<br><b>Comments: </b>' . $comments . '<br><i>Date: ' . $time . '</i><br><br>';

$i++;

}

}

// Print this only when there aren't any more entries..

if($_GET['NumLow'] > $numallComments) {

print 'No More Entries!&';

}

break;

 

case 'write' :

// Recieve Variables From Flash

$name = ereg_replace("&", "%26", $_POST['yourname']);

$email = ereg_replace("&", "%26", $_POST['youremail']);

$comments = ereg_replace("&", "%26", $_POST['yourcomments']);

$submit = $_POST['submit'];

 

// Current system date in yyyy-mm-dd format

$submitted_on = date ("Y-m-d H:i:s",time());

 

// Check if its submitted from Flash

if($submit == 'Yes'){

// Insert the data into the mysql table

$sql = 'INSERT INTO ' . $table . 'guestbook';

' (`ID`,

`name`,

`email`,

`comments`,

`time`

)

VALUES

(\'\','

. '\'' . $name . '\','

. '\'' . $email . '\','

. '\'' . $comments . '\','

. '\'' . $submitted_on . '\'

)';

$insert = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());

 

 

}

?>

 

 

Varbūt būs kaut kas saprotams, ja es visu skriptu ielikšu? Tā rindiņa - pie beigām ir tā pati, ko rakstīju agrāk!!!

Link to comment
Share on other sites

Šito penteri:

$sql = 'INSERT INTO ' . $table . 'guestbook';
' (`ID`,
`name`,
`email`,
`comments`,
`time`
)
VALUES
(\'\','
. '\'' . $name . '\','
. '\'' . $email . '\','
. '\'' . $comments . '\','
. '\'' . $submitted_on . '\'
)';

aizstāj ar šo:

$sql = "INSERT INTO guestbook ".
 "(`ID`, `name`, `email`, `comments`, `time`) VALUES ".
 "('','$name', '$email', '$comments', '$submitted_on')";

(nav īsti skaidrs kurā tabulā vajag likt datus. Vai nu guestbook, vai $table. Ja pēdējā, tad aizstāj to guestbook ar tekstu $table)

Link to comment
Share on other sites

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<?

// Part One - Initiate a mySQL Database Connection
// Database Connectivity Variables and other Variables
   $DBhost = "mans_hosts";   // Database Server
   $DBuser = "mans_lietotajvards";            // Database User
   $DBpass = "mana_parole";            // Database Pass
   $DBName = "mans_datubazes_vards";            // Database Name
   $table = "guestbook";             // Database Table
   $numComments = 10;       // Number of Comments per page

   // Connect to mySQL Server
   $DBConn = mysql_connect("mans_hosts","mans_lietotajvards","mana_parole") or die("Error in GuestBook Application: " . mysql_error());
   // Select mySQL Database
   mysql_select_db("mans_datubazes_vards") or die("Error in GuestBook Application: " . mysql_error());

// Part Two - Choose what action to perform
   $action = $_GET['action'];

   switch($action) {
      case 'read' :
         // Fetch all comments from database table
         $sql = 'SELECT * FROM comments' . $table . 'guestbook';
         $allComments = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());
         $numallComments = mysql_num_rows($allComments);
         // Fetch page-wise comments from database table
         $sql .= ' ORDER BY `time` DESC LIMIT ' . $_GET['NumLow'] . ', ' . $numComments;
         $fewComments = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());
         $numfewComments = mysql_num_rows($fewComments);
         // Generate Output for Flash to Read
         print '&Kopējais ierakstu skaits=' . $numallComments . '&';
         print "<br>&Ieraksti=";    

         if($numallComments == 0) {
            print "Viesu grāmatā nav ziņu";
         } else {
            while ($array = mysql_fetch_array($fewComments)) {
               $name = mysql_result($fewComments, $i, 'Vārds');
               $email = mysql_result($fewComments, $i, 'E-pasts');
               $comments = mysql_result($fewComments, $i, 'Komentāri');
               $time = mysql_result($fewComments, $i, 'Laiks');

               print '<b>Vārds: </b>' . $name . '<br><b>E-pasts: </b>' . $email . '<br><b>Komentāri: </b>' . $comments . '<br><i>Datums: ' . $time . '</i><br><br>';
               $i++;
            }

        }

        // Print this only when there aren't any more entries..
        if($_GET['NumLow'] > $numallComments) {
           print 'Nav vietas jaunām ziņām!&';
        }
        break;

      case 'write' :
         // Recieve Variables From Flash
         $name = ereg_replace("&", "%26", $_POST['yourname']);
         $email = ereg_replace("&", "%26", $_POST['youremail']);
         $comments = ereg_replace("&", "%26", $_POST['yourcomments']);
         $submit = $_POST['submit'];

         // Current system date in yyyy-mm-dd format
         $submitted_on = date ("Y-m-d H:i:s",time());

         // Check if its submitted from Flash
         if($submit == 'Yes'){
         // Insert the data into the mysql table
         $sql = "INSERT INTO guestbook ".
 "(`ID`, `name`, `email`, `comments`, `time`) VALUES ".
 "('','$name', '$email', '$comments', '$submitted_on')";
         $insert = mysql_query("guestbook") or die("Error in GuestBook Application: " . mysql_error());



         print "&gb_status=Paldies par ierakstīto ziņu.&done=yes&";
         return;
         }
         print "&_root.write.gb_status=Kļūda!&";
         break;
   }
?> 

 

 

ŠIS IR KODS PILNĪBĀ, PRTOTAMS, BEZ PAROLĒM U.T.T., BET PĀRĒJAIS VISS TAS PATS, KAS ORIĢINĀLĀ. VAI KĀDS NEVARĒTU PATEIKT, KUR MAN KĻŪDA, BŪŠU ĻOTI PATEICĪGS

Link to comment
Share on other sites

par taviem izlecieniem nemaz i negribas tev atbildēt, bet nu varbūt tu sapratīsi savu izgāšanos un kādreiz to nožēlosi (attīecībā uz php->html)

 

1. vispirms ieliec lapai aiz <? rindiņu

error_reporting(E_ALL);

 

2. pēc tam 35 rindu

$sql = 'SELECT * FROM comments' . $table . 'guestbook';

pārveido par

echo $sql = 'SELECT * FROM comments' . $table . 'guestbook';

un paskaties ko izvadīs - šo stringu ieliec kādā rīkā kas darbojas ar sql datu bāzi (phpMyAdmin, MySqlCC utt).

gribētos teikt ka rezultāts būs šāds, kas nebūt nav sql vaicājums, bet gan parodija par to "SELECT * FROM comments guestbook guestbook"

Šī vieta izskatās dīvaina..

 

3. print aizvieto ar echo, nafig tev print

 

un tad skaties uz kuru rindu tev met kļūdu, un kas tieši tā ir par kļūdu..

No ētikas viedokļa uz to kodu ir grūti skatīties - manīgo nepārbaudīšana, visa malšana vienā lielā penterī utt..

Link to comment
Share on other sites


×
×
  • Create New...