$ error显示为未定义的变量

时间:2015-06-15 02:47:08

标签: php mysql error-handling

所以我在过去几周一直在使用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('','','','','','','','','','','','','','','','','','');
 }

?>
&#13;
&#13;
&#13;

虽然这段代码完美无缺, 以下代码基于上述代码,似乎给了我一个我不知道如何处理的错误。

&#13;
&#13;
<?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('','','','');
 }

?>
&#13;
&#13;
&#13;

我收到错误&#34;第34行&#34;未定义的可变量其中包含带有($ error!=&#39;&#39;)条件的if语句。我不清楚如何用$ error解决这个问题。 我真的很感激任何建议和帮助。我仍然是PHP的新手,希望能够解决所有这些疑虑

2 个答案:

答案 0 :(得分:0)

<?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>';
     }
     ?> 

答案 1 :(得分:0)

好的,所以在修修补补之后,我意识到这个错误非常愚蠢。我必须确保在使用它之前已经完成了对$ error的赋值,并且在之前的代码中它还没有完成。我只是在第34行使用$ error而不检查它是否先前已分配。

我的错误是在我的行

&#13;
&#13;
function renderForm($component, $manufacturer, $vendor, $cost)
&#13;
&#13;
&#13;

我所要做的就是分配错误并使其成为

&#13;
&#13;
function renderForm($component, $manufacturer, $vendor, $cost, $error)
&#13;
&#13;
&#13;

这也需要编辑,无论我通过添加额外属性$ error使用他的功能。我希望遇到这个问题的任何人都知道他们现在应该检查什么。