6 Replies - 233 Views - Last Post: 01 February 2012 - 04:20 PM Rate Topic: -----

Topic Sponsor:

#1 squibby  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 59
  • Joined: 21-January 12

Some variables not accepted by insert query

Posted 30 January 2012 - 09:33 PM

I have an input form to take student details and insert into a mysql database. The following forms processes the forms data. I put all the POST data in variables and then put those variables in the query.

I had no problems with it until i added some javascript methods of date entry on the 2 dates represented by variables $joindate and $birthdate.

After running this page and checking inside php myadmin, all variables go into the database except these 2. It must have something to do with them using javascript. The strange thing is that the variables do hold values, as when i echo them out in the table on the page, they show the correct values that i put in the form.

So my question is why does this happen, and how to make the database accept the variables??


<?php
include('header.php');
include('auth.php');
 ?>

<div id="content">
	<div id="logbox">
		<b>You are logged in as:&nbsp;<font color=#00CC00><?php  echo  $_SESSION['myusername'];?></font></b>
	</div>
	<div id="contentleft">

		<h2>Added Succesfully!</h2>

		<?php
			$chinesename = $_POST['chinesename'];
			$englishname = $_POST['englishname'];
			$gender = $_POST['gender'];
			$courseconsultant = $_POST['courseconsultant'];
			$phonenumber = $_POST['phonenumber'];
			$email = $_POST['email'];
			$source = $_POST['source'];
			$birthday = $_POST['birthday'];
			$joindate = $_POST['joindate'];
			$notes = $_POST['notes'];
			$image = $_FILES['file']['name'];
			$tmp_name = $_FILES['file']['tmp_name'];

			include ('connect.php');
			mysql_query("set names utf8");

			$query = "INSERT INTO students(STU_ID,STU_Chinese_Name,STU_English_Name,STU_Gender,STU_Course_Consultant,STU_Phone_Number,STU_Email,STU_Source,STU_Notes,STU_Picture,STU_Birthday,STU_joindate) VALUES ('NULL','$chinesename','$englishname','$gender','$courseconsultant','$phonenumber','$email','$source','$notes','$image','$birthday','$joindate')";

			mysql_query($query) or die ('Error updating database');
		?>

		<html>
			<body>
				<p>The following student profile has been entered into the database:</p>
					<table class ="gridtable">
						<tr>
							<td>Chinese Name
							<td><font color=#00CC00><?php echo $chinesename;?></td></font>
						</tr>
						<tr>
							<td>English Name</td>
							<td><font color=#00CC00><?php echo $englishname;?></td></font></font>
						</tr>
						<tr>
							<td>Gender</td>
							<td><font color=#00CC00><?php echo $gender;?></td></font>
						</tr>
						<tr>
							<td>Course Consultant</td>
							<td><font color=#00CC00><?php echo $courseconsultant;?></td></font>
						</tr>
						<tr>
							<td>Phone Number</td>
							<td><font color=#00CC00><?php echo $phonenumber;?></td></font>
						</tr>
						<tr>
							<td>Email</td>
							<td><font color=#00CC00><?php echo $email;?></td></font>
						</tr>
						<tr>
							<td>Source</td>
							<td><font color=#00CC00><?php echo $source;?></td></font>
						</tr>
						<tr>
							<td>Birthday</td>
							<td><font color=#00CC00><?php echo $birthday;?></td></font>
						</tr>
						<tr>
							<td>Join Date</td>
							<td><font color=#00CC00><?php echo $joindate;?></td></font>
						</tr>
						<tr>
							<td>Notes</td>
							<td><font color=#00CC00><?php echo $notes;?></td></font>
						</tr>
						<tr>
							<td>Image</td>
							<td><font color=#00CC00><?php echo $image;?></td></font>
						</tr>
					</table> 
						</br>



			<fieldset>
			<h2>What do you want to do next?</h2>
			<br>
			<a href="menu_addinfo.php">Add another student</a><br>
			<a href="form_addstudent.php">Delete this record</a><br>
			<a href="form_addstudent.php">Add a transaction</a><br>
			</fieldset>

	</div>
	<?php include('sidebar.php'); ?>
</div>
<?php include('footer.php'); ?>



Is This A Good Question/Topic? 0
  • +

Replies To: Some variables not accepted by insert query

#2 no2pencil  Icon User is online

  • 2 girls, 1 club
  • member icon

Reputation: 3061
  • View blog
  • Posts: 22,961
  • Joined: 10-May 07

Re: Some variables not accepted by insert query

Posted 30 January 2012 - 09:45 PM

You are not doing any error checking on the POST array. It's difficult (if not impossible) to tell you what's happening just from viewing your code.

I would test it with this :

$query = "INSERT INTO students(STU_ID,STU_Chinese_Name,STU_English_Name,STU_Gender,STU_Course_Consultant,STU_Phone_Number,STU_Email,STU_Source,STU_Notes,STU_Picture,STU_Birthday,STU_joindate) VALUES ('NULL','$chinesename','$englishname','$gender','$courseconsultant','$phonenumber','$email','$source','$notes','$image','$birthday','$joindate')";

$result=mysql_query($query);
if(!$result) die("The following SQL failed<br>$query");


Was This Post Helpful? 1
  • +
  • -

#3 squibby  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 59
  • Joined: 21-January 12

Re: Some variables not accepted by insert query

Posted 30 January 2012 - 11:04 PM

hi thanks no2pencil, i did as you suggested but there was no error and no difference.

All the variables were echoed out as normal, and a check to the database showed no variables being entered for $birthday and $joindate. is strange.
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • 2 girls, 1 club
  • member icon

Reputation: 3061
  • View blog
  • Posts: 22,961
  • Joined: 10-May 07

Re: Some variables not accepted by insert query

Posted 30 January 2012 - 11:27 PM

Can you take your mysql insert output text from php & plug it into mysql in a manual interface, such as the command prompt or mysqladmin?

It's possible that one of the types are not defined correctly.
Was This Post Helpful? 1
  • +
  • -

#5 squibby  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 59
  • Joined: 21-January 12

Re: Some variables not accepted by insert query

Posted 30 January 2012 - 11:47 PM

INSERT INTO students(STU_ID,STU_Chinese_Name,STU_English_Name,STU_Gender,STU_Course_Consultant,STU_Phone_Number,STU_Email,STU_Source,STU_Notes,STU_Picture,STU_Birthday,STU_joindate) VALUES 

('NULL','wang','david','male','34','12345678','abc@efg.com','1','some notes','','2000/3/3','2010/3/3')



i put this code in via phpmyadmin. it all went in no problem.(i left the image file out)

so no problem with any data types. but like i said these 2 dates are being entered in the form via a javascript datepicker. thats the difference between these fields and the others. But as they are echoed out in the form after being put into variables i dont see why they cant go into the query. can javascript things interfere with anything like this?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is online

  • No Sugar Coding Here!
  • member icon

Reputation: 4684
  • View blog
  • Posts: 20,362
  • Joined: 23-August 08

Re: Some variables not accepted by insert query

Posted 31 January 2012 - 05:49 AM

What is the type of these fields?

STU_Birthday,STU_joindate


Assuming STU_ID is an autoincrementing field, I would remove it from the INSERT statement; the id will be autogenerated.

Can also turn on SQL logging and see what errors are reported.
Was This Post Helpful? 1
  • +
  • -

#7 squibby  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 59
  • Joined: 21-January 12

Re: Some variables not accepted by insert query

Posted 01 February 2012 - 04:20 PM

ok i got it working. the problem was that the date that jquery the jquery date picker generated was in the wrong format. the year was at the end instead of the beginnning. I think it still should have shown an error when putting it in but anyway its done now, i understand the jquery settings better now as that was my first time of using it.

Thanks to everyone.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1