insert not working

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

AlexAmblerAsked:
Who is Participating?
 
Marco GasiConnect With a Mentor FreelancerCommented:
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
 
Aaron TomoskySD-WAN SimplifiedCommented:
Can you pass the echo $insert line?
0
 
Marco GasiFreelancerCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Marco GasiFreelancerCommented:
I confirm you have to check your values: tested in my database your code works fine and data are saved correctly.
0
 
AlexAmblerAuthor 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();
0
 
Marco GasiFreelancerCommented:
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
 
AlexAmblerAuthor 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
?
0
 
AlexAmblerAuthor 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

0
 
Marco GasiFreelancerCommented:
Yes :-)
0
 
AlexAmblerAuthor Commented:
Excellent, thank you very much.
0
 
Marco GasiFreelancerCommented:
Glad to help you. Bye
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.