Click here to Skip to main content
14,691,047 members
Please Sign up or sign in to vote.
1.80/5 (2 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 ???




	<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 23-Sep-20 6:04am
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
<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>
   
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[^].
Tino Fourie 30-Jun-20 19:09pm
   
Old post I know, but you got me back on track. My code was working great and today I made quite a few changes all over. After that I got that damn messages and I could not figure out what the hell I did. I searched StackExchange for many hours and there were many a solution which included another 5 days coding just to fix it. So simple!! Not sure what they are smoking there but it aint good. +5 to you Richard!! +5 to OP for asking the question.
Richard MacCutchan 1-Jul-20 3:33am
   
Thanks for the comments, and good luck to you.

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