¿Cómo soluciono el aviso: variable no definida: error en C:\xampp\htdocs\new.php en línea?

programación


Hola, soy principiante en PHP. Estoy intentando diseñar un sitio web usando php y phpmyadmin. Se supone que este sitio web ve todos los registros de una base de datos, elimina y agrega registros. El siguiente código es para agregar un nuevo registro, tengo errores en las líneas 16, 20, 30, 32, 34, 36 y 83.

Ejemplo de error: Aviso: Variable no definida: error en C:\xampp\htdocs\New.php en la línea 16, lo mismo se aplica a lo mencionado anteriormente, líneas 20, 30, 32, 34, 36 y 83.

He incluido los números de línea junto a las líneas de código con los errores (por ejemplo, Línea30)

¿Qué está causando estos problemas y qué debo hacer para resolverlos?

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

	}

	?>

Lo que he probado:

Libros de PHP, sitios web en línea, youtube y consultoría a colegas.

Solución 1

No has definido la variable error mencionado en la línea 16. La sintaxis de las otras declaraciones no es correcta, debe ser

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