$_POST or $_GET when to use?

Hi gang,

I have a question, or three.

I am taking data from a form and inserting it into mysql, (script done and working… :)). I have the form posting to insert.php and I have a link taking the user back to the form page. What I want to do is land the user back on the form page with the new data desplayed on the page to.

ON the html form I am posting to insert.php, I understand I need to use [php]<?php $_PHP_SELF ?>[/php] to land on the same page, am I right here?

Now do I use [php]$_GET[/php] or [php]$_POST[/php]

Thanks for your help.

essenz

I didn’t get your question. :’(

When the user insert the data into the FORM and hits submit he is redirected to insert.php, Now You want to redirect user back to the HTML FORM page??? Is there any HTML output on the insert.php file or is it pure PHP (only processing the information)???

Regarding the question about GET and POST. The general rule is to user POST when you want to insert data into the databases and use GET when you only want to retrieve (fetch) the information from the databases.

OK now I understand the $_GET or $_POST idea.

On the insert.php page there is an html link back to the form page:

[php]echo “

Back to main page

”;[/php]

When the user “Submit” the form it lands on “insert.php”, then need to click on the above link to go back and enter more records. The goal is to stay on the html form page.

assetdetails.php

[php]

		<head>
					<title>Asset Register</title>
		</head>
Insert Asset Details Into Register Database
Make :
Model :
Asset Number :
Registration :
[/php]

insert.php

[php]<?php

	 include 'header.html';

	 include 'link.inc'; 

				 // Get values from form 
	 $make=$_POST['make'];
	 $model=$_POST['model'];
	 $assetnum=$_POST['assetnum'];
	 $registration=$_POST['registration'];


				 // Insert data into mysql 
	 $sql="INSERT INTO assets (make, model, assetnum, registration)VALUES('$make', '$model', '$assetnum', '$registration')";
	 			if (!mysql_query($sql, $link))
	 {
	 die('Error: ' . mysql_error($link));
 }


	 			echo "<p><br>Good skills bro<br>";


	 			echo "<br><a href='assetdetails.php'>Back to main page</a></p>";
	 
	 			 //list assets in the register
		$sql = 'SELECT make, model, assetnum FROM assets';

		mysql_select_db('assets');
		$result = mysql_query( $sql, $link );
				if(! $result )
				{
  die('Could not get data: ' . mysql_error());
			  }
				while($row = mysql_fetch_array($result, MYSQL_ASSOC))
				{

				echo "<p>Make : {$row['make']}  <br> ".
				     "Model : {$row['model']} <br> ".
     			 "Asset Number : {$row['assetnum']} <br> ".
     			 "--------------------------------<br></p>";
				} 
						 echo "Fetched data successfully\n";

	 mysql_close($link);

?>[/php]

Hope this makes sense.

Thanks.

I see you are also fetching the information also in insert.php.

Now your goal is to stay one page and also fetch the information too??

Yes, that’s the goal.

I have used mysqli api therefore you need to update link.inc.php file in the following manner.

link.inc.php
[php]<?php

$host = 'localhost';
$user = 'tanzeelniazi';
$pass = 'abc';
$db = 'phphelp';

$connection = mysqli_connect($host, $user, $pass, $db);

if (mysqli_connect_errno()){

	die(mysqli_connect_error());
}

?>[/php]

and Here is your assetdetails.php file.
[php]<?php

session_start();
// include 'header.html';

require ('link.inc.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST'){

	
	$make = $_POST['make'];
	$model = $_POST['model'];
	$assetnum = $_POST['assetnum'];
	$registration = $_POST['registration'];

	$query = "INSERT INTO 
					assets (make, model, assetnum, registration)
				VALUES ('{$make}', '{$model}', '{$assetnum}', '{$registration}')
			 ";

	$result = mysqli_query($connection, $query);

	if ($result && mysqli_affected_rows($connection) == 1){

		$_SESSION['message'] = 'Record Inserted. Good Skills Bro';
	}

	$query = "SELECT make, model, assetnum FROM assets";

	$result = mysqli_query($connection, $query);

	if(!$result){

		die(mysqli_error($connection));
	}

	while ($rows = mysqli_fetch_assoc($result)){

			$row[] = $rows;
	}

}

?>

Asset Register

form {

margin-left: 100px;

}

label.ast_form {
width: 7em;
float: left;
text-align: left;
margin-right: 0.5em;
display: block
}

input.submit {
font-size: 1.3em;
width: 100px;
height: 40px;
margin-left: 4em;
font-weight: bold;
color: #000;
background: #ffa20f;
border: 2px outset #d7b9c9
}

#message{
margin-bottom: 25px;
padding: 15px;
width: 400px;
border: 2px dotted red;
text-align: justify;
}

<?php
if (isset($_SESSION['message'])){

	echo '<div id="message">';
	echo $_SESSION['message'];
	unset($_SESSION['message']);
	echo '</div>';
}

?>

<?php if (isset($row) && is_array($row)) { ?> <?php foreach ($row as $value) { ?>
<p>	Make: <?php echo $value['make']; ?> <br />
 	Model: <?php echo $value['model']; ?> <br />
 	Asset Number: <?php echo $value['assetnum']; ?> <br />
 	--------------------------------
 </p>
<?php } ?> <?php } ?>

Insert Asset Details Into Register Database

<p>
	<label class="ast_form" for="make">Make: </label>
	<input type="text" name="make" id="make" required>
<br />
	<label class="ast_form" for="model">Model: </label>
	<input type="text" name="model" id="model" required>
<br />
	<label class="ast_form" for="assetnum">Asset Number: </label>
	<input type="text" name="assetnum" id="assetnum" required>
<br />
	<label class="ast_form" for="registration">Registration: </label>
	<input type="text" name="registration" id="registration" required>
</p>

<p> 
	<input class="submit" type="submit" name="submit" id="submit">
</p>
<?php
if (isset($result))
	{mysqli_free_result($result);}

if (isset($connection))
	{mysqli_close($connection);}

?>[/php]

I hope this is what you intended for :’( and now have an idea on how to use this method.

Thank you tanzeelniazi

Lot’s of new stuff here for me to digest. Works great, but I had to go back to “mysql_”, mysqli_ is not supported by the version I have, PHP 5.3

Thanks again.
:wink:

You are welcome.

mysqli was introduced in version 5.0 of PHP. If its not running on your side then you might be using a version of PHP older than 5.0.

Sponsor our Newsletter | Privacy Policy | Terms of Service