Solved

insert from text box into mysql db

Posted on 2011-03-02
10
567 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

737 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