2014-04-08 81 views
0

我讀到未定義指數誤差所有的答案,但不是因爲我已經在使用isset功能檢查PLZ如何解決售後服務幫助解決這個問題充滿了對我..未定義指數形式的錯誤

<?php 
$con=mysqli_connect("localhost","root","","contact"); 
if (mysqli_connect_errno()) 
{ 
    echo "failed".mysqli_connect_error(); 
    } 

檢查submited數據

if(isset($_POST['submit'])) 
     { 
     $name=$_POST['name']; 
     $website=$_POST['website']; 
     $gender=$_POST['gender']; 
     $comment=$_POST['comment']; 
     } 
     $sql="insert into form(name,website,gender,comment) Values('$_POST[name]','$_POST[website]','$_POST[gender]','$_POST[comment]')"; 

     if(!mysqli_query($con,$sql)) 
      { 
       die('error:'.mysqli_error($con)); 
      } 
     else "added"; 

     mysqli_close($con); 
     ?> 
    <html> 
     <body> 
      <form method=post action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
       Name: <input type="text" name="name"><br> 
       E-mail: <input type="text" name="email"><br> 
       Website: <input type="text" name="website"><br> 
       <input type="radio" name="gender" value="female">Female 
       <input type="radio" name="gender" value="male">Male<br> 
       Comment: <textarea name="comment" rows="5" cols="40"></textarea> 
       <input type=submit name="submit"><br> 
      </form> 
     </body> 

這些錯誤來自

Notice: Undefined index: name in H:\Wamp\Xamp\htdocs\form.php on line 15 

Notice: Undefined index: website in H:\Wamp\Xamp\htdocs\form.php on line 15 

Notice: Undefined index: gender in H:\Wamp\Xamp\htdocs\form.php on line 15 

Notice: Undefined index: comment in H:\Wamp\Xamp\htdocs\form.php on line 15 
+1

移動烏爾查詢代碼裏面,如果(isset($ _ POST [ '提交'] )) –

+1

[PHP:「Notice:Undefined variable」和「Notice:Undefined index」]的可能重複(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-索引) – Rikesh

+0

以isset($ _ POST ['submit'])的右括號並將其放在b ''和'?>' – Vagabond

回答

1

請嘗試以下糾正代碼:

if(isset($_POST['submit'])) 
     { 
     $name=isset($_POST['name']) ? $_POST['name'] : ''; 
     $website=isset($_POST['website']) ? $_POST['website'] : ''; 
     $gender=isset($_POST['gender']) ? $_POST['gender'] : ''; 
     $comment=isset($_POST['comment']) ? $_POST['comment'] : ''; 
     $sql="insert into form(name,website,gender,comment) Values('$name','$website','$gender','$comment')"; 

     // Open the database connection here 
     // aka, mysqli_connect() 


     if(!mysqli_query($con,$sql)) 
      { 
       die('error:'.mysqli_error($con)); 
      } 
     else "added"; 

     mysqli_close($con); 
     } 
     ?> 
    <html> 
     <body> 
      <form method=post action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
       Name: <input type="text" name="name"><br> 
       E-mail: <input type="text" name="email"><br> 
       Website: <input type="text" name="website"><br> 
       <input type="radio" name="gender" value="female">Female 
       <input type="radio" name="gender" value="male">Male<br> 
       Comment: <textarea name="comment" rows="5" cols="40"></textarea> 
       <input type=submit name="submit"><br> 
      </form> 
     </body> 

我所做的是添加了驗證檢查,如果這些字段設置,如果是這樣,那麼設置的值,如果沒有,則設置變量(又名$ name)改爲''。如果必填字段爲「=」(等於空白),您應該添加一些進一步的驗證。

我還調整了您的查詢,不使用$ _POST變量,而是使用您分配$ _POST值的變量,因此您知道它們存在的確定。

最後,我移動了mysql連接代碼,並將自己查詢到if(isset(submit))語句,因此它不會嘗試處理那些尚未提交表單的常規頁面加載。

0

變化

if(isset($_POST['submit'])) 
     { 
     $name=$_POST['name']; 
     $website=$_POST['website']; 
     $gender=$_POST['gender']; 
     $comment=$_POST['comment']; 
     } 
     $sql="insert into form(name,website,gender,comment) Values('$_POST[name]','$_POST[website]','$_POST[gender]','$_POST[comment]')"; 
     if(!mysqli_query($con,$sql)) 
     { 
      die('error:'.mysqli_error($con)); 
     } 
     else "added"; 

     mysqli_close($con); 
     ?> 

if(isset($_POST['submit'])) 
{ 
     $name=$_POST['name']; 
     $website=$_POST['website']; 
     $gender=$_POST['gender']; 
     $comment=$_POST['comment']; 
     $sql="insert into form(name,website,gender,comment) values ('$name','$website','$gender','$comment')"; 
     if(!mysqli_query($con,$sql)) 
     { 
      die('error:'.mysqli_error($con)); 
     } 
     else "added"; 
     mysqli_close($con); 

    }?> 
+0

undefind變量$ sql這也檢查之前,然後問題在這裏.. – Zu007

+0

Try @ user1153551答案 –

+0

@ Zu007 - 如果條件不只是一個SQL語句。 – Rikesh

1

更新此插入查詢,

$sql="insert into form(name,website,gender,comment) values('". $name ."','". $website ."','". $gender ."','". $comment ."')"; 

希望這有助於你!

0

比朋友的問題是很難 什麼錯?

if(isset($_POST['submit'])) 
     { 
     $name=$_POST['name']; 
     $website=$_POST['website']; 
     $gender=$_POST['gender']; 
     $comment=$_POST['comment']; 


     $sql="insert into form(name,website,gender,comment) Values('". $name . "','" . $website . "','" . $gender . "','" . $comment . "')"; 

//這也是如果(isset()函數)塊

if(!mysqli_query($con,$sql)) 
      { 
       die('error:'.mysqli_error($con)); 
      } 
     else "added"; 
     } 

感謝名單向所有