Click here to Skip to main content
15,883,844 members
Please Sign up or sign in to vote.
2.09/5 (3 votes)
See more:
Hi there I am a beginner of PHP, I am attempting to design a website using php and phpmyadmin. This website is supposed to view all records from a database, delete and add records.The following code is for adding a new record, I have errors on line 16, 20, 30, 32, 34, 36 and 83.

Error Example: Notice: Undefined variable: error in C:\xampp\htdocs\New.php on line 16 the same applies to as mentioned previously, lines 20, 30, 32, 34, 36 and 83.

I have included the line numbers next to the lines of code with the errors (e.g.Line30)

What is causing these problems and what do I need to do to resolve them ???




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

	}

	?>


What I have tried:

PHP books, Online websites, youtube and consulting fellow colleagues.
Posted
Updated 28-Mar-23 7:37am
v2

1 solution

You have not defined the variable error referred to on line 16. The sysntax of the other statements is not correct, it should be
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>
 
Share this answer
 
v2
Comments
Member 13710355 6-Mar-18 14:43pm    
@Richard MacCutchan, what do you mean by that the other statements are just invalid statements ? Also how would you define the variable error ?
Richard MacCutchan 6-Mar-18 14:56pm    
My mistake, they are valid. But you did not tell us what errors they produced.
Richard MacCutchan 6-Mar-18 15:07pm    
See my updated solution.
Member 13710355 7-Mar-18 10:17am    
Thank you @Richard MacCutchan for replying and the solution you have given me, but for some reason the same error is appearing for all of line numbers mentioned in my question above.

I'm not sure how to define the variables for error, ID, ProductName, Price and Stock.
Richard MacCutchan 7-Mar-18 10:40am    
I suspect it is because you are defining the variables within blocks of code i.e. between curly brace characters, so they are not known outside those blocks. You need to declare them at file scope (i.e. the beginning of the program) so they are visible to the whole program. Check the PHP documentation for samples and full explanation of scope rules: PHP 5 Variables[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900