未定义的索引使用isset解决,但无法在数据库

时间:2015-12-31 07:08:29

标签: php undefined-index

我已经使用isset函数来清除未定义的索引,但是在其他函数上出现问题,例如当我将数据提交到localhost服务器中的数据库时,无法查看或插入。 **

  <?php
            $con = mysqli_connect ('127.0.0.1','root','');
            if (!$con)
            {
                echo 'Not connected to server';
            }

            if(!mysqli_select_db($con,'contact'))
            {
                echo 'database not selected';           
            }



            $name = $_POST['name'];
            $subject = $_POST['subject'];   
            $email = $_POST['email'];
            $message = $_POST['message'];

            $sql = "INSERT INTO feedback (name, subject, email, message)    
             VALUES ('$name', '$subject', '$email','$message')";
               ?>

           <!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">
           <head>
         <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
          <title>Tam Nasi Ayam</title>
         <link href="styles/style.css" rel="stylesheet" type="text/css"    
         media="screen" />
          </head>
         <body>
          <div id="container">
         <header>
          <nav>
          <ul id="nav">
          <li><a href="index.php">Home</a></li>
          <li><a href="menu.php">Menu</a></li>
         <li><a href="promotion.php">Promotion</a></li>
          <li><a href="gallery.php">Gallery</a></li>
         <li><a href="contact.php" class="current">Contact</a></li>
        </ul>
          </nav>
        </header>
        </div>
         </body>


         <?php

         if (isset($_POST['upload_img'])){
         $file_name = $_FILES['image'] ['name'];
         $file_type = $_FILES['image'] ['type'];
         $file_size = $_FILES['image'] ['size'];
         $file_tmp_name = $_FILES['image'] ['tmp_name'];

         if ($file_name{move_uploaded_file($file_tmp_name,"img/$file_name");


            }
            }
            ?>
           <html>
           <body>
           <center>


           <form action="" method="post" enctype="multipart/form-data"      
           align="center">
            <label>Upload Image</label><br><br>
            <input type="file" name="image"><br><br>
            <input type="submit" value="Upload Image" name="upload_img">

             </form>

             </center>
             </body>
              </html>


              <?php


              $folder = "img/" ;


               if(is_dir($folder)){if($handle = opendir($folder)){

                while (($file=readdir($handle))!= false) {

                if ($file==='.'|| $file=== '..') continue;  

         echo '<img src="img/'.$file.'" width="150" height="150" alt=""   
          align="center">';

           }
         closedir($handle);
            }   


             }

             ?>


         <form name="form 2" method="post" align="center"       
         action="foodfile.php"><br>    
         <br>

         Days:<input name="days" type="text" />

         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


           <br><br>
         Date: <input type="text" name="date"  />
         </label>    
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

          Time: <input  type="text" name="time" /><br><br>


          Food: <input type="text" name="food" /><br><br>


          Drink:<input type="text" name="drink" /><br><br> 


          Custom: <br> <textarea name="custom" ></textarea> <br><br>         

           Price: <input name="price" type="text" />

            <p>&nbsp;</p>


            <div align="center">
             <p>
           <input type="submit" name="button"  value="Submit" />

            <input type="submit" name="button3"  value="Default" />
            </p>
            </div>
            </form>



             <?php
             if (!mysqli_query ($con, $sql))
            {
            echo '';

            }
            else
             {
            echo '';    
              }
              ?>

            <div class="border2"></div>
            <br>
            </div>
             <footer>
            <div class="border"></div>
            <div class="footer-widget">
            <h6>Sitemaps</h6>
             <a href="index.php">Homepage</a><br>
             <a href="menu.php">Menu</a><br>
             <a href="promotion.php">Promotion</a><br>
              <a href="gallery.php">Gallery</a><br>
             <a href="contact.php">Contact</a><br>
              </div>
            <div class="footer-widget">
             <h6>HighTech Sdn. Bhd.</h6>
           <p>&copy; Copyright 2012 - All Rights Reserved HighTechnology 
            Sendirian Berhad</p><br>
             </div>

              <div class="footer-widget">
               <h6>We Are Social!</h6>
               <div id="social"> <a    
               href="http://instagram.com/explore/locations472284877/"                 
                class="s3d forrst" >   
               Instagram <span class="icons forrst"></span></a>  
             <a href="https://ms-my.facebook.com/Tamnasiayam" class="s3d 
                 facebook"     
               target="_blank"> Facebook <span class="icons facebook">             
               </span> </a></div>
               </div>
                 <br>
               <br />
                </span></span></footer>
             </div>
              </body>
               </html>

**

2 个答案:

答案 0 :(得分:0)

你忘了运行查询。 使用此$con->query($sql);

以便您的整个代码看起来像

<?php $con = mysqli_connect ('127.0.0.1','root',''); 
  if (!$con) { echo 'Not connected to server'; }
  if(!mysqli_select_db($con,'contact')) 
  { echo 'database not selected';   } 
  $name = $_POST['name']; 
  $subject = $_POST['subject']; 
  $email = $_POST['email']; 
  $message = $_POST['message']; 
  $sql = "INSERT INTO feedback (name, subject, email, message) VALUES ('$name', '$subject', '$email','$message')"; ?>
  $con->query($sql);
?>

答案 1 :(得分:0)

你的代码应该是:

<?php 
$con = mysqli_connect ('127.0.0.1','root',''); 
if (!$con) { 
    echo 'Not connected to server'; 
} 
if(!mysqli_select_db($con,'contact')) { 
    echo 'database not selected'; 
} 
$name = $_POST['name']; 
$subject = $_POST['subject']; 
$email = $_POST['email']; 
$message = $_POST['message']; 
$sql = "INSERT INTO feedback (name, subject, email, message) VALUES ('$name', '$subject', '$email','$message')";
$insert = mysqli_query($con,$sql); // add this line
?>