josuenerd Posted October 11, 2010 Share Posted October 11, 2010 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 https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/ Share on other sites More sharing options...
DarkMantis Posted October 11, 2010 Share Posted October 11, 2010 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 https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1120996 Share on other sites More sharing options...
Pawn Posted October 11, 2010 Share Posted October 11, 2010 http://www.phpfreaks.com/tutorial/php-basic-database-handling Link to comment https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121008 Share on other sites More sharing options...
josuenerd Posted October 11, 2010 Author Share Posted October 11, 2010 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 https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121072 Share on other sites More sharing options...
BlueSkyIS Posted October 11, 2010 Share Posted October 11, 2010 http://w3schools.com/html/default.asp Link to comment https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121074 Share on other sites More sharing options...
josuenerd Posted October 11, 2010 Author Share Posted October 11, 2010 I know html. The problem I have is making the above database information show up in rows in my html. I made a quick html table example: http://trendsavers.com/josuenerd/table.html Link to comment https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121088 Share on other sites More sharing options...
litebearer Posted October 11, 2010 Share Posted October 11, 2010 show us what code you have written thus far (don't worry if it is wrong) Link to comment https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121093 Share on other sites More sharing options...
josuenerd Posted October 11, 2010 Author Share Posted October 11, 2010 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 https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121096 Share on other sites More sharing options...
coupe-r Posted October 11, 2010 Share Posted October 11, 2010 $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 https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121139 Share on other sites More sharing options...
josuenerd Posted October 11, 2010 Author Share Posted October 11, 2010 It worked thanks 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 Link to comment https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121144 Share on other sites More sharing options...
coupe-r Posted October 11, 2010 Share Posted October 11, 2010 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 https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121157 Share on other sites More sharing options...
josuenerd Posted October 11, 2010 Author Share Posted October 11, 2010 It worked thanks I have to start planning out the script I am trying to make. I will post more info later. Link to comment https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121182 Share on other sites More sharing options...
josuenerd Posted October 11, 2010 Author Share Posted October 11, 2010 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 https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121189 Share on other sites More sharing options...
josuenerd Posted October 11, 2010 Author Share Posted October 11, 2010 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 https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121209 Share on other sites More sharing options...
josuenerd Posted October 11, 2010 Author Share Posted October 11, 2010 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 https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121211 Share on other sites More sharing options...
coupe-r Posted October 11, 2010 Share Posted October 11, 2010 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 https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121215 Share on other sites More sharing options...
josuenerd Posted October 11, 2010 Author Share Posted October 11, 2010 Thanks How do I add a auto id autoincrement to the first column of my db? Link to comment https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121218 Share on other sites More sharing options...
coupe-r Posted October 11, 2010 Share Posted October 11, 2010 Well, you need to delete your current fields, then re-do them starting with ID. Make sure all ID fields in your DB are INTEGERS. Do you know about primary / foreign keys? Link to comment https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121219 Share on other sites More sharing options...
josuenerd Posted October 11, 2010 Author Share Posted October 11, 2010 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: Step 2: 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 https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121228 Share on other sites More sharing options...
coupe-r Posted October 11, 2010 Share Posted October 11, 2010 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 https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121232 Share on other sites More sharing options...
josuenerd Posted October 11, 2010 Author Share Posted October 11, 2010 I will start reading about all of this. Can you post here everything that would be important to learn for php programming? I want to learn Link to comment https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121234 Share on other sites More sharing options...
coupe-r Posted October 11, 2010 Share Posted October 11, 2010 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 https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121237 Share on other sites More sharing options...
josuenerd Posted October 11, 2010 Author Share Posted October 11, 2010 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 https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121240 Share on other sites More sharing options...
coupe-r Posted October 11, 2010 Share Posted October 11, 2010 No, that is correct, I made the mistake. On your insert.php page, at the very top, try adding this if(!isset($_POST['firstname']) || !isset($_POST['lastname']) || !isset($_POST['age'])) { header('Location: submit.php'); } Link to comment https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121242 Share on other sites More sharing options...
josuenerd Posted October 11, 2010 Author Share Posted October 11, 2010 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 https://forums.phpfreaks.com/topic/215597-i-am-trying-to-create-a-simple-php-project-need-help/#findComment-1121245 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.