Page 1 of 1

php learner: dont know what's wrong

Posted: Thu Dec 18, 2014 8:06 am
by chized
hello everyone, thanks for opening up the forum to all. am a bit new to php, ve not mastered a lot of things. i've trying to find what is wrong with this code: meant to insert data from a form into the database. i'll be grateful for the help. thanks
error message is:
Notice: Undefined variable: name in C:\xampp\htdocs\pgschool\pgindex.php on line 38

Notice: Undefined variable: regno in C:\xampp\htdocs\pgschool\pgindex.php on line 38

Notice: Undefined variable: faculty in C:\xampp\htdocs\pgschool\pgindex.php on line 38.......
========================================
here is the code
=======================================================

Code: Select all

<?php include("includes/functions.php");?>
<html>
<head>
<link rel = "stylesheet" href = "includes/pgindex.css" type = "text/css" media = "screen">
<head>

<?php $connection = mysql_connect("localhost", "root", "");
	if (!$connection){
		die("Database connection failed to connect: " .mysql_error());
	}
	
	$db_select = mysql_select_db("pgschool", $connection);
	if (!$db_select){
		die("database selection  failed: " . mysql_error());
	}
	
 ?>
		<?php
			if (isset($_POST['submit'])){
			$id = mysql_prep($_GET['id']);			
			$name = mysql_prep($_POST['name']);
			$regno = mysql_prep($_POST['regno']);
			$faculty = mysql_prep($_POST['faculty']);
			$dept = mysql_prep($_POST['dept']);
			$deg_in_view = mysql_prep($_POST['deg_in_view']);
			$initial_reg_date = mysql_prep($_POST['initial_reg_date']);
			$payment_id = mysql_prep($_POST['payment_id']);
			$session = mysql_prep($_POST['session']);
			$amount_due = mysql_prep($_POST['amount_due']);
			$amount_paid = mysql_prep($_POST['amount_paid']);
			$receipt_no = mysql_prep($_POST['receipt_no']);
			$balance = mysql_prep($_POST['balance']);
			$officer_remark = mysql_prep($_POST['officer_remark']);
			$date = mysql_prep($_POST['date']);
			}
			$query = "INSERT INTO payhistory (name, regno, faculty, dept, deg_in_view,
			 initial_reg_date, payment_id, session, amount_due, amount_paid, receipt_no, balance, officer_remark, date
			 ) VALUES ('$name','$regno','$faculty','$dept','$deg_in_view','$initial_reg_date',
			$payment_id,'$session', $amount_due, $amount_paid,$receipt_no,$balance,
			'$officer_remark',$date)";
			
			$result = mysql_query($query, $connection); 
			
			if ($result)  {
				//success!
				//redirect_to("home.php");
				echo "<p> Form has been succesfully submitted. </p>";
				
				}else{
				//Display error msg
				echo "<p> Form submission failed..</p>";
				echo "<p>". mysql_error() ."</p>";
				}
				
		?>


 
 <body>
	<div>
		
	<h3>PG Payment History form</h3>
	</div>
	<form action = "pgindex.php" name = "" method = "post">
		
		<p>NAME:  <input type = "text" id = "" name = "name" /></p>
		<p>REG NO:  <input type = "text" id = "" name = "reg_no" /></p>
		<p>FACULTY:  	<select name="faculty" >
		<?php $faculties = mysql_query("SELECT * FROM faculties", $connection); 
			if (!$faculties){
				die ("database query failed: ". mysql_error());
			}?>
			
			<?php while ($faculty = mysql_fetch_array($faculties)) { ?>
			<option><?php echo $faculty["fac_name"]; ?></option>
			<?php } ?>			
		</select></p>
		<p>DEPT:  
		<select name = "dept" >
			<?php $depts = mysql_query("SELECT * FROM departments ", $connection); 
			if (!$depts){
				echo ("database query failed: ". mysql_error());
			}?>
			
			<?php while ($dept = mysql_fetch_array($depts)) { ?>
			<option><?php echo $dept["dept_name"]; ?></option>
			<?php } ?>			
				
		</select></p>
		<p>DEGREE IN VIEW
		<input type = "text" id = "" name = "deg_in_view" /></p>
		<p>DATE OF FIRST REG:  <input type = "text" id = "" name = "initial_reg_date" /></p>
		<table id = "details">
			<tr>				
				<th>S/N</th>				
				<th>SESSION</th>			
				<th>AMOUNT DUE</th>
				<th>AMOUNT PAID</th>
				<th>RECEIPT NO.</th>
				<th>BALANCE</th>				
				
			</tr>
			
			<tr>
				<td><input type = "text" id = "" name = "payment_id" /></td>
				<td><input type = "text" id = "" name = "session" /></td>				
				<td><input type = "text" id = "" name = "amount_due" /></td>			
				<td><input type = "text" id = "" name = "amount_paid" /></td>
				<td><input type = "text" id = "" name = "receipt_no" /></td>
				<td><input type = "text" id = "" name = "balance" /></td>
			</tr>
				
			</table> <br/>
			 
				<p>LIASON OFFICER REMARK:  <input type = "text" id = "" name = "officer_remark" /></p>
				<p>DATE:
				<input type = "text" name = "date" value ="<?php echo date ("y/m/d"). '<br/>'; ?>" /></p>
				
				<input type = "submit" id = "" name = "submit" value = "submit" />
			
  	</form>

<body>
</html>
<?php mysql_close($connection);?>

Re: php learner: dont know what's wrong

Posted: Thu Dec 18, 2014 8:30 am
by Celauran
You're setting $name etc inside a conditional block, then running a query based on them outside of the block. This means the query will run even if $_POST['submit'] isn't set and, thereby, the variables aren't set.

Re: php learner: dont know what's wrong

Posted: Fri Dec 19, 2014 5:31 am
by chized
thanks, i'll try to upgrade, fix the problem and then reply

Re: php learner: dont know what's wrong

Posted: Tue Dec 23, 2014 4:01 pm
by chized
i ve upgraded, and tried to re-write my code, yet its not working. this error message is displayed

Could not enter data: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

Code: Select all

    		<?php
		if(isset($_POST['submit']))
		{
		$dbhost = 'localhost';
		$dbuser = 'root';
		$dbpass = '';
		$conn = mysql_connect($dbhost, $dbuser, $dbpass);
		if(! $conn )
		{
		  echo('Could not connect: ' . mysql_error());
		}

		if(! get_magic_quotes_gpc() )
		{
			
		   $name = addslashes ($_POST['name']);
		   $regno = addslashes ($_POST['regno']);
		   $faculty = addslashes ($_POST['faculty']);
		   $dept = addslashes ($_POST['dept']);
		   $deg_in_view = addslashes ($_POST['deg_in_view']);
		   $initial_reg_date = addslashes ($_POST['initial_reg_date']);
		   $payment_id = addslashes ($_POST['payment_id']);
		   $session = addslashes ($_POST['session']);
		   $amount_due = addslashes ($_POST['amount_due']);
		   $amount_paid = addslashes ($_POST['amount_paid']);
		   $receipt_no = addslashes ($_POST['receipt_no']);
		   $balance = addslashes ($_POST['balance']);
		   $officer_remark = addslashes ($_POST['officer_remark']);
		   $date = addslashes ($_POST['date']);
		   
		  }
		else
		{
		  // $emp_name = $_POST['emp_name'];
		   //$emp_address = $_POST['emp_address'];
		   $id = ($_GET['id']);
		   $name = ($_POST['name']);
		   $regno = ($_POST['regno']);
		   $faculty = ($_POST['faculty']);
		   $dept = ($_POST['dept']);
		   $deg_in_view = ($_POST['deg_in_view']);
		   $initial_reg_date = ($_POST['initial_reg_date']);
		   $payment_id = ($_POST['payment_id']);
		   $session = ($_POST['session']);
		   $amount_due = ($_POST['amount_due']);
		   $amount_paid = ($_POST['amount_paid']);
		   $receipt_no = ($_POST['receipt_no']);
		   $balance = ($_POST['balance']);
		   $officer_remark = ($_POST['officer_remark']);
		   $date = ($_POST['date']);
		}

		$sql = "INSERT INTO payhistory ".
			   "(name, regno, faculty, dept, deg_in_view, initial_reg_date, payment_id, session, amount_due, amount_paid, receipt_no, balance, officer_remark, date) ".
			   "VALUES('$name','$regno','$faculty','$dept','$deg_in_view','$initial_reg_date',$payment_id,'$session',$amount_due,$amount_paid,$receipt_no,$balance,'$officer_remark',$date)";
		mysql_select_db('pgschool');
		$retval = mysql_query( $sql, $conn );
		if(! $retval )
		{
		  die('Could not enter data: ' . mysql_error());
		}
		echo "Entered data successfully\n";
		echo '<a href ="basic.php">Enter new data</a>';

		}
		else
		{
		?>
		 <body>
			<div>
				
			<h3>PG Payment History form</h3>
			</div>
			<form action = "basic.php" name = "" method = "post">
				
				<p>NAME:  <input type = "text" id = "" name = "name" /></p>
				<p>REG NO:  <input type = "text" id = "" name = "regno" /></p>
				
				<p>FACULTY:  	<select name="faculty" >
				
							<option>SCIENCE</option>	
							<option>ENGINEERING</option>
							<option>AGRICULTURE</option>
							<option>ENVIRONMENTAL</option>
							<option>MANAGEMENT</option>
							<option>TECHNOOGY EDUCATION</option>				
				   
				</select></p>
				<p>DEPT:  
				<select name="dept" >
							<option>Computer Science</option>	
							<option>Civil Engineering</option>
							<option>Physics</option>
							<option>Information Technology</option>
							<option>Accounting Technology</option>
							<option> Chemistry</option>	
				</select></p>
				<p>DEGREE IN VIEW
				<input type = "text" id = "" name = "deg_in_view" /></p>
				<p>DATE OF FIRST REG:  <input type = "text" id = "" name = "initial_reg_date" /></p>
				<table id = "details">
					<tr>				
						<th>S/N</th>				
						<th>SESSION</th>			
						<th>AMOUNT DUE</th>
						<th>AMOUNT PAID</th>
						<th>RECEIPT NO.</th>
						<th>BALANCE</th>				
						
					</tr>
					
					<tr>
						<td><input type = "text" id = "" name = "payment_id" /></td>
						<td><input type = "text" id = "" name = "session" /></td>				
						<td><input type = "number" id = "" name = "amount_due" /></td>			
						<td><input type = "number" id = "" name = "amount_paid" /></td>
						<td><input type = "text" id = "" name = "receipt_no" /></td>
						<td><input type = "number" id = "" name = "balance" /></td>
					</tr>
					
					
					</table> <br/>
					 
						<p>LIASON OFFICER REMARK:  <input type = "text" id = "" name = "officer_remark" /></p>
						<p>DATE:
						<input type = "text" name = "date" value ="<?php echo date('m.d.y');?>" /></p>
						
						<input type = "submit" id = "" name = "submit" value = "submit" />
					
				</form>
			
		<?php } ?>
		<?php mysql_close($conn); ?>
		<body>
		</html>

Re: php learner: dont know what's wrong

Posted: Tue Dec 23, 2014 5:43 pm
by Celauran
The obvious mistakes I see are the date field; date is a keyword and needs back ticks if it's used as a column name, so you need `date` in your field list. You'll also need quotes around the date value itself.