Solved

insert from text box into mysql db

Posted on 2011-03-02
10
554 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
Comment Utility
I guess you need to configure database access eg. host, database name, database user and database password.
0
 

Author Comment

by:AlexAmbler
Comment Utility
hi,
database connection is in the header.

0
 
LVL 3

Expert Comment

by:wwwdeveloper2
Comment Utility
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
Comment Utility
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
Comment Utility
This is misplaced. Input text fields are outside the form, move then between <form> and </form>
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thank you.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.

771 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

10 Experts available now in Live!

Get 1:1 Help Now