4

I have a confusion in mind regarding a logic.. it is a form that i first want to validate then I want to save in database and then i want to display it on another page.

I have validated it (transferring (i.e. form action) the form to the same page), I have saved it in database, and now I m trying to display the data on another page.

What I have thought, that I'll save the data to the db and transfer some unique value to another page, maybe some sort of number auto generated by database related to the same form.

now I have problem how to do it? Any ideas guys?

Since it is a logic hence I am tagging php, MySQL and JS

aks
  • 291
  • 4
  • 11

3 Answers3

2

Call mysql_insert_id() once you perform INSERT operation something like this,

$lastInsertedId =  mysql_insert_id(); # Assuming you have an auto increment column in your table.
header('Location: view.php?id='.$lastInsertedId);

Than in your view.php get the id & fetch record to display it,

$id = $_GET['id'];

Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • Maybe you should include what the database table has to look like in order for this to work. Sounds like the author doesn't know about that. – Till Helge Mar 12 '13 at 08:05
  • @rikesh but suppose, there are many users accessing that form at the same time, from different pc's and different ip's and say they all save values in db at the same time. My target here is that the person who saved the value in db shall see his data on second page. accessing last saved value will give the desired output? – aks Mar 12 '13 at 08:09
  • @aks - Yes he can. Read http://stackoverflow.com/questions/12467111/what-happens-if-mysql-insert-id-called-form-different-places-and-different-brows – Rikesh Mar 12 '13 at 08:11
  • This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_insert_id() PDO::lastInsertId() – aks Mar 12 '13 at 08:16
  • this shows such sort of url... localhost/test/page1.php?id=9 I pull out values from db using this value... and if user try to change the id=9 to id= 12 or 7 or 5 or 7 in address bar then what will happen?? – aks Mar 12 '13 at 08:52
  • @aks - You can use `$_SESSION` such case, seems you need to get hands on basic PHP. – Rikesh Mar 12 '13 at 09:01
1

After insertion query,use

$lastID = mysql_insert_id();

After then you can redirect to another page like,

header("location:view_page.php?id=$lastID");

Than in view_page.php page,use

$lastID= $_GET['id'];

after then fetch record from database to display data.

NOTE: It will work only when your database table has autoincremented field as index key.

Purushottam
  • 844
  • 14
  • 25
1

Using mysql_insert_id() is a way. But If you do not want to query your database and still have that then you can do it this way . Below goes the example

<form name="frm" method="POST" action="display.php">
 <input type="text" name="your_name" value="" />
 <input type="text" name="age" value="" />
 <input type="submit" name="save" value="Save" />
</form>

Now in your display.php page

<?php
 // DO YOUR DATABASE CONNECTIVITY
 $name = $_POST['your_name'];
 $age = $_POST['age'];

 $query = "insert into table_name values('$name','$age')";
 if(mysql_query($insert)) {

?>
<table>
 <tr>
  <td>Name</td>
  <td>Age</td>
 </tr>
 <tr>
  <td><?php echo $name;?></td>
  <td><?php echo $age;?></td>
 </tr>
</table>
<?php } else { echo "No Insertion"; } ?>

Remember mysql_* function are depreciated. So avoid those. Also check for sql injection

Roger
  • 1,693
  • 1
  • 18
  • 34
  • this shows such sort of url... http://localhost/test/page1.php?id=9 I pull out values from db using this value... and if user try to change the id=9 to id= 12 or 7 or 5 or 7 in address bar then what will happen?? – aks Mar 12 '13 at 08:51
  • @aks: It wnot show such type of url. As this is a POST type form submission. The url that you have shown above is passing parameter through url. Use POST type in your form method to avoid such situation. – Roger Mar 12 '13 at 09:48
  • sorry.. I posted wrong question to you.. I cant use your method as I cant transfer values directly to that page.. I have to action back values to the same page due to validations issues... so I have to transfer values through url method only... – aks Mar 12 '13 at 09:56
  • @aks: Ok. Then you can use `base64_encode()` to encode the `id` that you are sending. Also I am not forcing you to use my method . Just giving a way around. :) – Roger Mar 12 '13 at 10:01
  • naah.. this is not the case.. I definitely was looking for some method to encode it... and that ll be my second step in this direction.. thanks anyways – aks Mar 12 '13 at 11:00