Solved

insert from text box into mysql db

Posted on 2011-03-02
10
558 Views
Last Modified: 2013-12-13
Hi,
I am trying to insert data from a text box in to a mysql database, I run the code below and nothing is inserted to the db, when I use
echo $insert."<br>"; exit;
it should the values are empty, could you please advise what code I am missing?

Thank you

<!-- Begin one column box -->
<div class="onecolumn">
	<div class="header">
		<h2><?= _fanbase;?></h2>
		
		<!-- Begin 2nd level tab -->
		<ul class="second_level_tab">
			<li>
				<a href="?p=9" class="button_dark">
					<?= _back;?>
				</a>
			<li/>
		</ul>
		<!-- End 2nd level tab -->
		
	</div>
	
	
	<div id="content_wrapper">
	<br/>
		<div class="onecolumn_wrapper">
		 <div class="onecolumn" style="margin-right:20%; margin-left:20%;">
		  <div class="content" >
		    <table class="general">
		      <tr>
		        <th><strong></strong></th>
                
	          </tr>
		    </form>
		    </table>
		    <?php	

		//code runs if the form has been submitted
		if (isset($_POST['submit'])) 

		{ 

		//makes sure no blank fields 
		//if (!$_POST['first_name'] | !$_POST['last_name'] ) 
		//{
		//die('You did not complete all of the required fields');
		//}
		
		//insert into the database
		$_POST = array_map('mysql_real_escape_string',  $_POST);   

        $insert  =  "INSERT INTO subscribe
					(
					 first_name,
					 last_name
					 )
           VALUES  (
					'".$_POST['first_name']."',
					'".$_POST['last_name']."'
					)";
		   
		//print_r($_POST); 
		echo $insert."<br>"; exit; 
        $add_member =   mysql_query($insert);
		//$res = mysql_query($sql);

?>
    <h1>Registered</h1>
    <p>Thank you, contact details saved</a>.</p>
    <?php 
		} 
		else 
		{ 
?>
    <h1>details</h1>
    <table class="general">
        <tr>
          <td width="109" colspan="2"><div align="left">First Name</div></td>
          <td width="151"><div align="left">
            <input type="text" name="first_name" id="first_name" />
          </div></td>
          
          <td width="143"><div align="left">Last Name</div></td>
          <th width="151"><div align="left">
            <input type="text" name="last_name" id="last_name" />
          </div></th>
          
        </tr><td width="109"></div>
        </tr>
      </form>
    </table>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <input name="submit" type="submit" class="inputbox2_button" id="subscribe" value="Save Guest Details" />
      <br />
        <ul class="register_form">


      </ul>
      <!-- end #mainContent -->
    </form>
    <?php
}

?>

</div>     
        
                                         
            </p> 
			<!-- close table -->
			
			
		  </div>
		 </div>
		</div>
	</div>
	<br/>
</div>

<br class="clear"/><br/>

Open in new window

0
Comment
Question by:AlexAmbler
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 4

Expert Comment

by:te-edu
ID: 35023333
I guess you need to configure database access eg. host, database name, database user and database password.
0
 

Author Comment

by:AlexAmbler
ID: 35023460
hi,
database connection is in the header.

0
 
LVL 3

Expert Comment

by:wwwdeveloper2
ID: 35023603
Here is an example mysql php connection.  Put this in the top of your page and edit to include your information:

<?php
//Connect To Database
$hostname='your_dbhostname';
$username='db_username';
$password='dbpassword';
$dbname='your_dbname';

mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);
?>

I hope this helps.  If you need more information, let me know.
0
 
LVL 14

Expert Comment

by:spiderfix
ID: 35024450
Viewing the connection code would help.
You can replace actual server, user, pw, and db info for generalized statements.

From the code you posted I don't see a database called for. I see a table "subscribe" but no database.

If the db is in the header then we can’t see that without the header.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35024787
This is misplaced. Input text fields are outside the form, move then between <form> and </form>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35024790
Or move the line 87:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
to line 70, that's it.
0
 

Author Comment

by:AlexAmbler
ID: 35073483
The connection to the db is in the header to this is not the issues,
I have move the   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
from line 87 to 70, but this does not help?
0
 
LVL 2

Accepted Solution

by:
eZov earned 250 total points
ID: 35106761
You have HTML tag          </form> on three places. You have to remove it from 29 and 86.
It has to remain just on line 96:

      </ul>
      <!-- end #mainContent -->
    </form>
0
 
LVL 2

Expert Comment

by:eZov
ID: 35107116
There is another fault in the script:
            //print_r($_POST);
            echo $insert."<br>"; exit;
        $add_member =   mysql_query($insert);
            //$res = mysql_query($sql);
it has to be:
            //print_r($_POST);
        $add_member =   mysql_query($insert);
            echo $insert."<br>"; exit;
            //$res = mysql_query($sql);

exit is before insert, and therefore sql insert never happens.
0
 

Author Closing Comment

by:AlexAmbler
ID: 35123816
thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now