如何在线修复通知:未定义的变量:C:\xampp\htdocs\new.php 中的错误


您好,我是 PHP 的初学者,我正在尝试使用 php 和 phpmyadmin 设计一个网站。 该网站应该查看数据库中的所有记录,删除和添加记录。以下代码用于添加新记录,第16、20、30、32、34、36和83行有错误。

错误示例: 注意:未定义变量:第 16 行 C:\xampp\htdocs\New.php 中的错误同样适用于前面提到的第 20、30、32、34、36 和 83 行。

我已经在有错误的代码行旁边包含了行号(例如Line30)

是什么导致了这些问题以及我需要做什么来解决这些问题???

PHP
	<title>New Record

	

	

	<?php

	// if there are any errors, display them

	(Line16)if ($error != '');

	{

	(Line20)echo '<div style="padding: 4px; color: red">'.$error.'</div>';
//if assist
	}

	?>

	

	<div>
       
	(Line30)ID: * <input type="int" name="ID"<?php echo $ID; ?> /><br>

	(Line32)ProductName: * <input type="VARCHAR" name="ProductName"<?php echo $ProductName; ?> /><br>

	(Line34)Price:  * <input type="text" name="Price"<?php echo $Price; ?> /><br>
	
	(Line36)Stock:  * <input type="int" name="Stock"<?php echo $Stock; ?> /><br>

	<p>* required</p>

	

	</div>

	

	

	

	<?php

	

	//connect to database
		 $con = mysqli_connect("localhost","root","");
		 if (!$con) 
		 {
			 mysqli_select_db("stationaryonlinecustomers", $con);
		 }


	
	// 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

	$ID = mysql_real_escape_string(htmlspecialchars($_POST['ID']));

	$ProductName = mysql_real_escape_string(htmlspecialchars($_POST['ProductName']));

    $Price = mysql_real_escape_string(htmlspecialchars($_POST['Price']));
       
    $Stock = mysql_real_escape_string(htmlspecialchars($_POST['Stock']));
      
	}

	// check to make sure both fields are entered

	(Line 83)if ($ID == '' || $ProductName == '' || $Price == '' || $Stock =='')

	{

	// generate error message

	$error = 'ERROR: Please fill in all required fields!';
	
	}

	else{
	// save the data to the database

	$u = mysql_query($con, "INSERT productorders SET ID='".$ID."', ProductName='".$ProductName."', Price='".$Price."', Stock='".$Stock."'");
	

	// once saved, redirect back to the view page

	header("location:View.php");

	

	

	// if the form hasn't been submitted, display the form

	renderForm('','','');

	}

	?>

我尝试过的:

PHP 书籍、在线网站、youtube 和咨询同事。

解决方案1

您还没有定义变量 error 参考第16行。其他语句的sysntax不正确,应该是

PHP
<div>
<!-- note the position of the tag closer (/>) should come before the PHP statement
 -->
ID: * <input type="int" name="ID" /><?php echo $ID; ?> <br>

ProductName: * <input type="VARCHAR" name="ProductName" /><?php echo $ProductName; ?><br>

Price:  * <input type="text" name="Price" /><?php echo $Price; ?><br>

Stock:  * <input type="int" name="Stock" /><?php echo $Stock; ?><br>

<p>* required</p>



</div>

コメント

タイトルとURLをコピーしました