簡體   English   中英

$error 顯示為未定義的變量

[英]$error appears as an undefined variable

所以在過去的幾周里我一直在使用 php 並且對此非常陌生。 我在 MySQL 中創建了一個數據庫,並使用 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($customer, $contactPerson, $description, $manufacturer, $serialNumber, $category, $status, $start, $end, $delivered, $quotationReference, $amount, $customerAddr, $invoice, $remarks, $error) { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta charset="UTF-8"> <title>New Entry Form</title> <link rel="stylesheet" type="text/css" href="css/newstyle.css" <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> <script> $(document).ready(function() { $("#datepicker").datepicker(); }); </script> </head> <body> <?php // if there are any errors, display them if ($error != '') { echo '<div style="padding:4px; border:1px solid white; color:white;">'.$error.'</div>'; } ?> <span href="#" class="button" id="toggle-login">New Entry</span> <div id="login"> <div id="triangle"></div> <h1>Add Details</h1> <form action="" method="POST"> <input type="text" placeholder="Customer*" name="customer" value="<?php echo $customer; ?>"/> <input type="text" placeholder="Customer Address" name="customerAddr" value="<?php echo $customerAddr; ?>"/> <input type="text" placeholder="Contact Person*" name="contactPerson" value="<?php echo $contactPerson; ?>"/> <input type="text" placeholder="Description*" name="description" value="<?php echo $description; ?>"/> <input type="text" placeholder="Manufacturer*" name="manufacturer" value="<?php echo $manufacturer; ?>"/> <input type="text" placeholder="Serial Number" name="serialNumber" value="<?php echo $serialNumber; ?>"/> <input type="text" placeholder="Category" name="category" value="<?php echo $category; ?>"/> <input type="text" placeholder="Status*" name="status" value="<?php echo $status; ?>"/> <input placeholder="Start Date" class="textbox-n" type="text" onfocus="(this.type='date')" id="start" name="start" value="<?php echo $start; ?>"> <input placeholder="End Date" class="textbox-n" type="text" onfocus="(this.type='date')" id="end" name="end" value="<?php echo $end; ?>"> <input placeholder="Delivered Date" class="textbox-n" type="text" onfocus="(this.type='date')" id="delivered" name="delivered" value="<?php echo $delivered; ?>"> <!--<input type="date" placeholder="Start Date" /> <input type="date" placeholder="End Date" /> <input type="date" placeholder="Delivered Date" />--> <input type="text" placeholder="Quotation Reference" name="quotationReference" value="<?php echo $quotationReference; ?>"/> <input type="text" placeholder="Amount" name="amount" value="<?php echo $amount; ?>"/> <input type="text" placeholder="Invoice" name="invoice" value="<?php echo $invoice; ?>"/> <input type="textarea" rows=4 cols=20 placeholder="Remarks" name="remarks" value="<?php echo $remarks; ?>"/> <!--<input type="submit" value="Add Entry" />--> <input type="submit" name="submit" value="Submit"> </form> </div> </form> </body> </html> <?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'])) { // get form data, making sure it is valid $customer = mysqli_real_escape_string($connection, htmlspecialchars($_POST['customer'])); $contactPerson = mysqli_real_escape_string($connection, htmlspecialchars($_POST['contactPerson'])); $description = mysqli_real_escape_string($connection, htmlspecialchars($_POST['description'])); $manufacturer = mysqli_real_escape_string($connection, htmlspecialchars($_POST['manufacturer'])); $serialNumber = mysqli_real_escape_string( $connection, htmlspecialchars($_POST['serialNumber'])); $category = mysqli_real_escape_string( $connection, htmlspecialchars($_POST['category'])); $status = mysqli_real_escape_string( $connection, htmlspecialchars($_POST['status'])); $end = mysqli_real_escape_string( $connection, htmlspecialchars($_POST['end'])); $delivered = mysqli_real_escape_string( $connection, htmlspecialchars($_POST['delivered'])); $start = mysqli_real_escape_string( $connection, htmlspecialchars($_POST['start'])); $quotationReference = mysqli_real_escape_string($connection, htmlspecialchars($_POST['quotationReference'])); $amount = mysqli_real_escape_string($connection, htmlspecialchars($_POST['amount'])); $customerAddr = mysqli_real_escape_string($connection, htmlspecialchars($_POST['customerAddr'])); $invoice = mysqli_real_escape_string($connection, htmlspecialchars($_POST['invoice'])); $remarks = mysqli_real_escape_string($connection, htmlspecialchars($_POST['remarks'])); // check to make sure both fields are entered if ($customer == '' || $contactPerson == '' || $description == '' || $manufacturer == '' || $status == '') { // generate error message $error = 'ERROR: Please fill in all required fields!'; // if either field is blank, display the form again renderForm($customer, $contactPerson, $description, $manufacturer, $serialNumber, $category, $status, $start, $end, $delivered, $quotationReference, $amount, $customerAddr, $invoice, $remarks, $error); } else { $q= "INSERT INTO brdatabase (customer, contactPerson , description, manufacturer , serialNumber, category , status, start, end , delivered , quotationReference, amount , customerAddr, invoice, remarks) VALUES ('$customer', '$contactPerson', '$description', '$manufacturer', '$serialNumber', '$category', '$status', '$start', '$end', '$delivered', '$quotationReference', '$amount', '$customerAddr', '$invoice', '$remarks') "; if(mysqli_query($connection, $q)) { // once saved, redirect back to the view page header("Location: index.php"); } else { echo "error : data cannot be submitted"; renderForm('','','','','','','','','','','','','','','','','',''); } } } else // if the form hasn't been submitted, display the form { renderForm('','','','','','','','','','','','','','','','','',''); } ?>

盡管此代碼運行良好,但基於上述代碼的以下代碼似乎給了我一個我不知道如何解決的錯誤。

 <?php /* NEWCOMP.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($component, $manufacturer, $vendor, $cost) { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta charset="UTF-8"> <title>New Entry Form</title> <link rel="stylesheet" type="text/css" href="css/newstyle.css" <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> <!-- <script> $(document).ready(function() { $("#datepicker").datepicker(); }); </script> </head> --> <body> <?php // if there are any errors, display them if ($error != '') { echo '<div style="padding:4px; border:1px solid white; color:white;">'.$error.'</div>'; } ?> <span href="#" class="button" id="toggle-login">New Entry</span> <div id="login"> <div id="triangle"></div> <h1>Add Details</h1> <form action="" method="POST"> <input type="text" placeholder="Component*" name="component" value="<?php echo $component; ?>"/> <input type="text" placeholder="Manufacturer*" name="manufacturer" value="<?php echo $manufacturer; ?>"/> <input type="text" placeholder="Vendor*" name="vendor" value="<?php echo $vendor; ?>"/> <input type="text" placeholder="Cost*" name="cost" value="<?php echo $cost; ?>"/> <input type="submit" name="submit" value="Submit"> </form> </div> </form> </body> </html> <?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'])) { // get form data, making sure it is valid $component = mysqli_real_escape_string($connection, htmlspecialchars($_POST['component'])); $manufacturer = mysqli_real_escape_string($connection, htmlspecialchars($_POST['manufacturer'])); $vendor = mysqli_real_escape_string($connection, htmlspecialchars($_POST['vendor'])); $cost = mysqli_real_escape_string($connection, htmlspecialchars($_POST['cost'])); // check to make sure both fields are entered if ($component == '' || $manufacturer == '' || $vendor == '' || $cost == '') { // generate error message $error = 'ERROR: Please fill in all required fields!'; // if either field is blank, display the form again renderForm($component, $manufacturer, $vendor, $cost); } else { $q= "INSERT INTO compdatabase (component, manufacturer, vendor, cost) VALUES ('$component', '$manufacturer', '$vendor', '$cost') "; if(mysqli_query($connection, $q)) { // once saved, redirect back to the view page header("Location: pricelist.php"); } else { echo "error : data cannot be submitted"; renderForm('','','',''); } } } else // if the form hasn't been submitted, display the form { renderForm('','','',''); } ?>

我收到一個錯誤“第 34 行未定義變量”,其中包含以 ($error != '') 作為條件的 if 語句。 我不清楚如何用 $error 解決這個問題。 我真的很感激任何建議和幫助。 我對 PHP 還是很陌生,希望能解決所有這些疑問

 <?php /* NEWCOMP.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($component, $manufacturer, $vendor, $cost) { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta charset="UTF-8"> <title>New Entry Form</title> <link rel="stylesheet" type="text/css" href="css/newstyle.css" <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> <!-- <script> $(document).ready(function() { $("#datepicker").datepicker(); }); </script> </head> --> <body> <span href="#" class="button" id="toggle-login">New Entry</span> <div id="login"> <div id="triangle"></div> <h1>Add Details</h1> <form action="" method="POST"> <input type="text" placeholder="Component*" name="component" value="<?php echo $component; ?>"/> <input type="text" placeholder="Manufacturer*" name="manufacturer" value="<?php echo $manufacturer; ?>"/> <input type="text" placeholder="Vendor*" name="vendor" value="<?php echo $vendor; ?>"/> <input type="text" placeholder="Cost*" name="cost" value="<?php echo $cost; ?>"/> <input type="submit" name="submit" value="Submit"> </form> </div> </form> </body> </html> <?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'])) { // get form data, making sure it is valid $component = mysqli_real_escape_string($connection, htmlspecialchars($_POST['component'])); $manufacturer = mysqli_real_escape_string($connection, htmlspecialchars($_POST['manufacturer'])); $vendor = mysqli_real_escape_string($connection, htmlspecialchars($_POST['vendor'])); $cost = mysqli_real_escape_string($connection, htmlspecialchars($_POST['cost'])); $error = ''; // check to make sure both fields are entered if ($component == '' || $manufacturer == '' || $vendor == '' || $cost == '') { // generate error message $error = 'ERROR: Please fill in all required fields!'; // if either field is blank, display the form again renderForm($component, $manufacturer, $vendor, $cost); } else { $q= "INSERT INTO compdatabase (component, manufacturer, vendor, cost) VALUES ('$component', '$manufacturer', '$vendor', '$cost') "; if(mysqli_query($connection, $q)) { // once saved, redirect back to the view page header("Location: pricelist.php"); } else { echo "error : data cannot be submitted"; renderForm('','','',''); } } } else // if the form hasn't been submitted, display the form { renderForm('','','',''); } ?> <?php // if there are any errors, display them if ($error != '') { echo '<div style="padding:4px; border:1px solid white; color:white;">'.$error.'</div>'; } ?>

好吧,在修補之后,我意識到這個錯誤非常愚蠢。 我必須確保在使用 $error 之前分配給它,而在我以前的代碼中它沒有完成。 我只是在第 34 行中使用了 $error 而沒有檢查它之前是否已分配。

我的錯誤是在我的行中

 function renderForm($component, $manufacturer, $vendor, $cost)

我所要做的就是分配錯誤並使其成功

 function renderForm($component, $manufacturer, $vendor, $cost, $error)

這也需要在我使用他的函數的任何地方通過添加額外的屬性 $error 進行編輯。 我希望遇到這個問題的任何人都知道他們現在應該檢查什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM