Head First PHP&MySQL 学习笔记(三,四) —— 现实的实际应用


PHP

超级全局变量

_$SERVER['PHP_SELF']存储了当前脚本的名字。如,sendemail.php中_$SERVER['PHP_SELF']的值是'sendemail.php'


逻辑运算符

!  ||  &&


判断语句if  --- PHP的语法与C语言惊人的相似

if ()		//>, >=, <, <=, !=(<>), ==
{
	;
}
else
{
	;
}

foreach循环遍历

foreach($_POST['todelete'] as $delete_id) {                                
            $query = "DELETE FROM email_list WHERE id='$delete_id'";               
            mysqli_query($dbc, $query) or die("Error querying database.");         
            //echo $query . '<br />';                                              
        }

表单中的数组 todelete[]

'[]' 告诉PHP脚本,$_POST[‘todelete’]是一个数组

    while ($row = mysqli_fetch_array($result))                                     
    {                                                                              
        $firstname = $row['first_name'];                                           
        $lastname  = $row['last_name'];                                            
        echo '<input type="checkbox" value="' . $row['id'] . '"name="todelete[]" />';
        echo $row['first_name'];                                                   
        echo ' ' . $row['last_name'];                                              
        echo ' ' . $row['email'];                                                  
        echo '<br />';                                                             
    }

PHP中可以这样

一个if...else语句段可以放在两个连续的<?php ?>中。也就是说PHP解析器连续的执行<?php ?>中的PHP脚本。

<?php                                                                              
                                                                                   
    $output_form = false;
    if($output_form)                                                               
    {
?>

<!-- html content -->
<?php                                                                              
    }                                                                              
    else                                                                           
    {
         //..... 
    }
?>    


PHP自引用(删掉html只用php就够了)

PHP中同时包含HTML和PHP脚本,见文章最后面的示例程序

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">              
    <label for="subject">Subject of email:</label><br />                           
    <input id="subject" name="subject" type="text" size="30"                       
     value="<?php echo $subject; ?>"/><br />                                       
    <label for="elvismail">Body of email:</label><br />                            
    <textarea id="elvismail" name="elvismail" rows="8" cols="40"                   
    ><?php echo $text; ?></textarea><br />                                         
    <input type="submit" name="submit" value="Submit" />                           
  </form> 


MySQL

修改表属性ALTER TABLE

      
ALTER TABLE email_list ADD id INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY(id);


示例程序

//sendemail.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"                     
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">                       
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">                
<head>                                                                             
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />            
  <title>Make Me Elvis - Send Email</title>                                        
  <link rel="stylesheet" type="text/css" href="style.css" />                       
</head>                                                                            
<body>                                                                             
  <img src="blankface.jpg" width="161" height="350" alt="" style="float:right" />
  <img name="elvislogo" src="elvislogo.gif" width="229" height="32" border="0" alt="Make Me Elvis" />
  <p><strong>Private:</strong> For Elmer's use ONLY<br />                          
  Write and send an email to mailing list members.</p>                             
                                                                                   
<?php                                                                              
                                                                                   
    $output_form = false;                                                          
    if(isset($_POST['submit']))                                                    
    {                                                                              
        $from = '[email protected]';                                       
        $subject = $_POST['subject'];                                              
        $text    = $_POST['elvismail'];                                            
                                                                                   
        if(empty($subject) || empty($text))                                        
        {                                                                          
            echo 'You forgot the email subject or content.<br />';                 
            //echo $_POST['submit'] . '<br />';                                    
            $output_form = true;                                                   
        }                                                                          
    }                                                                              
    else                                                                           
    {                                                                              
        $output_form = true;                                                       
    }                                                                              
                                                                                   
    if($output_form)                                                               
    {                                                                              
?>                                                                                 
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">              
    <label for="subject">Subject of email:</label><br />                           
    <input id="subject" name="subject" type="text" size="30"                       
     value="<?php echo $subject; ?>"/><br />                                       
    <label for="elvismail">Body of email:</label><br />                            
    <textarea id="elvismail" name="elvismail" rows="8" cols="40"                   
    ><?php echo $text; ?></textarea><br />                                         
    <input type="submit" name="submit" value="Submit" />                           
  </form>                                                                          
<?php                                                                              
    }                                                                              
    else                                                                           
    {                                                                              
        $dbc = mysqli_connect('localhost', 'root', '123456', 'elvis_store')        
            or die('Error connecting to MySQL server.');                           
                                                                                   
                                                                                   
        $query = "SELECT* FROM  email_list";                                       
        $result = mysqli_query($dbc, $query) or die("Error querying database.");
                                                                                
        while ($row = mysqli_fetch_array($result))                              
        {                                                                       
            $firstname = $row['first_name'];                                    
            $lastname  = $row['last_name'];                                     
            $msg       = "Dear $firstname $lastname,\n$text";                   
            $to        = $row['email'];                                         
                                                                                
            //mail($to, $subject, $msg, 'From:' . $from); /* Note: sendmail need installed the configured correctly */
            echo 'Email sent to:' . $to . '<br />';                             
        }                                                                       
                                                                                
        mysqli_close($dbc);                                                     
        //echo "Customer added:\n $firstname, \n $lastname, \n $emal"           
    }                                                                           
?>                                                                              
                                                                                
</body>                                                                         
</html>                                                                         

//removeemail.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"                     
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">                       
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">                
<head>                                                                             
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />            
  <title>Make Me Elvis - Remove Email</title>                                      
  <link rel="stylesheet" type="text/css" href="style.css" />                       
</head>                                                                            
<body>                                                                             
  <img src="blankface.jpg" width="161" height="350" alt="" style="float:right" />
  <img name="elvislogo" src="elvislogo.gif" width="229" height="32" border="0" alt="Make Me Elvis" />
                                                                                   
<?php                                                                              
    $dbc = mysqli_connect('localhost', 'root', '123456', 'elvis_store')            
        or die('Error connecting to MySQL server.');                               
                                                                                   
    if(isset($_POST['Remove'])) {                                                  
        foreach($_POST['todelete'] as $delete_id) {                                
            $query = "DELETE FROM email_list WHERE id='$delete_id'";               
            mysqli_query($dbc, $query) or die("Error querying database.");         
            //echo $query . '<br />';                                              
        }                                                                          
    }                                                                              
?>                                                                                 
                                                                                   
  <p>Select the customers you want to remove.</p>                                  
  <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">                 
    <label for="email">Customers:</label><br />                                    
<?php                                                                              
    $query = "SELECT* FROM  email_list";                                           
    $result = mysqli_query($dbc, $query) or die("Error querying database.");       
                                                                                   
    while ($row = mysqli_fetch_array($result))                                     
    {                                                                              
        $firstname = $row['first_name'];                                           
        $lastname  = $row['last_name'];                                            
        echo '<input type="checkbox" value="' . $row['id'] . '"name="todelete[]" />';
        echo $row['first_name'];                                                   
        echo ' ' . $row['last_name'];                                              
        echo ' ' . $row['email'];                                                  
        echo '<br />';                                                             
    }                                                                              
    mysqli_close($dbc);                                                            
?>                                                                                 
    <input type="submit" name="Remove" value="Remove" />                           
  </form>                                                                          
</body>                                                                            
</html>




你可能感兴趣的:(Head First PHP&MySQL 学习笔记(三,四) —— 现实的实际应用)