Jump to content

I am trying to create a simple php project. (need help)


josuenerd

Recommended Posts

There is only one thing I want to know.  How to I fill out a text box, click submit and make that information go to a msql database? 

 

This is very simple but I am very new to php.  This is all I want to learn.  Also showing the value on a table after I submitted would be great.

 

Thanks and I hope someone can help.  :)

Link to comment
Share on other sites

Hi there,

 

I'm not going to give you the answer becuase that would be pointless. It would not help you learn at all, but here are some resources you can learn from:

 

PHP:

http://w3schools.com/php/default.asp

 

 

HTML:

http://w3schools.com/html/default.asp

 

and if you want to learn the new HTML 5 (not yet standardised):

http://w3schools.com/html5/default.asp

 

 

 

I hope this helps you!

Link to comment
Share on other sites

Thanks I did it hear and learned how to do it in less than 1 hour.

 

http://trendsavers.com/josuenerd/submitform.html

 

I really think I am going to love php.

 

-

 

How can I make the 3 inputs show on a html table row after I submit?

 

for example a  table like this:

 

First Name                                      Last Name                                    Age

James                                            Smith                                            25

Henry                                            Smith                                              24

 

Link to comment
Share on other sites

submitform.html

 

<html>
<body>

<form action="insert.php" method="post">
Firstname: <input type="text" name="Firstname" /><br>
Lastname: <input type="text" name="Lastname" /><br>
Age: <input type="text" name="Age" /><br><br>
<input type="submit" value="Submit Profile" />
</form>

</body>
</html> 

 

 

 

insert.php

 

<?php
$con = mysql_connect("localhost","trend_learnu","xxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("trend_learningdb", $con);

$sql="INSERT INTO Peoples (FirstName, LastName, Age)
VALUES
('$_POST[Firstname]','$_POST[Lastname]','$_POST[Age]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?> 

 

 

Those are the 2 files I have for now.  I would like to show the table on insert.php.

Link to comment
Share on other sites

$result = mysql_query("SELECT * FROM peoples");

 

while($row=mysql_fetch_array($result))

{

  $firstname = $row['FirstName'];

  $lastname = $row['LastName'];

  $age = $row['Age'];

 

  $table = '<table>';

  $table.= '<tr>';

  $table.= '<td>'.$firstname.'</td>';

  $table.= '<td>'.$lastname.'</td>';

  $table.= '<td>'.$age.'</td>';

  $table.= '</tr>';

  $table.= '</table>';

}

 

echo $table;

 

 

Something like that.  I don't have access to test this, but it should be pretty good.

Link to comment
Share on other sites

It worked thanks  :D

 

http://trendsavers.com/josuenerd/learning10.php

 

 

<?php$db = mysql_connect("localhost","trend_learnu","asdfasdf");mysql_select_db("trend_learningdb",$db);$result = mysql_query("SELECT * FROM Peoples");while($row=mysql_fetch_array($result)){  $firstname = $row['Firstname'];  $lastname = $row['Lastname'];  $age = $row['Age'];   $table = '<table width="608" border="0" align="center" cellpadding="5" cellspacing="0">  <tr>    <td width="207" bgcolor="#CCCCCC">Firstname</td>    <td width="204" bgcolor="#CCCCCC">Lastname</td>    <td width="197" bgcolor="#CCCCCC">Age</td>  </tr>';   $table.= '<tr>';   $table.= '<td>'.$firstname.'</td>';   $table.= '<td>'.$lastname.'</td>';   $table.= '<td>'.$age.'</td>';   $table.= '</tr>';   $table.= '</table>';}echo $table;?>

 

 

 

One thing I notice is that it only shows the last one.  These are all the ones I have in phpmyadmin:

 

Peter  Griffin  35

Glenn Quagmire 33

Joshua Sandoval 27

Tom Cruise 40

Lolers Sanders 27

Henry Bullworth 57

Test last 22

 

Is it possible to show each one in a row?

 

 

Thanks alot guys I am loving php :D

Link to comment
Share on other sites

Maybe try this?

 

 

<?php$db = mysql_connect("localhost","trend_learnu","asdfasdf");mysql_select_db("trend_learningdb",$db);$result = mysql_query("SELECT * FROM Peoples");$table = '<table width="608" border="0" align="center" cellpadding="5" cellspacing="0">  <tr>    <td width="207" bgcolor="#CCCCCC">Firstname</td>    <td width="204" bgcolor="#CCCCCC">Lastname</td>    <td width="197" bgcolor="#CCCCCC">Age</td>  </tr>';while($row=mysql_fetch_array($result)){  $firstname = $row['Firstname'];  $lastname = $row['Lastname'];  $age = $row['Age'];   $table.= '<tr>';   $table.= '<td>'.$firstname.'</td>';   $table.= '<td>'.$lastname.'</td>';   $table.= '<td>'.$age.'</td>';   $table.= '</tr>';}$table .= '</table>';echo $table;?>

 

Link to comment
Share on other sites

Right now I am just using firstname, lastname and age as a test.

 

My mail goal is to make it show the date and weight. (Current date and persons weight)

 

I want a user to sign up and register.  They will then have access to enter the date and weight just for logging reasons.

 

Will it be very complicated to allow each user their own area to do this?

Link to comment
Share on other sites

Can someone please check the two php files below:

 

 

submit.php

 

<html>
<body>
<?php
$today = date("F j, Y");
?> 


<form action="insert.php" method="post">
<input name="Date" type="hidden" value="<?php PRINT "$today"; ?>" />
First Name: <input type="text" name="Firstname" /><br><br>
Last Name: <input type="text" name="Lastname" /><br><br>
Weight: <input type="text" name="Weight" /><br><br>

<input type="submit" value="Submit Profile" />
</form>



</body>
</html> 

 

 

 

insert.php

 

 

<?php
$con = mysql_connect("localhost","trend_learnu","asdfasdf");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("trend_learningdb", $con);

$sql="INSERT INTO Peoples (Firstname, Lastname, Weight, Date)
VALUES
('$_POST[Firstname]','$_POST[Lastname]','$_POST[Weight]','$_POST[Date]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";



$db = mysql_connect("localhost","trend_learnu","12345");
mysql_select_db("trend_learningdb",$db);

$result = mysql_query("SELECT * FROM Peoples");

$table = '<table width="608" border="0" align="center" cellpadding="5" cellspacing="0">
  <tr>
    <td width="207" bgcolor="#CCCCCC">Firstname</td>
    <td width="204" bgcolor="#CCCCCC">Lastname</td>
    <td width="197" bgcolor="#CCCCCC">Weight</td>
    <td width="197" bgcolor="#CCCCCC">Date</td>
  </tr>';

while($row=mysql_fetch_array($result))
{
  $firstname = $row['Firstname'];
  $lastname = $row['Lastname'];
  $weight = $row['Weight'];
  $date = $row['Date'];

   $table.= '<tr>';
   $table.= '<td>'.$firstname.'</td>';
   $table.= '<td>'.$lastname.'</td>';
   $table.= '<td>'.$weight.'</td>';
   $table.= '<td>'.$date.'</td>';
   $table.= '</tr>';
}
$table .= '</table>';

echo $table;



mysql_close($con)


?> 

 

 

 

 

If you go to the script live and submit: http://trendsavers.com/cutlog/submit.php

 

You can see that what gets submitted goes to the top row.  How do I make it go to the bottom row?

 

 

 

 

 

Link to comment
Share on other sites

Sometimes it does add it to the bottom.  What do I need to do to the scripts above so that it always strictly goes to the bottom of the html table?

 

Am I supposed to put the mysql table with maybe a number so it goes like 1, 2, 3, 4, 5...if so how do I do this?

Link to comment
Share on other sites

Yes, it is good practice have your first column in your DB be an ID that autoincrements.

 

Then, you can query with ORDER BY id DESC or ASC.

 

This will order them how you want.

 

Currently, you can order, but you can't order by when they submitted, you can only order by last name, first name, etc.

Link to comment
Share on other sites

All together I think I have about 3 hours of learning php. I don't know about primary and foreign keys. 

 

I went to phpmyadmin and did this:

 

Step 1:

1syzqd.png 

 

 

Step 2:

xojti9.png

 

 

Did I do this correctly and now am I able to make my script submit and show latest "number" or field in the bottom of my html table?

Link to comment
Share on other sites

Yes, that looks fine.

 

change:

$result = mysql_query("SELECT * FROM Peoples");

 

To this:

$result = mysql_query("SELECT * FROM Peoples ORDER BY number DESC");

 

Knowing PHP is great, but you also need to know about databases, primary keys, foreign keys, relations, etc

Link to comment
Share on other sites

Databases: Normal Forms, primary keys, foreign keys, Relational Databases, SQL language - Just to start.  Much more later

 

PHP: You are doing pretty good so far, but you can look at arrays, loops(for, foreach, while, etc), sessions functions, classes(leave this for later, you won't understand them right away.)

 

http://www.w3schools.com/php/default.asp

 

For PHP stuff, its important to know what each of those do, not know how to do them for right now.

Link to comment
Share on other sites

I tried and it was going to the top

 

$result = mysql_query("SELECT * FROM Peoples ORDER BY number DESC");

 

I tried and it now goes to the bottom

 

$result = mysql_query("SELECT * FROM Peoples ORDER BY number ASC");

 

Does this mean there is something wrong with my script?

 

I also see a bug.  If someone goes straight to insert.php it would enter blank fields to the database.  What is my best solution for this?

 

I just want a customer to enter their weight and right after they submit they see their results.

 

OR better yet always showing the results and on the bottom having the weight submit button. When submitted the html table updates and shows the new weight.

 

Link to comment
Share on other sites

I tried it but it would just redirect insert.php to submit.php.  Also after I submit from submit.php it would just look the same at insert.php.

 

Is it possible to just have 1 php file that shows the html table and at the bottom It has the input fields and submit buttons?  After this maybe updating the html form.  Or somehow refreshing the page or data.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.