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
Solved

insert from text box into mysql db

Posted on 2011-03-02
10
564 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 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