trouble inserting into my data base

This process page was working to insert info into my data base

Page 1 of 1

4 Replies - 722 Views - Last Post: 15 October 2010 - 09:49 AM Rate Topic: -----

#1 bgile  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 29-September 09

trouble inserting into my data base

Posted 12 October 2010 - 07:35 AM

I was using this process page to insert info into my data base now it will not work it stopped putting the contact info from my contact page into the data base. All help greatly appreciated. Thanks.
  <html>
	              <head>
	                <title>Processing form</title>
	                
	                <style>
	                    h1   {color:#000080; font-size:35px}
	                </style>
	              </head>
	             
	              <body>
			    <?php    
	                 
	                     $first_name = stripslashes($_POST['first_name']);
	                     $last_name = stripslashes($_POST['last_name']);
				         $address=$_POST['address']; 
				         $city=$_POST['city']; 
				         $state=$_POST['state'];
				         $zip=$_POST['zip'];
				         $area=$_POST['area'];
				         $exchange=$_POST['exchange'];
				         $phone=$_POST['phone'];
				         $email=$_POST['email'];
				         $contact=$_POST['contact'];
				         $services=$_POST['services'];
				         $comments=$_POST['comments'];
				          mysql_connect("localhost", "####", "######") or die(mysql_error()); 
				          mysql_select_db("harts") or die(mysql_error());
				          mysql_query("INSERT INTO contact_info VALUES ('$id','$first_name', '$last_name','$address', '$city', '$state', '$zip','$area','$exchange', '$phone', '$email', '$contact', '$services', '$comments', NOW())");
				          ?>
			            <h1>Your information has been successfully submitted .<br />
			                 We will contact you as soon as possible.</h1> 
			             
			            </body>
			            </html>
:helpsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: trouble inserting into my data base

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6076
  • View blog
  • Posts: 23,543
  • Joined: 23-August 08

Re: trouble inserting into my data base

Posted 12 October 2010 - 07:43 AM

Yet another case of always assuming your query succeeds. DON'T DO THAT!

$query = "INSERT INTO contact_info VALUES ('$id','$first_name', '$last_name','$address', '$city', '$state', '$zip','$area','$exchange', '$phone', '$email', '$contact', '$services', '$comments', NOW())";
if(!mysql_query($query))
{
    die("Query $query failed: " . mysql_error());
}


Also, big fail on allowing unsanitized user input into your query. You need to to mysql_real_escape_string() on any user input to make sure it's clean before presenting it to the database.

EDIT: Oops, forgot the $query in mysql_query()

This post has been edited by JackOfAllTrades: 12 October 2010 - 07:52 AM

Was This Post Helpful? 1
  • +
  • -

#3 xerxes333  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 504
  • Joined: 05-July 07

Re: trouble inserting into my data base

Posted 12 October 2010 - 07:44 AM

Try assigning your query to a string $query = "insert into ...." and then echo it to make sure there is not something in the string messing things up (like sql injections). Also does mysql_query return true or false? Also its goo practice to explicitly indicate which columns you are inserting data into (ie: insert into my_table (col1, col2, col3) values (val1, val2, val3)
Was This Post Helpful? 1
  • +
  • -

#4 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,299
  • Joined: 22-October 08

Re: trouble inserting into my data base

Posted 12 October 2010 - 01:29 PM

Ok make sure that the form is using the "post" method not "get". As explained dont always assume your query will succseed. also with you database table structure is the id column an auto increment? you miht be trying to insert a row with the same id.
Also the variable id is not specified, thus will not execute
Thanks Aaron1178
Was This Post Helpful? 1
  • +
  • -

#5 bgile  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 29-September 09

Re: trouble inserting into my data base

Posted 15 October 2010 - 09:49 AM

Thanks for all the great help this is what I finally came up with thanks. And it works.
 <html>
              <head>
                <title>Processing form</title>
                
                <style>
                    h1   {color:#000080; font-size:35px}
                </style>
		              </head>
		             
		                 <?php
							$con = mysql_connect("localhost","#####","#####");
							if (!$con)
							  {
							  die('Could not connect: ' . mysql_error());
							  }
							
							mysql_select_db("#####", $con);
							
							$sql="INSERT INTO contact (id, first_name, last_name, address, city, state, zip, area, exchange, phone, email, contact, services, comments)
							VALUES
							('$_POST[id]','$_POST[first_name]','$_POST[last_name]','$_POST[address]','$_POST[city]','$_POST[state]','$_POST[zip]','$_POST[area]','$_POST[exchange]','$_POST[phone]','$_POST[email]','$_POST[contact]','$_POST[services]','$_POST[comments]')";
							
							if (!mysql_query($sql,$con))
							  {
							  die('Error: ' . mysql_error());
							  }
							echo "1 record added";
							
							mysql_close($con)
		                     ?> 
				            <h1>Your information has been successfully submitted .<br />
				                 We will contact you as soon as possible.</h1> 
				             
				            </body>
				            </html>

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1