【解決方法】通知を修正するにはどうすればよいですか: undefined variable: error in C:\xampp\htdocs\new.php on line

プログラミングQA


こんにちは、私は PHP の初心者です。php と phpmyadmin を使用して Web サイトを設計しようとしています。 この Web サイトは、データベースからすべてのレコードを表示し、レコードを削除および追加することになっています。次のコードは、新しいレコードを追加するためのものです。16、20、30、32、34、36、および 83 行目にエラーがあります。

エラーの例: Notice: Undefined variable: error in C:\xampp\htdocs\New.php on line 16 同じことが、前述の行 20、30、32、34、36、および 83 にも適用されます。

エラーのあるコード行の横に行番号を含めました (egLine30)

これらの問題の原因と、それらを解決するために何をする必要がありますか???

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 の書籍、オンライン Web サイト、YouTube、同僚のコンサルティング。

解決策 1

変数が定義されていません error 行 16 で参照されています。他のステートメントの構文が正しくありません。次のようにする必要があります。

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