We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

insert not working

AlexAmbler
AlexAmbler asked
on
Medium Priority
166 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

Comment
Watch Question

Aaron TomoskyDirector, SD-WAN Solutions
CERTIFIED EXPERT

Commented:
Can you pass the echo $insert line?
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
I confirm you have to check your values: tested in my database your code works fine and data are saved correctly.

Author

Commented:
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();
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

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

Author

Commented:
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
?
Freelancer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

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

Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Yes :-)

Author

Commented:
Excellent, thank you very much.
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Glad to help you. Bye
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.