Problem inserting data to MySQL

I have a 2-page table where the data from that table will then be inserted into another 2-page table and also inserted into the MySQL database. Everything inserts into the database if all fields have something entered into the table. However, if I enter only the first few fields (Lastname, Firstname, StudentID…which will sometimes be the case), nothing gets inserted into the db at all.

Of note, Lastname, Firstname, StudentID and Date all appear in the first few fields of both forms. Date is pre-populated to today’s date.

Can anyone give me any idea why this would happen? Any help is greatly appreciated!

Here is my insert code.[php]

include "MySQLConnector.txt";

// Get the data from the form:
$Lastname = trim($_REQUEST[‘lastname’]);
$Firstname = trim($_REQUEST[‘firstname’]);
$StudentID = trim($_REQUEST[‘studentid’]);
$Credtran = trim($_REQUEST[‘credtran’]);
$Credleft = trim($_REQUEST[‘credleft’]);
$Major = trim($_REQUEST[‘major’]);
$Comments = trim($_REQUEST[‘comments’]);
$Advisorlast = trim($_REQUEST[‘advisorlast’]);
$Advisorfirst = trim($_REQUEST[‘advisorfirst’]);
$Date = trim($_REQUEST[‘date’]);

if (isset($_REQUEST['rdoPrereq'])) {
	$Prereq = $_REQUEST['rdoPrereq'];
	if ($Prereq == 1) {
  	} elseif ($Prereq == 2) {
  	}	
} else {
	$Prereq = NULL;
}

// Echo back the data from the form to be sure it’s coming back
/*
echo ’

   ’ . $Lastname . ‘’;
echo ’   ’ . $Firstname . ‘’;
echo ’   ’ . $StudentID . ‘’;
echo ’   ’ . $Credtran . ‘’;
echo ’   ’ . $Credleft . ‘’;
echo ’   ’ . $Major . ‘’;
echo ’   ’ . $Comments . ‘’;
echo ’   ’ . $Advisorlast . ‘’;
echo ’   ’ . $Advisorfirst . ‘’;
echo ’   ’ . $Date . ‘’;
echo ’   ’ . $Prereq . ‘’;
*/
$query = "INSERT INTO finaidevaluation (FormID, Lastname, Firstname, StudentID, Credtran, Credleft, Major, Comments, Advisorlast, Advisorfirst, Date, Prereq) 
		  VALUES (NULL, '" . $Lastname . "', '" . $Firstname . "', '" . $StudentID . "', '" . $Credtran . "', '" . $Credleft . "', '" . $Major . "', '" . $Comments . "', '" . $Advisorlast . "', '" . $Advisorfirst . "', '" . $Date . "', '" . $Prereq. "')" ;
	
	
mysqli_query($dbc,$query);


mysqli_close($dbc);

?>

Academic Evaluation
	<script type="text/javascript">
		function printpage()
		{
		window.print()
		}
	</script>
	
	<style type="text/css">.break { page-break-before: always; }</style>
<?php $Lastname = $_REQUEST['lastname']; $Firstname = $_REQUEST['firstname']; $StudentID = $_REQUEST['studentid']; $Date = $_REQUEST['date']; $Credtran = $_REQUEST['credtran']; $Credleft = $_REQUEST['credleft']; $Major = $_REQUEST['major']; $Comments = $_POST['comments']; $Advisorfirst = $_REQUEST['advisorfirst']; $Advisorlast = $_REQUEST['advisorlast']; if (isset($_REQUEST['rdoPrereq'])) { $Prereq = $_REQUEST['rdoPrereq']; if ($Prereq == 1) { $Prereq = "Yes"; } elseif ($Prereq == 2) { $Prereq = "No"; } } else { $Prereq = ""; }[/php]

Would it be better to use _POST than _REQUEST ?

ANY how you should put a mysql error on the end to see if it tells you why it wont work.

[php] $query = “INSERT INTO finaidevaluation (FormID, Lastname, Firstname, StudentID, Credtran, Credleft, Major, Comments, Advisorlast, Advisorfirst, Date, Prereq)
VALUES (NULL, '” . $Lastname . “’, '” . $Firstname . “’, '” . $StudentID . “’, '” . $Credtran . “’, '” . $Credleft . “’, '” . $Major . “’, '” . $Comments . “’, '” . $Advisorlast . “’, '” . $Advisorfirst . “’, '” . $Date . “’, '” . $Prereq. “’)”
or die(mysqli_error()); [/php]

Also how are you cleaning the requested results?

What are you using to sanitize the $_REQUEST with ?
Are you using mysqli_real_escape_string() function strip_slashers Function ?

As this might be the problem you are just sending anything to the mysql which it will not like some chars need to be escaped before they can be entered in to the mysql.

[php]
$Lastname = trim($_REQUEST[‘lastname’]);
[/php]

[php]
$Lastname = trim(mysqli_real_escape_string($_REQUEST[‘lastname’]));
[/php]

Thanks. I put the mysql error code you suggested but see an error. Where would that error show up? It’s very strange. If I enter just the name and studentid in both forms (date is populated in both) and also enter the ‘prereq’ ‘credtran’ and ‘credleft’ it goes into the db. But if I just enter the name and studentid on both forms it doesn’t. And when I use the echo to bring back the data from the form, it is bringing it all back; just not inserting it into the db.

It would show up on the page the code is on

Maybe the problem is in the forms

Any idea what could cause that? Would the fact that I have an input form that contains 2 fields each for name, studentid and date make a difference? The name and studentid fields are manually entered twice (exactly the same in each field) and the date fields are both populated with today’s date. However, for the “insert” php file I only pull one of each of the fields. It does pull it in okay (as I stated earlier with using the echo). It just seems that if it’s pulling the data in, it should go into the db. If there were a problem with a form, which form would likely be the problem…the input form or output form?

Thanks so much for your help!

if all variables are set befoe entering the db then maybe show me what your trying to enter as it could be anything like i said some chars need escaping

Or it might be on this part of the query that needs the error message

[php]mysqli_query($dbc,$query) or die (mysqli_error($dbc));[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service