2015-02-24 106 views
0

如何解決這個錯誤我的代碼工作正常,但我不知道它爲什麼顯示錯誤我的圖片上傳正常。上傳照片錯誤注意:未定義索引:照片

請幫我解決這個問題 感謝 我有這個錯誤

注意:未定義指數:照片I:\ XAMPP \ htdocs中\ CCS \ ad.php上線121

注意:未定義指數:照片I:\ XAMPP \ htdocs中\ CCS上線\ ad.php 125

注意:未定義指數:照片I:\ XAMPP \ htdocs中\ CCS \上線ad.php 127

線121 是

$target = $target . basename($_FILES['photo']['name']); 

線125 是

$photo=($_FILES['photo']['name']); 

線126 是

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 

和這裏完整代碼

<?php 

/* 
NEW.PHP 
Allows user to create a new entry in the database 
*/ 

// creates the new record form 
// since this form is used multiple times in this file, I have made it a function that is easily reusable 
function renderForm($sname, $fname, $error) 
{ 
?> 
<?php 
// if there are any errors, display them 
if ($error != '') 
{ 
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; 
} 
?> 



    <form action="" method="post" enctype="multipart/form-data" class="registration_form"> 
    <fieldset> 
    <legend>Admission </legend> 

    <p>Create a Admission Voucher <span class="style4" style="background:#EAEAEA none repeat scroll 0 0;line-height:1;margin-left:410px;;padding:9px 9px;">Please Fill the All Info </span> </p> 
     <div class="elements"> 
     <label for="sname">Student Name :</label> 
     <input type="text" id="sname" name="sname" size="70" /> 

</div> 
    <div class="elements"> 
     <label for="fname">Father Name :</label> 
     <input type="text" id="fname" name="fname" size="25" /> 
</div> 

      <div class="elements"> 
     <label for="photo">Photo Attachment :</label> 
<input type="hidden" name="size" value="350000"> 
      <input type="file" name="photo" size="25"> 
    </div> 



    <div class="submit"> 
<button name="submit" type="submit" class="pure-button pure-button-primary" value="Submit">Submit</button> 
    </div> 
    </fieldset> 
</form> 

    <?php 
    } 

    //This is the directory where images will be saved 
    $target = "images/"; 
    $target = $target . basename($_FILES['photo']['name']); 

    //This gets all the other information from the form 

    $photo=($_FILES['photo']['name']); 
    //Writes the photo to the server 
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
    { 

    //Tells you if its all ok 
    echo "<center>Photo ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory</center>"; 
    } 



    // connect to the database 
    include('connect-db.php'); 



    // check if the form has been submitted. If it has, start to process the form and save it to the database 
    if (isset($_POST['submit'])) 
    { 
    // get form data, making sure it is valid 
    $sname = mysql_real_escape_string(htmlspecialchars($_POST['sname'])); 
    $fname = mysql_real_escape_string(htmlspecialchars($_POST['fname'])); 
    $photo = mysql_real_escape_string(htmlspecialchars($_FILES['photo']['name'])); 

    // check to make sure both fields are entered 
    if ($sname == '') 
    { 
    // generate error message 
    $error = 'ERROR: Please fill in all required fields!'; 

    // if either field is blank, display the form again 
    renderForm($sname, $fname,$error); 
    } 
    else 
    { 
    // save the data to the database 
     mysql_query("INSERT admission SET sname='$sname', fname='$fname', photo='$photo'") 
    or die(mysql_error()); 
    echo "<center>Admission</center>"; 
    echo "<center>Successful!</center>"; 
    // once saved, redirect back to the view page 

    } 
    } 
    else 
    // if the form hasn't been submitted, display the form 
    { 
    renderForm('','','',''); 
    } 


    ?> 
+1

不,這不是dublicate – user1833487 2015-02-24 00:19:40

+1

的區別是什麼?告訴我們! – Rizier123 2015-02-24 00:20:11

+0

http://stackoverflow.com/questions/888/how-do-you-debug-php-scripts – Halcyon 2015-02-24 00:20:33

回答

4

這是一個註釋太長,因爲我想在那裏解釋,但認爲這將是更好的,如果它是在回答中表示。

注意:我也將$_FILES['uploadedfile']更改爲$_FILES['photo'],因爲這會產生錯誤。

將一切從$target = "images/";開始到echo "<center>Photo ". basename...然後if (isset($_POST['submit'])){下放置,然後修改條件語句改爲if (isset($_POST['submit']) && !empty($_FILES['photo'])){,你應該是好去。

這裏有一個重寫:

<?php 

/* 
NEW.PHP 
Allows user to create a new entry in the database 
*/ 

// creates the new record form 
// since this form is used multiple times in this file, I have made it a function that is easily reusable 
function renderForm($sname, $fname, $error) 
{ 
?> 
<?php 
// if there are any errors, display them 
if ($error != '') 
{ 
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; 
} 
?> 

    <form action="" method="post" enctype="multipart/form-data" class="registration_form"> 
    <fieldset> 
    <legend>Admission </legend> 

    <p>Create a Admission Voucher <span class="style4" style="background:#EAEAEA none repeat scroll 0 0;line-height:1;margin-left:410px;;padding:9px 9px;">Please Fill the All Info </span> </p> 
     <div class="elements"> 
     <label for="sname">Student Name :</label> 
     <input type="text" id="sname" name="sname" size="70" /> 

</div> 
    <div class="elements"> 
     <label for="fname">Father Name :</label> 
     <input type="text" id="fname" name="fname" size="25" /> 
</div> 

      <div class="elements"> 
     <label for="photo">Photo Attachment :</label> 
<input type="hidden" name="size" value="350000"> 
      <input type="file" name="photo" size="25"> 
    </div> 

    <div class="submit"> 
<button name="submit" type="submit" class="pure-button pure-button-primary" value="Submit">Submit</button> 
    </div> 
    </fieldset> 
</form> 

<?php 
    } 

    // connect to the database 
    include('connect-db.php'); 

    // check if the form has been submitted. If it has, start to process the form and save it to the database 
    if (isset($_POST['submit']) && !empty($_FILES['photo'])) 
    { 

    //This is the directory where images will be saved 
    $target = "images/"; 
    $target = $target . basename($_FILES['photo']['name']); 

    //This gets all the other information from the form 

    $photo=($_FILES['photo']['name']); 
    //Writes the photo to the server 
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
    { 

    //Tells you if its all ok 
    echo "<center>Photo ". basename($_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory</center>"; 
    } 

    // get form data, making sure it is valid 
    $sname = mysql_real_escape_string(htmlspecialchars($_POST['sname'])); 
    $fname = mysql_real_escape_string(htmlspecialchars($_POST['fname'])); 
    $photo = mysql_real_escape_string(htmlspecialchars($_FILES['photo']['name'])); 

    // check to make sure both fields are entered 
    if ($sname == '') 
    { 
    // generate error message 
    $error = 'ERROR: Please fill in all required fields!'; 

    // if either field is blank, display the form again 
    renderForm($sname, $fname,$error); 
    } 
    else 
    { 
    // save the data to the database 
     mysql_query("INSERT admission SET sname='$sname', fname='$fname', photo='$photo'") 
    or die(mysql_error()); 
    echo "<center>Admission</center>"; 
    echo "<center>Successful!</center>"; 
    // once saved, redirect back to the view page 

    } 
    } 
    else 
    // if the form hasn't been submitted, display the form 
    { 
    renderForm('','','',''); 
    } 


?> 
+0

現在非常感謝它工作正常 – user1833487 2015-02-24 01:38:05

1

後您的交易</form>更改代碼

<?php 
} 





// check if the form has been submitted. If it has, start to process the form and save it to the database 
if (isset($_POST['submit'])) 
{ 
    //This is the directory where images will be saved 
$target = "images/"; 
$target = $target . basename($_FILES['photo']['name']); 

//This gets all the other information from the form 

$photo=($_FILES['photo']['name']); 
//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "<center>Photo ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory</center>"; 
} 



// connect to the database 
include('connect-db.php'); 
// get form data, making sure it is valid 
$sname = mysql_real_escape_string(htmlspecialchars($_POST['sname'])); 
$fname = mysql_real_escape_string(htmlspecialchars($_POST['fname'])); 
$photo = mysql_real_escape_string(htmlspecialchars($_FILES['photo']['name'])); 

// check to make sure both fields are entered 
if ($sname == '') 
{ 
// generate error message 
$error = 'ERROR: Please fill in all required fields!'; 

// if either field is blank, display the form again 
renderForm($sname, $fname,$error); 
} 
else 
{ 
// save the data to the database 
    mysql_query("INSERT admission SET sname='$sname', fname='$fname', photo='$photo'") 
or die(mysql_error()); 
echo "<center>Admission</center>"; 
echo "<center>Successful!</center>"; 
// once saved, redirect back to the view page 

} 
} 
else 
// if the form hasn't been submitted, display the form 
{ 
renderForm('','','',''); 
} 


?>