Forum Moderators: coopster

Message Too Old, No Replies

Trying to save user input in database and than display it

         

rus3439

9:33 pm on Aug 21, 2004 (gmt 0)

10+ Year Member



I have two buttons ina form "Submit" and "Show All Records". When submit button pressed it just gives a message that data was stores in a database successfully. But when "Show All Records" is pressed it should display the contents of the table.
Here is my html form:
<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>

And here is php code:
<?php
//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 = "data";
mysql_select_db($db);

$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 :)";

mysql_close($conn);

}

else if ($_POST['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);

//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);
}
?>

Anybody, please help! This is my first php code and i'm stuck.
thanks

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

dreamcatcher

10:51 pm on Aug 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Firstly, change this:


$result= MYSQL_QUERY("INSERT INTO users (Name, Address, SS#, Birthday, Birth_Place, Comments)".
"VALUES ('".$Name."','".$Address."','".$SS."','".$Birthday."','".$Birth_place."','".$comments."')");

to this:


$result= MYSQL_QUERY("INSERT INTO users (Name, Address, SS#, Birthday, Birth_Place, Comments) VALUES('".$Name."','".$Address."','".$SS."','".$Birthday."','".$Birth_place."','".$comments."')") or die(mysql_error());

As with your previous post, always use mysql_error().

I also remove the "." before values which doesn`t need to be there.

rus3439

11:56 pm on Aug 21, 2004 (gmt 0)

10+ Year Member



get the same error message:

You have an error in your SQL syntax near '' at line 1

as in the other program
Also when i delete "." in front of the variables it gives me syntax error.
Any advice?
thanks,

dkin

5:48 am on Aug 22, 2004 (gmt 0)

10+ Year Member



Try these and post any errors.

<?php
if ($Submit) {
$conn = mysql_connect("localhost","*****","*****") or
die("Could not connect: " . mysql_error());
//select the database
$db = "data";
mysql_select_db($db);

$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."')") or die(mysql_error());

echo "Your Query was succesfully stored in the database :)";

mysql_close($conn);

}

if ($Show_All_Records) {

//displaying the database

$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);

//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);
}
?>

[edited by: jatar_k at 4:26 pm (utc) on Aug. 24, 2004]

rus3439

7:07 pm on Aug 22, 2004 (gmt 0)

10+ Year Member



Ok, i tried your changes and the second button works. However i added some error checking and i get the following error:
You have an error in your SQL syntax near '' at line 1
The error checking is in the following line:

$result= MYSQL_QUERY("INSERT INTO users (Name, Address, SS#, Birthday, Birth_place, comments)".
"VALUES ('$Name', '$Address', '$SS#', '$Birthday', '$Birth_place', '$comments')")or die(mysql_error());

I think i have a problem with MySql because even when i try to insert some records into the table working directly in MySql it won't let me store any new data and will give an error window saying the same thing:
You have an error in your SQL syntax near '' at line 1

Can you please advise on how to fix this problem.

Many thanks,

[edited by: jatar_k at 4:31 pm (utc) on Aug. 24, 2004]

dreamcatcher

10:15 pm on Aug 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why are you concatenating your query?

)"."VALUES(

You dont need "." between your row names and values.

dkin

11:22 pm on Aug 22, 2004 (gmt 0)

10+ Year Member



also, change your database column name from this SS# to SS. you do not want # in there.

You will also have to change this

$SS = $_POST["SS#"];

to this

$SS = $_POST["SS"];

After making those changes let me know how it works.

Just starting this "being helpful" thing so I would like to know how my suggestions are doing.

Buyurun

1:33 am on Aug 23, 2004 (gmt 0)

10+ Year Member



I suggest making the first query look like this:

$query = "INSERT INTO users VALUES ('".$Name."','".$Address."','".$SS."','".$Birthday."','".$Birth_Place."','".$comments."');

$result = mysql_query($query) or die (mysql_error());

Putting it all in mysql_query looked quite ugly and is prone to error.

In the original there is also no need for the concatentation operator between the "INSERT" and "VALUES", which is solved in my SQL above.

rus3439

9:26 am on Aug 23, 2004 (gmt 0)

10+ Year Member



Thanks a lot for your suggestions. I just got home from work and will test it out first thing tomorrow morning.
Thanks,

rus3439

6:33 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



Hi everybody,
Following suggestions of dkin i changed the query string and got rid of ".". But now i get the following error message when "submit" is pushed:
You have an error in your SQL syntax near 'VALUES ('Ruslan', 'Clement', '#', '032277', '', '')' at line 2
I also changed ss# in mysql table and everywhere else in the code.

I also tried to change a string according to Buyurun's suggestion and now it does not give any error messages.
I assume it works! However, it won't print ANYTHING INSIDE THE WHILE LOOP OR AFTER IT.
Any suggestions? Thanks in advance,
Here is how the code looks now:

<?
//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')")or die(mysql_error());
$query = "INSERT INTO users VALUES ('".$Name."','".$Address."','".$SS."','".$Birthday."','".$Birth_Place."','".$comments."')"or die(mysql_error());
echo "Your Query was succesfully stored in the database :)";

mysql_close($conn);

}

if ($_POST['Show_All_Records']) {

//establish connection to mysql
$conn = mysql_connect("localhost","*****","*****") or
die("Could not connect: " . mysql_error());
echo "Connected to MySql".'<br>'.'<br>';

//select the database
$db = mysql_select_db("data");
$sql = "SELECT * FROM `tablename`";
$result = mysql_query($sql) or die(mysql_error());

//display the results
print'Name:'.($_POST["Name"]).'<br>';
print'Address:'.($_POST["Address"]).'<br>';
print'SS#:'.($_POST["SS"]). '<br>';
print'Birthday:'.($_POST["Birthday"]). '<br>';
print'Place Of Birth:'.($_POST["Birth_place"]). '<br>';
print'Comments:'.($_POST["comments"]).'<br>'.'<br>';

//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"];
}
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);
}
?>
thanks,
Ruslan

[edited by: coopster at 9:34 pm (utc) on Aug. 23, 2004]
[edit reason] generalized username and password [/edit]

rus3439

6:53 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



Hi everydody again,
I added echo statament inside the loop, but it still won't display anything. I added this line:

echo "$Name <br> $Address <br> $SS <br> $Birthday <br> $Birth_place <br> $Comments";

Also i put this inside the loop just to see if it eneters the loop but it still displays nothing.
Added this:
print'Name:'.($_POST["Name"]).'<br>';
Any advise please,
thanks,
Ruslan

dkin

7:14 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



If I am correct, you do not want those variables in a loop, those variables are defined with $POST_ which means each variable only has one value a loop is useless in this situation.

Remove this.

//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"];
}

Sub this out

//select the database
$db = mysql_select_db("data");
$sql = "SELECT * FROM `tablename`";
$result = mysql_query($sql) or die(mysql_error());

And Replace it with this

//select the database
$db = mysql_select_db("data");
$sql = "SELECT * FROM `tablename`";
$result = mysql_query($sql) or die(mysql_error());
$myrow = mysql_fetch_array($result);

Take this out.

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>';

And replace it with this.

echo 'Your Query was succesfully stored in the database :)';
echo ''.$myrow["Name"].' <br> '.$myrow["Address"].' <br> '.$myrow["SS"].' <br> '.$myrow["Birthday"].' <br> '.$myrow["Birth_place"].' <br> '.$myrow["comments"].' <br>';

When that is complete post your full code and I will go over it to make sure everything is correct.

If you get any errors post them as well.

[edited by: jatar_k at 4:47 pm (utc) on Aug. 24, 2004]

rus3439

7:17 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



Also i tried to put this statement after the query string:
This is my query string:
$query = "INSERT INTO users VALUES ('".$Name."','".$Address."','".$SS."','".$Birthday."','".$Birth_Place."','".$comments."')"or die(mysql_error());
I added this:
$result = mysql_query($query) or die(mysql_error());
and got the following message:
Table 'data.users' doesn't exist

My database name is "data" and my table name is "tablename".
Any thoughts,
thanks
Ruslan

rus3439

7:18 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



$query = "SELECT * FROM 'tablename'";

rus3439

7:30 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



Hello dkin,
I did what you suggested and got this output when "Show all records" is pushed:

Connected to MySql

Your Query was succesfully stored in the database :)

Here is the php code:
<?
if ($_POST['Submit']) {
$conn = mysql_connect("localhost","*****","*****") or
die("Could not connect: " . mysql_error());
//select the database
$db = mysql_select_db("data") or die(mysql_error());

$Name = $_POST["Name"];
$Address = $_POST["Address"];
$SS = $_POST["SS"];
$Birthday = $_POST["Birthday"];
$Birth_Place = $_POST["Birth_place"];
$Comments = $_POST["comments"];
$query = "INSERT INTO users VALUES ('".$Name."','".$Address."','".$SS."','".$Birthday."','".$Birth_Place."','".$comments."')"or die(mysql_error());
//$result = mysql_query($query) or die(mysql_error());
echo "Your Query was succesfully stored in the database :)";
mysql_close($conn);

}

if ($_POST['Show_All_Records']) {
$conn = mysql_connect("localhost","*****","*****") or
die("Could not connect: " . mysql_error());
echo "Connected to MySql".'<br>'.'<br>';

$db = mysql_select_db("data");
$sql = "SELECT * FROM `tablename`";
$result = mysql_query($sql) or die(mysql_error());
$myrow = mysql_fetch_array($result);

echo 'Your Query was succesfully stored in the database :)';
echo ''.$myrow["Name"].' <br> '.$myrow["Address"].' <br> '.$myrow["SS"].' <br> '.$myrow["Birthday"].' <br> '.$myrow["Birth_place"].' <br> '.$myrow["comments"].' <br>';

mysql_close($conn);
}
?>

[edited by: jatar_k at 4:41 pm (utc) on Aug. 24, 2004]

dkin

7:56 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



Copy this code, do not change anything except the database username pass etc etc.

<?
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"];

$query = "INSERT INTO users VALUES ('".$Name."','".$Address."','".$SS."','".$Birthday."','".$Birth_Place."','".$comments."')"or die(mysql_error());
echo "Your Query was succesfully stored in the database :)";

mysql_close($conn);
}

if ($Show_All_Records) {
$conn = mysql_connect("localhost","*****","*****") or
die("Could not connect: " . mysql_error());
echo "Connected to MySql".'<br>'.'<br>';
$db = mysql_select_db("data");
$sql = "SELECT * FROM `tablename`";
$result = mysql_query($sql) or die(mysql_error());

//display the results
print'Name:'.($_POST["Name"]).'<br>';
print'Address:'.($_POST["Address"]).'<br>';
print'SS#:'.($_POST["SS"]). '<br>';
print'Birthday:'.($_POST["Birthday"]). '<br>';
print'Place Of Birth:'.($_POST["Birth_place"]). '<br>';
print'Comments:'.($_POST["comments"]).'<br>'.'<br>';

//grabbing all data from the table
echo 'Your Query was succesfully stored in the database :)';

while ($myrow = mysql_fetch_array($result)) {
echo ''.$myrow["Name"].' <br> '.$myrow["Address"].' <br> '.$myrow["SS"].' <br> '.$myrow["Birthday"].' <br> '.$myrow["Birth_Place"].' <br> '.$myrow["Comments"].' <br>';
}
mysql_close($conn);
}
?>

Also I am assuming you would like to display the input from the user and the entire database, is this correct?

[edited by: jatar_k at 4:49 pm (utc) on Aug. 24, 2004]

rus3439

8:08 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



Yes that's correct. Whatever the user enters in the form i want it to be displayed after hitting the "Show All Records" button.
Good news: I am now able to insert data into the table (in MySql directly) after we changed the query string. It also diplays it after running the code. But won't display the user input.
thanks,
Ruslan

rus3439

8:13 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



I think we do need a some sort of loop here because it only displays one record. I have inserted one more record but it will only display one.
thanks,
Ruslan

dkin

8:14 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



Did you try the code that I posted? if so what was the output.

rus3439

8:22 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



the output is the following:
Connected to MySql

Your Query was succesfully stored in the database :)Ruslan
Clement st.
999999999
03/22/1977
Us
No

This is the first row of info that is stored in my database manually. But the Info i entered in the form is not dispalyed. Aslo The second row in the database is not displayed.
thanks,
Ruslan

[edited by: coopster at 9:37 pm (utc) on Aug. 23, 2004]
[edit reason] generalized information [/edit]

dkin

8:39 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



ll these scripts are untested but I will help until this is done.

try this

<?
$Name = $_POST["Name"];
$Address = $_POST["Address"];
$SS = $_POST["SS"];
$Birthday = $_POST["Birthday"];
$Birth_Place = $_POST["Birth_place"];
$Comments = $_POST["comments"];

if ($Submit) {
$conn = mysql_connect("localhost","*****","*****") or
die("Could not connect: " . mysql_error());
//select the database
$db = mysql_select_db("data");

$query = "INSERT INTO users VALUES ('".$Name."','".$Address."','".$SS."','".$Birthday."','".$Birth_Place."','".$comments."')"or die(mysql_error());
echo "Your Query was succesfully stored in the database :)";

mysql_close($conn);
}

if ($Show_All_Records) {

//establish connection to mysql
$conn = mysql_connect("localhost","*****","*****") or
die("Could not connect: " . mysql_error());
echo "Connected to MySql".'<br>'.'<br>';

//select the database
$db = mysql_select_db("data");
$result = mysql_query("SELECT * FROM 'tablename'",$conn);

//display the results
echo 'Name:', $Name ,'<br>'
. 'Address:', $Address ,'<br>'
. 'SS#:', $SS ,'<br>'
. 'Birthday:', $Birthday ,'<br>'
. 'Place Of Birth:', $Birth_place ,'<br>'
. 'Comments:', $Comments ,'<br>'.'<br>';

//grabbing all data from the table
echo 'Your Query was succesfully stored in the database :)';

while ($myrow = mysql_fetch_array($result)) {
echo ''.$myrow["Name"].' <br> '.$myrow["Address"].' <br> '.$myrow["SS"].' <br> '.$myrow["Birthday"].' <br> '.$myrow["Birth_Place"].' <br> '.$myrow["Comments"].' <br>';
}
mysql_close($conn);
}
?>

[edited by: jatar_k at 4:43 pm (utc) on Aug. 24, 2004]

rus3439

8:55 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



the same output:
Connected to MySql

Your Query was succesfully stored in the database :)

Ruslan
Clement st.
999999999
03/22/1977
Us
No

it seems to me it does not put values submitted into a database. Or maybe when connection to database established the second time, when "show all records" pushed, it accesses the original database.
I really appreciate your help on this, but i gotta go to work now and be back to try to finish this in about 11 hours.
Feel free to post your suggestions- i will test them out as soon as i get back.
Thanks again,
Ruslan

[edited by: coopster at 9:41 pm (utc) on Aug. 23, 2004]
[edit reason] generalized information [/edit]

dkin

9:30 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



I am beating my head off the wall because of the amount of times I have looked at this code. lol.

I cannot test it so I rely solely on your feedback.

Try this, edit the database information.

<?
//establish connection to mysql

// check which button was clicked
// perform calculation

$dbHost = "localhost";
$dbUser = "username";
$dbPass = "password";
$dbName = "data";

//connect to the mysql server
$conn = @mysql_connect($dbHost, $dbUser, $dbPass);

//return success or failure of connection
if(!$conn)
{
//report error
echo "Could not connect to server";
exit;
}

//select the database
if(!@mysql_select_db($dbName))
{
//report error
echo "Could not select database";
exit;
}

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

//Start First If Statement

if ($Submit) {

//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')")or die(mysql_error());

$query = "INSERT INTO users (Name, Address, SS, Birthday, Birth_place, comments) VALUES ('".$Name."','".$Address."','".$SS."','".$Birthday."','".$Birth_Place."','".$comments."')"or die(mysql_error());
echo "Your Query was succesfully stored in the database :)";

}

//End First if statement

//Start 2nd if statement

if ($Show_All_Records) {

//Select from Database

$result = mysql_query("SELECT * FROM 'users'",$conn);

//display the results

echo 'Name:', $Name ,'<br>'
. 'Address:', $Address ,'<br>'
. 'SS#:', $SS ,'<br>'
. 'Birthday:', $Birthday ,'<br>'
. 'Place Of Birth:', $Birth_place ,'<br>'
. 'Comments:', $Comments ,'<br>'.'<br>';

//grabbing all data from the table

echo 'Your Query was succesfully stored in the database :)';

//Start loop.

while ($myrow = mysql_fetch_array($result)) {
echo ''.$myrow["Name"].' <br> '.$myrow["Address"].' <br> '.$myrow["SS"].' <br> '.$myrow["Birthday"].' <br> '.$myrow["Birth_Place"].' <br> '.$myrow["Comments"].' <br>';
}
//End Loop
}
//End 2nd If Statement
mysql_close($conn);
?>

[edited by: jatar_k at 4:45 pm (utc) on Aug. 24, 2004]

rus3439

7:40 am on Aug 24, 2004 (gmt 0)

10+ Year Member



Hi, just got back from work...
I tried this code and it does not output anything at all on either buttons.
I think it does not read this kind of a form:
if ($Show_All_Records)
i'll keep trying and keep posting my results.
thanks for your contribution,
Ruslan

rus3439

9:09 am on Aug 24, 2004 (gmt 0)

10+ Year Member



I made some changes and here is what i got.
Good news! The input from the user is finally saved in the table. I logged into mysql and checked the table contents - they are updated very time i run php code and enter new input. So the only problem is to dispaly ALL the rows of the table. So far it dispays first one only.
this loop: while($myrow = mysql_fetch_array($result))
gives error message:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\foxserv\www\page2.php on line 45

Here is my code now (it is very different now):
<?php
$conn = mysql_connect("localhost","user","pass") or
die("Could not connect: " . mysql_error());
$db = mysql_select_db("data") or die(mysql_error());

//store all inputs into variables
$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 tablename VALUES ('$Name', '$Address', '$SS', '$Birthday', '$Birth_place', '$comments')");

//start first if statement
if ($_POST['Submit']) {
echo "Your Query was succesfully stored in the database :)";
}

if ($_POST['Show_All_Records']) {
//select the database
$db = mysql_select_db("data");
$sql = "SELECT * FROM `tablename`";
$result = mysql_query($sql) or die(mysql_error());
$myrow = mysql_fetch_array($result);

//display the results
print'Name:'.($_POST["Name"]).'<br>';
print'Address:'.($_POST["Address"]).'<br>';
print'SS#:'.($_POST["SS"]). '<br>';
print'Birthday:'.($_POST["Birthday"]). '<br>';
print'Place Of Birth:'.($_POST["Birth_place"]). '<br>';
print'Comments:'.($_POST["comments"]).'<br>'.'<br>';

echo 'Your Query was succesfully stored in the database :)'.'<br>'.'<br>';
echo "<p>",$myrow['Name'],":",
$myrow['Address'],
$myrow['SS'],
$myrow['Birthday'],
$myrow['Birth_place'],
$myrow['comments'] ;
mysql_close($conn);
}
?>

Hate to torture you, but any thoughts?
thanks,
ruslan

[edited by: jatar_k at 4:51 pm (utc) on Aug. 24, 2004]

jatar_k

5:19 pm on Aug 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Let's see if we can explain all of this

<?php 
// db connect
$conn = mysql_connect("localhost","user","pass") or
die("Could not connect: " . mysql_error());
$db = mysql_select_db("data") or die(mysql_error());

all of the above is just fine, we should now be connected to mysql.

Alright here's where it looks like thing start to go awry. First thing we need to look at is that we have 2 seperate jobs we want this script to do. Both of these need mysql connected so we can leave the above mysql_connect outside of our condition. For the rest the code will need to have 2 optional chunks of code therefore we go with an if else if since I don't know if you have some default for the script other than what I see in this post.

our base sctructure for the entire script will be

if ($_POST['Submit']) {
// Section A
} else if ($_POST['Show_All_Records']) {
// Section B
}

Section A - insert user data

Now, we need to do the following things in this section of code

1. acquire the data that the user submitted
2. construct our mysql query
3. execute our query and confirm that it succeeded

Your first part is fine but I would probably test the submitted data to see if required fields are missing and that the values are within the desired range. We can leave that for another day though.

//store all inputs into variables 
$Name = $_POST["Name"];
$Address = $_POST["Address"];
$SS = $_POST["SS"];
$Birthday = $_POST["Birthday"];
$Birth_Place = $_POST["Birth_place"];
$Comments = $_POST["comments"];

I like to do this in seperate lines to aid in debugging. Building my query into a variable and then use that variable in the mysql_query. This allows me to echo the constructed query if need be. While I am developing queries and any other db stuff I also use or die statements on everything sent to mysql, otherwise you don't get the actual error from mysql and you can't possibly debug it. After the query is sent we then test for success.

$sql = "INSERT INTO tablename VALUES "; 
$sql .= "('$Name', '$Address', '$SS', '$Birthday', '$Birth_place', '$comments')";
$result=mysql_query($sql) or die(mysql_error());
if (!$result) echo "no insert";
else echo "insert succeeded";

Section B - show all records

Now we are already connected to mysql and have selected the proper db so we are ready to roll. For this section of code we need to

1. construct our query
2. execute our query
3. display the returned results

your construct and execute look good.

$sql = "SELECT * FROM `tablename`"; 
$result = mysql_query($sql) or die(mysql_error());

we will do some straight forward tests for returned data and see if we can't loop through the rows. We can use mysql_num_rows to see how many rows were returned. If it is more than 0 then we need to reset the internal row pointer of the resource identifier otherwise our loop won't work. We test for 0 rows returned because then nothing needs to be done.

$howmany = mysql_num_rows($result); 
echo "<p>Your query returned $howmany rows";
if ($howmany > 0) {
mysql_data_seek($result,0);
while($myrow = mysql_fetch_array($result)) {
echo "<p>",$myrow['Name'];
echo "<br>",$myrow['Address'];
echo "<br>",$myrow['SS'];
echo "<br>",$myrow['Birthday'];
echo "<br>",$myrow['Birth_place'];
echo "<br>",$myrow['comments'];
}
}

and finally close the connection

mysql_close($conn);

Now let's see if all that works, here's the complete code

<?php 
$conn = mysql_connect("localhost","user","pass") or
die("Could not connect: " . mysql_error());
$db = mysql_select_db("data") or die(mysql_error());
if ($_POST['Submit']) {
$Name = $_POST["Name"];
$Address = $_POST["Address"];
$SS = $_POST["SS"];
$Birthday = $_POST["Birthday"];
$Birth_Place = $_POST["Birth_place"];
$Comments = $_POST["comments"];
$sql = "INSERT INTO tablename VALUES ";
$sql .= "('$Name', '$Address', '$SS', '$Birthday', '$Birth_place', '$comments')";
$result=mysql_query($sql) or die(mysql_error());
if (!$result) echo "no insert";
else echo "insert succeeded";
} else if ($_POST['Show_All_Records']) {
$sql = "SELECT * FROM `tablename`";
$result = mysql_query($sql) or die(mysql_error());
$howmany = mysql_num_rows($result);
echo "<p>Your query returned $howmany rows";
if ($howmany > 0) {
mysql_data_seek($result,0);
while($myrow = mysql_fetch_array($result)) {
echo "<p>",$myrow['Name'];
echo "<br>",$myrow['Address'];
echo "<br>",$myrow['SS'];
echo "<br>",$myrow['Birthday'];
echo "<br>",$myrow['Birth_place'];
echo "<br>",$myrow['comments'];
}
}
}
?>

I split some things to 2 lines so we wouldn't get sidescroll on this thread but it won't make a difference.

rus3439

6:45 pm on Aug 24, 2004 (gmt 0)

10+ Year Member



this code works, but it only displays four first inputs: Name, Address, SS is assigned 0, Birthday and nothing after that.
Thanks a lot for your contribution,
Ruslan

jatar_k

6:56 pm on Aug 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well, what's in the actual db? all it is doing is grabbing * from the db.

is the case right in the column names and the script?
what data is in the db right now?

rus3439

7:03 pm on Aug 24, 2004 (gmt 0)

10+ Year Member



i found the bug. It is just minor bug with assigment of unputs to vars. It was like this:
$Birth_Place = $_POST["Birth_place"];
$Comments = $_POST["comments"];

I changed it to this:
$Birth_place = $_POST["Birth_place"];
$comments = $_POST["comments"];

Everything works now,
thanks a lot jatar_k and dkin for your contribution,
Ruslan