Làm cách nào để sửa thông báo: biến không xác định: lỗi trong C:\xampp\htdocs\new.php trực tuyến

lập trình


Xin chào, tôi là người mới bắt đầu học PHP, tôi đang cố gắng thiết kế một trang web bằng php và phpmyadmin. Trang web này có nhiệm vụ xem tất cả các bản ghi từ cơ sở dữ liệu, xóa và thêm bản ghi. Đoạn mã sau dùng để thêm bản ghi mới, tôi gặp lỗi ở dòng 16, 20, 30, 32, 34, 36 và 83.

Ví dụ về lỗi: Lưu ý: Biến không xác định: lỗi trong C:\xampp\htdocs\New.php trên dòng 16, điều tương tự cũng áp dụng cho các dòng 20, 30, 32, 34, 36 và 83 như đã đề cập trước đó.

Tôi đã bao gồm số dòng bên cạnh các dòng mã có lỗi (egLine30)

Điều gì gây ra những vấn đề này và tôi cần làm gì để giải quyết chúng ???

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('','','');

	}

	?>

Những gì tôi đã thử:

Sách PHP, Website trực tuyến, youtube và tư vấn đồng nghiệp.

Giải pháp 1

Bạn chưa xác định biến error được đề cập ở dòng 16. Cú pháp của các câu lệnh khác là không chính xác, nó phải là

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をコピーしました