Bagaimana cara memperbaiki pemberitahuan: variabel tidak terdefinisi: kesalahan di C:\xampp\htdocs\new.php online

pemrograman


Halo Saya seorang pemula PHP, saya mencoba mendesain situs web menggunakan php dan phpmyadmin. Situs web ini seharusnya melihat semua catatan dari database, menghapus dan menambahkan catatan. Kode berikut untuk menambahkan catatan baru, saya memiliki kesalahan pada baris 16, 20, 30, 32, 34, 36 dan 83.

Contoh Kesalahan: Pemberitahuan: Variabel tidak terdefinisi: kesalahan di C:\xampp\htdocs\New.php pada baris 16 hal yang sama berlaku seperti yang disebutkan sebelumnya, baris 20, 30, 32, 34, 36 dan 83.

Saya telah menyertakan nomor baris di sebelah baris kode yang kesalahannya (misalnyaLine30)

Apa yang menyebabkan masalah ini dan apa yang harus saya lakukan untuk mengatasinya???

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

	}

	?>

Apa yang saya coba:

Buku PHP, Website online, youtube dan konsultasi sesama rekan.

Solusi 1

Anda belum mendefinisikan variabelnya error dirujuk pada baris 16. Sintaks pernyataan lain tidak benar, seharusnya benar

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