?
Solved

insert not working

Posted on 2011-04-24
11
Medium Priority
?
153 Views
Last Modified: 2012-05-11
I have the code below to insert two text fields in to a mysql db, when the submit button is clicked no data is saved in the db, if the text fields are empty the input check is not displaying the error message,
All the database connections are contained in the header file, these work as other parts of the web site save to the database ok.

Am I missing something?

<div id="content_wrapper">
<br/>
	<div class="onecolumn_wrapper">
	 <div class="onecolumn" style="margin-right:20%; margin-left:20%;">
	  <div class="content" >
      
		    <?php	

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

		{ 

		//makes sure no blank fields 
		if (!$_POST['Name'] | !$_POST['Address'] ) 
		{
		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 teamdate
					(
					 Name,
					 Address
					 )
           VALUES  (
					'".$_POST['Name']."',
					'".$_POST['Address']."'
					)";
		   	
		//print_r($_POST); 
        $add_member = mysql_query($insert);
        //echo $insert."<br>"; exit; 
        //$res = mysql_query($sql);

?>  


    <h1>Saved</h1>
    <p>Thank you, new details saved</a>.</p>

<?php 
		} 
		else 
		{ 
?>    
   	<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <h1> </h1>
    <table class="general">
        <tr>
          <td colspan="2"><div align="left">Name</div></td>
          <td colspan="3"><div align="left">
            <input type="text" name="Name" id="Name" />
          </div></td>
          
          <td colspan="2"><div align="left">Address</div></td>
          <th width="300" colspan="2"><div align="left">
            <input type="text" name="Address" id="Address" />
          </div></th>
        </tr> 
	</table>

	<input name="submit" type="submit" class="inputbox2_button" id="submit" value="Save Team Details" />
      <br />
      </ul>
     </form>
    <?php

}

?>

</div>                                    
            </p> 

			
		  </div>
		 </div>
		</div>
	</div>
	<br/>
</div>

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

Open in new window

0
Comment
Question by:AlexAmbler
  • 6
  • 4
11 Comments
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 35456489
Can you pass the echo $insert line?
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 35456614
I tested your code echoing $insert (but without running a query) and the string printed seems to be correct. The only thing I can think is that in your Address value there is some character which make sql fails, a quote, for instance... Check that value, please or post here tha full $insert string.

Cheers
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 35456620
I confirm you have to check your values: tested in my database your code works fine and data are saved correctly.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:AlexAmbler
ID: 35457339
the code is still not working, once the subit is clicked I now see an error an the bottom of the page

Warning: mysql_close(): no MySQL-Link resource supplied in C:\inetpub\vhosts\0176\web\main_page.php on line 168

line 168 is
mysql_close();
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 35458180
mysql_close isn't necessary, since non-persistent links are automatically closed. If you are using a non-persitent connection, simply don't use mysql_close(): tha last opened mysql connection will be automatically closed.

Cheers
0
 

Author Comment

by:AlexAmbler
ID: 35459782
I think it is something to do with <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
When I view the source from the browser
<form action="/0176/web/main_page.php" method="post">

But the address is /0176/web/main_page.php?p=5&q=4&btn=1
?
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 total points
ID: 35459868
You can't use get parameters in a post call. If you need to pass some parameter, you ahve to add them as hidden fields to your form:

<form action="/0176/web/main_page.php" method="post">
<input type="hidden" name="p" value="5" />
<input type="hidden" name="q" value="4" />
<input type="hidden" name="btn" value="1" />

Then you'll retrieve that values this way:

$p = $_POST['p'];
$q = $_POST['q'];
$btn= $_POST['btn'];

Cheers

0
 

Author Comment

by:AlexAmbler
ID: 35469130
Hi,

Something like this?

Regards,
<div id="content_wrapper">
<br/>
	<div class="onecolumn_wrapper">
	 <div class="onecolumn" style="margin-right:20%; margin-left:20%;">
	  <div class="content" >
      
    <?php	

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

		{ 
		
		$p = $_POST['p'];
		$q = $_POST['q'];
		$btn= $_POST['btn'];
		
		//makes sure no blank fields 
		if (!$_POST['Name'] | !$_POST['Address'] ) 
		{
		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 teamdates
					(
					 Name,
					 Address
					 )
           VALUES  (
					'".$_POST['Name']."',
					'".$_POST['Address']."'
					)";
		   	
		//print_r($_POST); 
        $add_member = mysql_query($insert);
        //echo $insert."<br>"; exit; 
        //$res = mysql_query($sql);

?>  


    <h1>Saved</h1>
    <p>Thank you, new details saved</a>.</p>

<?php 
		} 
		else 
		{ 
?>    
    <form action="/0176/web/main_page.php" method="post">
	<input type="hidden" name="p" value="5" />
	<input type="hidden" name="q" value="4" />
	<input type="hidden" name="btn" value="1" />
    <h1> </h1>
    <table class="general">
        <tr>
          <td colspan="2"><div align="left">Name</div></td>
          <td colspan="3"><div align="left">
            <input type="text" name="Name" id="Name" />
          </div></td>
          
          <td colspan="2"><div align="left">Address</div></td>
          <th width="300" colspan="2"><div align="left">
            <input type="text" name="Address" id="Address" />
          </div></th>
        </tr> 
	</table>

	<input name="submit" type="submit" class="inputbox2_button" id="submit" value="Save Team Details" />
      <br />
      </ul>
     </form>
    <?php

}

?>

</div>                                    
            </p> 

			
		  </div>
		 </div>
		</div>
	</div>
	<br/>
</div>

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

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 35469295
Yes :-)
0
 

Author Closing Comment

by:AlexAmbler
ID: 35476151
Excellent, thank you very much.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 35476345
Glad to help you. Bye
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.
Suggested Courses
Course of the Month15 days, 1 hour left to enroll

840 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