Solved

insert from text box into mysql db

Posted on 2011-03-02
10
561 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

774 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