Forum Moderators: coopster

Message Too Old, No Replies

How to display MySql table after input have been taken fron a user

         

rus3439

12:47 am on Aug 21, 2004 (gmt 0)

10+ Year Member



Here is my code:
I have two button in html page - submit and show all records. When "Show All Records" clicked, it supposed to display entered info from the previous page, but it shows nothing. When submit button is pushed it says that data have been stored in database(that's all i need). I am extremely new to php so please advise.
Thanks a Lot.
<?
//establish connection to mysql

// check which button was clicked
// perform calculation
if ($_POST['Submit'])
{
$conn = mysql_connect("localhost","****","****") or
die("Could not connect: " . mysql_error());
//select the database
$db = mysql_select_db("data");

$Name = $_POST["Name"];
$Address = $_POST["Address"];
$SS = $_POST["SS#"];
$Birthday = $_POST["Birthday"];
$Birth_Place = $_POST["Birth_Place"];
$Comments = $_POST["Comments"];
//insert the values into the table
$result= MYSQL_QUERY("INSERT INTO users (Name, Address, SS#, Birthday, Birth_Place, Comments)".
"VALUES ('$Name', '$Address', '$SS#', '$Birthday', '$Birth_Place', '$Comments')");

echo "Your Query was succesfully stored in the database :)";
//print($_POST["Name"]);
mysql_close($conn);

}

else if ($_POST['Show All Records']) {


//displaying the database

//establish connection to mysql
$conn = mysql_connect("localhost","rus3439","rus34391") or
die("Could not connect: " . mysql_error());
echo "Connected";
//select the database
$db = mysql_select_db("data");
$sql = "SELECT * FROM `tablename`";
$result = mysql_query($sql) or die(mysql_error());
print($_POST["Name"]);

//grabbing all data from the table
while ($r = mysql_fetch_array($result))
{
//the format is $variable = $r["nameofmysqlcolumn"];
$Name=$r["Name"];
$Address=$r["Address"];
$SS=$r["SS#"];
$Birthday=$r["Birthday"];
$Birth_Place=$r["Birth_Place"];
$Comments=$r["Comments"];

//display the row
echo "Your Query was succesfully stored in the database :)";
echo "$Name <br> $Address <br> $SS# <br> $Birthday <br> $Birth_Place <br> $Comments <br>";
mysql_close($conn);
}
}
?>

[edited by: coopster at 3:40 pm (utc) on Aug. 21, 2004]
[edit reason] generalized username and password [/edit]

dkin

2:02 am on Aug 21, 2004 (gmt 0)

10+ Year Member



Now I am not sure if this is what you were looking for but I'll give it a shot. Report any errors and I will do my best. I changed SS# to SS which I suggest you do in the form and the db, seeing as # edits out the rest of the line it is on.

<?
//establish connection to mysql
// check which button was clicked
// perform calculation
if ($Submit)

{
$conn = mysql_connect("localhost","****","****") or
die("Could not connect: " . mysql_error());

//select the database

$db = mysql_select_db("data");

$Name = $_POST["Name"];
$Address = $_POST["Address"];
$SS = $_POST["SS#"];
$Birthday = $_POST["Birthday"];
$Birth_Place = $_POST["Birth_Place"];
$Comments = $_POST["Comments"];

//insert the values into the table
$result= MYSQL_QUERY("INSERT INTO users (Name, Address, SS#, Birthday, Birth_Place, Comments)".
"VALUES ('".$Name."','".$Address."','".$SS."','".$Birthday."','".$Birth_Place."','".$Comments."')");

echo "Your Query was succesfully stored in the database :)";
//print($_POST["Name"]);
mysql_close($conn);

}

if ($Show All Records) {

//displaying the database

//establish connection to mysql
$conn = mysql_connect("localhost","****","****") or die("Could not connect: " . mysql_error());
echo "Connected";
//select the database
$db = mysql_select_db("data");
$sql = "SELECT * FROM `tablename`";
$result = mysql_query($sql) or die(mysql_error());
$myrow = mysql_fetch_array($results);
print($_POST["Name"]);

//display the row
echo 'Your Query was succesfully stored in the database :)'
. ''.$myrow["Name"].' <br> '.$myrow["Address"].' <br> '.$myrow["SS"].' <br> '.$myrow["Birthday"].' <br> '.$myrow["Birth_Place"].' <br> '.$myrow["Comments"].' <br>';
mysql_close($conn);
}
?>

rus3439

3:52 am on Aug 21, 2004 (gmt 0)

10+ Year Member



Thanks,
i run your code and got this when click on "submit" button and "show all records" button.

Parse error: parse error, unexpected T_STRING in c:\foxserv\www\page4.php on line 32

This is line 32:
if ($Show All Records) {

thank you,

dkin

4:14 am on Aug 21, 2004 (gmt 0)

10+ Year Member



What is the name of your view all submissions button?

whatever it is change 'Show All Records' to that.

rus3439

4:21 am on Aug 21, 2004 (gmt 0)

10+ Year Member



My view all submissions is called "Show All Records".
Here is my html file, maybe the problem is there, even though it's pretty straightforward.

<form action="page4.php" method="post">
<P><STRONG>PLEASE ENTER THE FOLLOWING INFO BELOW::</STRONG></P>
<P><STRONG>Name:</STRONG>
<input type="text" name="Name" size="10"><br>
</P>
<P><STRONG>Address:</STRONG>
<input type="text" name="Address" size="10"><br>
</P>
<P><STRONG>SS#:</STRONG>
<input type="text" name="SS#" size="20"><br>
</P>
<P><STRONG>Birthday:</STRONG>
<input type="text" name="Birthday" size="10"><br>
</P>
<P><STRONG>Place Of Birth:</STRONG>
<input type="text" name="Birth_place" size="10"><br>
</P>
<P><STRONG>Comments:</STRONG>
<TEXTAREA NAME = "comments" ROWS = "4" cols = "20"></TEXTAREA>
</P>
<input type="submit" value="Submit" name="Submit">
<input type="submit" value="Show All Records" name="Show All Records">
</form>

thanks,

rus3439

4:23 am on Aug 21, 2004 (gmt 0)

10+ Year Member



Also do you think that the table i created in MySQl may cause this problem. The table there is empty - can that be a problem?
thanks,

dkin

6:24 am on Aug 21, 2004 (gmt 0)

10+ Year Member



that would be why you are getting no output, insert one test row into the database and run the script.

rus3439

7:27 am on Aug 21, 2004 (gmt 0)

10+ Year Member



what if i just want to get input from users and have the created table empty.
Do you think it still matters to have something in the table.
thanks,

dkin

7:33 am on Aug 21, 2004 (gmt 0)

10+ Year Member



I do not completely understand what you are getting at, create the table empty but enter 1 row for testing purposes, After testing is complete delete the row and launch the script.

jatar_k

5:29 pm on Aug 21, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld rus3439,

take a look at this thread
Basics of extracting data from MySQL using PHP [webmasterworld.com]