I am trying to convert a program for my computerscience class from Visual Basic To Web. So far I have a page to enter the data into the table, a page to view the table.
Right now all I have on the index page is the html with the form and the button, and the form name. On submit the page loads display.php which writes to the database and then displays all the data currently in the database. However everytime the page is refreshed the data is added again to the database. So if I refresh it 300 times, I will have the same entry in the database 300 times.
What I am trying to do is add a third page, one which is not seen but will process the adding of data to the database, and then go to the display page.
This may seem really stupid to anyone who knows php, so if you can help me link page 2 to the display page, or you have a better system(like how to add the data after they hit the submit button but before it loads the new paged) then please post and hlpe me!
Thanks,
Crimson
I'm not sure I understand your question, but I might, maybe you could do this.
Page 1: Form thing, goes to page two.
Page 2: Enter the database values, then redirect to page three
Page 3: Displays the table.
If you want to stop them from going back and keep entering, you could add a session variable, that is set to one on the first page, then on the second one, is turned off after the values are entered. If it's not set to one, it won't enter it, but if it is it will.
Page 1: Enter Database values. Press submit and go to page two.
Page 2:Nothing displays. The code to add the data from page on is entered into the database.
Page 3: Displays the table.
I don't get your session thing. (PHP newb).
I dont think I need page two, just when I had it store the data form page one, it happened at the beginning of page two.
I can send the code if you want, and pass the url so you can see it in action.
I have added a submit button to page to so you can see it actually exists now, but no matter what it enters all blank values into the database.
Umm, can you post your script here? Or PM it to me or something, so I can see what's wrong with the entering part?
CODE
Page 1:
<?php
if ($_GET['e'] == 1)
echo "You did not fill out a name!<br />";
echo "
<FORM METHOD="POST" ACTION="postdata.php">
<INPUT TYPE="text" NAME="myname" SIZE="20" MAXLENGTH="20" />
<input type="submit" value="Enter Name" />
</form>";
?>
Page 2:
<?php
if (!$_POST['myname'])
@header("Location: index.php?e=1");
else
$conn = mysql_connect('localhost',"<<username>>","<<password>>");
mysql_select_db("<<database name>>");
$sql = "INSERT INTO `<<table>>` (`username`) VALUES ({$_POST['myname']})";
mysql_query($sql, $conn);
@header("Location: display.php");
?>
Page 3:
<?php
$conn = mysql_connect('localhost',"<<username>>","<<password>>");
mysql_select_db("<<database name>>");
echo "<table border='1' cellpadding='2px' cellspacing='2px'>";
$sql = "SELECT * FROM `<<table>>`";
$thisresult = mysql_query($sql, $conn);
while ($row = mysql_fetch_row($thisresult))
{
echo "<tr><td>{$row['username']}</td></tr>";
}
echo "</table>";
?>
At it's simpliest...
It doesn't account for the possibility of an sql injection attack, but, I don't think you have to worry about such things.
He won't have to... this guys in a computer programming class of mine. I'm just about the only person doing anything in that class with php, I think hes trying to join in

OK here Goes... MY Code.
Please not I have added many things that you may find stupid because I was trying to trouble shoot.
After not working my code for the three pages are as follows.
Page1:
<form action="savetoDB.php" method="post">
Visa Number: <input type="text" name="visa"><br>
Last Name: <input type="text" name="last"><br>
First Name: <input type="text" name="first"><br>
Address: <input type="text" name="address"><br>
Phone Number: <input type="text" name="phone"><br>
<input type="Submit">
</form>
Page2:
<?
$username="(MY User Name is HERE)";
$password="(MY Password Is here)";
$database="(My Database is here)";
if($_POST['submit']){
// Connect to the mysql database
$connection=mysql_connect("localhost", $username, $password);
// Select a pre-made database in connection
mysql_select_db($database, $connection);
// Since the form action was set to post, we use
// $_POST['name_of_input_field] to get the values send to
// this script
$visa=$_POST['visa'];
$last=$_POST['last'];
$first=$_POST['first'];
$address=$_POST['address'];
$phone=$_POST['phone'];
// The next thing is to put it in MySQL table in the
// database selected above in the mysql_select_db
// We will assume the table has already been created
// with two collums named 'name' and 'email'.
// Use phpMyAdmin to create the table.
$results=mysql_query("INSERT INTO 'Accounts' ('Visa Number', 'Surname', 'First Name', 'Address', 'Phone Number')
VALUES (('$visa', '$last', '$first', '$address', '$phone')") or
die('Error making query');
echo'Data added to table';
}
?>
Your Data Has Been Saved.
<form action="display.php" method="post">
<input type="submit" name="Submit" value="Continue">
</form
Page3:
<?
$username="(MY User Name is HERE)";
$password="(MY Password Is here)";
$database="(My Database is here)";
?>
<?
//--------------------This is the only working display code----------------------------------
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost",$username,$password);
//select which database you want to edit
mysql_select_db($database);
//Display the title
echo "<b><center>Database Output</center></b><br><br>";
?>
<table width="75%" border="1">
<?
//select the table
$result = mysql_query("select * from Accounts");
//grab all the content
while($r=mysql_fetch_array($result))
{
//the format is $variable = $r["nameofmysqlcolumn"];
//modify these to match your mysql table columns
$visa=$r["Visa Number"];
$last=$r["Surname"];
$first=$r["First Name"];
$address=$r["Address"];
$phone=$r["Phone Number"];
//display the row
?> <tr><td><?echo "$visa"?></td><td><?echo "$last"?></td><td><?echo "$first"?></td><td><?echo "$address"?></td><td><?echo "$phone"?></td></tr>
<?
}
//--------------------This is the only working display code----------------------------------
?>
PS. I have taken out all server information for security reasons only. Prior to moving code around and adding the third page, everything worked fine just was in the wrong place, and did the correct things, but also did things that were incorrect.
Thanks!