MYSQL help required

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

MYSQL help required

Post by Live24x7 »

Have a mysql table which populates from a form using the code

mysql_query("INSERT INTO chapters VALUES ('$cname', '$clocation','$cbb','$cemail','$cmn','$cdt')");

When the form is filled for the first time, it get through into the table, but second entries onwards it does not populate the table.
The tables are getting populated perfectly well in the localhost but fail to update in the live environment.

Why is it getting thu in the first time but fails to update any subsequent entries ?

Any ideas whats cud be going wrong.?
:o
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: MYSQL help required

Post by Celauran »

See what this tells you.

Code: Select all

mysql_query("INSERT INTO chapters VALUES ('$cname', '$clocation','$cbb','$cemail','$cmn','$cdt')") or die(mysql_error());
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: MYSQL help required

Post by Live24x7 »

Thanks Celauran,

I get the following error: Duplicate entry '2147483647' for key 'PRIMARY'

FYI - my primary key is cmn (mobile number) and '2147483647' is the mobile number in the first row.
i get this error even if i enter any other mobile number (say 5555555555) in the mobile number field
if i remove mobile number as the primary key, the table updates with new data but replaces my any other number with '2147483647'

this has helped me get a bit closer at identifying the problem, but am clueless about the reason why this is happening. :roll:

Any ideas ?

my form code looks somewhat like this:

Code: Select all

<form name="cform" class='form' action='' method='POST' accept-charset='UTF-8' onsubmit="return rvalidateForm()">
				<label>Your Name </label> <input type='text' name='cname' maxlength="20" /> <br/>
				<label> Location</label> <?php include('statedropdown.php'); ?> <br/><div id="txtHint"><select name='clocation'><option value="">Select your City</option></select> </div>  <br/>
				<label>Does your Location have our branch </label>
				<select name='cbranch'> <br/><option value="">Select</option><option value="Yes">Yes</option> <option value="No">No</option><option value="dont know">Don't Know</option> </select>
				<label>Your Email</label>  <input type='text' name='cemail' maxlength="40" /><br/>
			<label>10 digit Mobile Number</label>  <input type='int' name='cmn' maxlength="10"/><br/>      
                     
				<input class='button' type='submit' name='submit' value='SUBMIT'/>    
				</form>	<!-- Form Code Ends --> 		
				<!-- Form JS Validation Start -->
				 <script type="text/javascript"> function rvalidateForm() { var x=document.forms["cform"]["cname"].value; if (x==null || x=="")  { alert("Name must be filled out");  return false; }var x=document.forms["cform"]["rstate"].value; if (x==null || x=="" ) { alert("Select your state");  return false; } var x=document.forms["cform"]["cbranch"].value; if (x==null || x=="")  { alert("Please Answer if your location has a blood bank");  return false; } var x=document.forms["cform"]["cemail"].value; var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {alert("Not a valid e-mail address");  return false; } var x=document.forms["cform"]["cmn"].value; if (x==null || x=="" || x.length>10 || x.length<10 || !x.toString().match(/^[-]?\d*\.?\d*$/)) {alert("Mobile number must be valid");  return false; } var y=document.forms["cform"]["cmn"].value; if (y.toString().match(/^0+[^0]+/)) { alert("Please do not enter 0 in the beginning of your mobile number");  return false; } } </script>
				<!-- Form JS Validation Ends -->
				<?php if (isset($_POST['submit'])) 
				{ 		
				
				$cname= $_POST['cname']; $clocation= $_POST['clocation']; 	$cbranch = $_POST['cbranch']; 	$cemail= $_POST['cemail']; 	$cmn= $_POST['cmn']; 
				
				// php side form validation begins
				function check_input($data) 
				{ $data = trim($data); $data = strip_tags($data); $data = stripslashes($data); $data = htmlspecialchars($data); $data = mysql_real_escape_string($data); 
				return $data; } //custom function to trim, stripslash and remove html chars
				$cname= check_input($_POST['cname']);  $cbranch = check_input($_POST['cbranch']); 	$cemail= check_input($_POST['cemail']); $cmn= check_input($_POST['cmn']); 	$clocation= check_input($_POST['clocation']); $problem="";
				$br="<br/>"; //line break uh !
				if(empty($cname)||empty($cbranch) || empty($cmn) || empty($clocation) || empty($cemail))  $problem ="All fields must be filled"; echo "$problem". "$br"; 
				if (!preg_match('/^[a-zA-Z]/', $cname) )  $problem ="Please fill valid name" ; echo "$problem". "$br"; 
				if (!preg_match('/([\w\-]+\@[\w\-]+\.[\w\-]+)/', $cemail)) $problem ="Please fill a valid email address"; echo "$problem". "$br"; 
				if (!preg_match('/^[0-9]{10}$/', $cmn)) $problem ="Please fill a valid Mobile number"; echo "$problem". "$br"; 
				if (empty($problem))  //if no problems insert into db and redirect to thankyou page
				{ require_once ('connect.php');
				//php final filters sanitization format is filter_var(variable, filter, options) -NOT WORKING AS IT SHOULD
					filter_var("$cname", FILTER_SANITIZE_STRIPPED);
					filter_var("$cemail", FILTER_VALIDATE_EMAIL);  
					filter_var("$cmn", FILTER_SANITIZE_NUMBER_INT);
						//working out the current date time
					date_default_timezone_set('Asia/Calcutta');
					$cdt=date("y.m.d , h:i:s");
				// Insert into DB after sanitization
					mysql_query("INSERT INTO chapters VALUES ('$cname', '$clocation','$cbranch','$cemail','$cmn','$cdt')") or die(mysql_error()); 
					?><script type="text/javascript"> <!--
					window.location = "cthankyou.php"//–></script><?php	
					}}?>
								

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: MYSQL help required

Post by Celauran »

Have you tried inserting into the database manually? That will at least tell us if it's a db problem or a problem with your PHP code.
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: MYSQL help required

Post by Live24x7 »

@ Celauran thanks,

As u suggested, I tried inserting manually into the db and it worked well. that means my database is fine :)
but if i go through the form i still get the same error - "Duplicate entry '2147483647' for key 'PRIMARY'"

So, now about php .. i have included the code above, and i dont see a reason why it should insert a constant number, that too picking it up from the first entry into the form :(
qeemat
Forum Newbie
Posts: 10
Joined: Sat Feb 18, 2012 1:12 am

Re: MYSQL help required

Post by qeemat »

learn from this code it is also insert query
<?php
// Make a MySQL Connection
mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

// Insert a row of information into the table "example"
mysql_query("INSERT INTO example
(name, age) VALUES('Timmy Mellowman', '23' ) ")
or die(mysql_error());

mysql_query("INSERT INTO example
(name, age) VALUES('Sandy Smith', '21' ) ")
or die(mysql_error());

mysql_query("INSERT INTO example
(name, age) VALUES('Bobby Wallace', '15' ) ")
or die(mysql_error());

echo "Data Inserted!";

?>
Post Reply