okay...:
this
PHP Code:
simple.php
<?php
include('dbconnect.php');
$name = $_POST[name];
$age = $_POST[age];
$data = "insert into simplerecord values(null,'$name','$age')";
mysql_query($data) or die(mysql_error());
?>
does the query and inserts a record. However, because the form action is now "simple2.php", it does not take data from the form. The record it inserts is blank.
Adding that code (or, more specifically, *moving* that code) to simple2.php would not be redundant because the code no longer works properly where it is anyway. You will end up with extra blank records every time someone visits the page, whether they fill out the form or not.
Am I correct that you want to display the simplerecord table after someone submits their record to it? Try something like this (all on one page):
PHP Code:
simple.php
<?php
if(!isset($_POST['name'])){
echo '
<html>
<head>
<title> simple form </title>
</head>
<body>
<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
<p><strong>Name:</strong><br>
<input type="text" name="name"></p>
<p><strong>Age:</strong><br>
<input type="text" maxlength="2" name="age"></p>
<p><input type="submit" value="send"></p>
</form>
</body>
</html>';
}else{
include('dbconnect.php');
$name = $_POST[name];
$age = $_POST[age];
$data = "insert into simplerecord values(null,'$name','$age')";
mysql_query($data) or die(mysql_error());
$data2 = "select * from simplerecord";
$query2 = mysql_query($data2);
$num2 = mysql_num_rows($query2);
$x = 0;
while($x < $num2)
{
$name2 = mysql_result($query2,$x,"name");
$ID2 = mysql_result($query2,$x,"ID");
$age2 = mysql_result($query2,$x,"age");
printf("Name: %s<br>",$name2);
printf("ID: %d<br>",$ID2);
printf("Age: %d<br><br>",$age2);
$x++;
}
}
?>
OR, you could remove the if - else statement and keep the code on two pages. only the form would go on "simple.php" (with action="simple2.php"), everything else would go on "simple2.php".
Bookmarks