Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

html form connect to mysql database

mysql

  • Please log in to reply
13 replies to this topic

#1 farhanyun91

farhanyun91

    CC Regular

  • Member
  • PipPipPip
  • 46 posts

Posted 29 February 2012 - 07:44 AM

hello there, i am a novice in web development program and still got lot of things to learn.Im having a problem with the connection between html form and mysql.as far as i know, we need to link html form to a php code($_POST).but what really wonders me is, where should i put the mysql_connection in?is it in the same file as i put the $_POST or what? and i don't really get it about adding a new row into MySQL.i do know that i can use the

$sql="INSECT INTO mydb(name,age)

Values('farhan', 21)
what wonders me is, how to make it more interactive?meaning to say, when user submit the form that has been filled in by pressing the submit button, the form then will be in the first row,followed with the second one with respective row and so forth.from what i've learned, the form will be sent to a file that contain $_POST but how will it be sent to MySQL database from $_POST?
and one more thing, what is the actual purpose of coding SQL in the same area as coding php when we can use myPHPAdmin to do the sql things?it seems like create a table and add a new row is easier to be done in myPhpAdmin than in the same area as php codes is in,am i right?Please enlighten me more about this subject.thank you

---------- Post added at 03:44 PM ---------- Previous post was at 03:38 PM ----------

and i have this code that doesn't work as expected


<html>

<head>

<title>Enrollment</title>

</head>

<body>

<form action="EnrolmentResult.php" method="get">



<fieldset>

<legend>Personal Details:</legend>



Name:     <input type="text" name="UserName"/></br>



Gender:   

Male<input type="radio" name="UserGender" value="Male"/>

Female<input type="radio" name="UserGender" value="Female"/></br>



<p> </p>



Address:</br><textarea name="userAddress" cols="30" rows="10" size="5"></textarea></br>



IC Number: <input type="text" name="ICNumber" maxlength="12"/>



<hr size="1S" noshade>



<fieldset>

<legend>Course Detail</legend>



Course Name: <select name="CourseName" size="1">

<option selected="selected">Bachelor of Computer Science</option>

<option>Bachelor of Multimedia</option>

<option>Bachelor of Graphic Design</option>

</select>



</br>



Subject to add:</br>

<input type="checkbox" name="subject" value="IAS 2243" checked="checked">IAS 2243

<input type="checkbox" name="subject" value="ITD 3194" checked="checked">ITD 3194

</br>

<input type="checkbox" name="subject" value="IKS1143" checked="checked">IKS1143

 <input type="checkbox" name="subject" value="ISS2131" checked="checked">ISS2131



</fieldset>



<hr size="1S" noshade>



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

<input type="reset" value="Reset"/>

</fieldset>



</form>

</body>

</html>

and this is the process code

<html>

<head>

<title>Enrollment Result</title>

</head>

<body

<? php

$Name=$_POST['UserName'];

$ICNumber=$_POST['ICNumber'];

$Address=$_POST['userAddress'];

$Gender=$_POST['userAddress'];

$ProgName=$_POST['CourseName'];

$Subj=$_POST['subject'];



echo "$Name";

echo "$ICNumber";

echo "$Address";

echo "$Gender";

echo "$ProgName";

echo "$Subj";



?>

</body>

</html>

what are the errors that i can't see????
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 29 February 2012 - 08:06 AM

PhpMyAdmin is an administration tool, meaning, you use it for administrative purposes, such as designing and setting up new tables, checking records to diagnose and debug code, modifying table structures when new features in your code are required. It is not a tool which should be used by programs which interact with the database. This is the reason for writing your SQL queries inside your PHP code.

Your PHP script is a little program which will interact with the database. It will tell the database, "I have some data here for you to store," (INSERT), and the database will follow your program's commands. These commands must be written in SQL, a language which the database understands.

Each PHP script serves a very specific purpose in your database application--that is, it performs one task and only that task. As such, it makes sense to write one SQL query that will be stored inside your PHP script and executed whenever the script gets called, since that SQL query describes the function that particular PHP script is supposed to do.

In order to translate the fields in your HTML form into a new row in your table, you must construct a SQL query with the data retrieved from your HTML form ($_POST). This part you have to do by hand. I would recommend looking into Prepared Statements in PHP to learn how to insert the data from the HTML form into the SQL query.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 farhanyun91

farhanyun91

    CC Regular

  • Member
  • PipPipPip
  • 46 posts

Posted 29 February 2012 - 08:15 AM

as what i understand, myPHPadmin is for DDL purpose while query is for DML purpose,is it?and when a user submit a new form, do i need to do the query and do it again for another form?is that how it goes?do the query whenever new row is to be inserted?

is that meant i will need to do query and database connection in the same file as i do $_POST?
  • 0

#4 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 29 February 2012 - 09:44 AM

Essentially, yes, phpMyAdmin is for all your DDL stuff, while your PHP script performs all your DML stuff, although you can do some DML stuff in phpMyAdmin, but you should really only limit this to debug purposes.

Your PHP script pseudocode should look something like this:
Fetch form field data from $_POST.
Prepare an INSERT SQL query.
Bind the form field values to the parameters in the query.
Execute the query.
Echo the results (if you want some sort of confirmation).

  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5 farhanyun91

farhanyun91

    CC Regular

  • Member
  • PipPipPip
  • 46 posts

Posted 29 February 2012 - 11:38 AM

i put a situation where user need to fill in a html form and it will be added in database,so is this the correct way of doing it

//this code will be in profile.htm file
<html>
<body>
<form action="process.php method="post">
Name<input type="text" name="userName"/></br>;
Age<input type="text" name="userAge"/></br>;
<input type="submit" value="submit"/>
</form>
</body>
</html>


//this will be in process.php file
//the $_post code
<html>
<php?
$userName="$_POST['userName'];
$userAge="$_POST['userAge'];
echo "your name is".$userName."""and you are".$userAge.
//the php mysql code
$host="localhost";
$users="root";
$psword="";
$dbname="my_DB";
$con=mysql_connect("$host", "$users", "$psword");
select_db_name("$con","my_DB");
or die(mysql_error());
$query="INSERT INTO tab_name(username, age)Values(.$userName.$userAge);
$result=mysql_query("$con","$query") or die("Can't execute");
mysql_close=("$con);

is this the way to add data to database?is this how it works and should be done?
  • 0

#6 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 29 February 2012 - 02:39 PM

Yes, if you oversee some minor syntax errors, that is the basics of how you do.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#7 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 01 March 2012 - 07:27 AM

Yes, but you are using regular statements without sanitizing your inputs, rather than using Prepared Statements like I suggested. Your method above will work, once the syntax errors are corrected, but it will leave you wide open to SQL Injection attacks.

For a tutorial on Prepared Statements, read this article:
Prepared Statements in PHP and MySQLi – Notebook | MattBango.com
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#8 farhanyun91

farhanyun91

    CC Regular

  • Member
  • PipPipPip
  • 46 posts

Posted 01 March 2012 - 10:52 AM

i have these two codes that seems couldn't display output. i don't know what i did wrong in these two codes.please check up for me.

//this is in enrollment.htn
<html>
<head>
<title>Enrollment</title>
</head>
<body>
<form action="enrolmentResult.php" method="post">

<fieldset>
<legend>Personal Details:</legend>

Name:     <input type="text" name="UserName"/></br>

Gender:   
Male<input type="radio" name="UserGender" value="Male"/>
Female<input type="radio" name="UserGender" value="Female"/></br>

<p> </p>

Address:</br><textarea name="userAddress" cols="30" rows="10" size="5"></textarea></br>

IC Number: <input type="text" name="ICNumber" maxlength="12"/>

<hr size="1S" noshade>

<fieldset>
<legend>Course Detail</legend>

Course Name: <select name="CourseName" size="1">
<option selected="selected">Bachelor of Computer Science</option>
<option>Bachelor of Multimedia</option>
<option>Bachelor of Graphic Design</option>
</select>

</br>

Subject to add:</br>
<input type="checkbox" name="addedSubject" value="IAS 2243" checked="checked">IAS 2243
<input type="checkbox" name="addedSubject" value="ITD 3194" checked="checked">ITD 3194
</br>
<input type="checkbox" name="addedSubject" value="IKS1143" checked="checked">IKS1143
 <input type="checkbox" name="addedSubject" value="ISS2131" checked="checked">ISS2131

</fieldset>

<hr size="1S" noshade>

<input type="submit" value="Submit"/>
<input type="reset" value="Reset"/>
</fieldset>

</form>
</body>
</html>


//this is in enrolmentresult.php
<html>
<head>
<title>Enrollment Result</title>
</head>
<body
<? php

//initialize db data to var
$host="localhost";
$db_Name="enrolment";
$psword="";
$user="root";

//connection
$con=mysql_connect($host, $db_Name, $psword, $user)
or die(mysql_error());

//select database
mysql_select_db($con,$db_Name)
or die("Couldn't select database");

$Name=$_POST['UserName'];
$ICNumber=$_POST['ICNumber'];
$Address=$_POST['userAddress'];
$Gender=$_POST['UserGender'];
$ProgName=$_POST['CourseName'];
$Subj=$_POST['addedSubject'];

//insert row to student table
$queryStud="INSERT INTO student (UserName, ICNumber, userAddress, UserGender) Values('$Name','$ICNumber','$Address','$Gender')
mysql_query($queryStud,$con) or die(mysql_error());

//insert row to subject table
$querySubj="INSERT INTO subject (CourseName, addedSubject) Values('$ProgName','$Subj')
mysql_query($querySubj,$con) or die(mysql_error());

//display output
echo"Successfuly added record";


?>
</body>
</html>

the output doesn't even appear in the browser.what are the errors and how am i suppose to solve them??
  • 0

#9 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 01 March 2012 - 11:37 AM

I think you might have some naming issues with your html form controls. I believe all the check boxes need to be named something different, since they're not radio buttons.

Try forgetting about all the database code for now and just echo your $_POST variables. What output are you getting?
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#10 farhanyun91

farhanyun91

    CC Regular

  • Member
  • PipPipPip
  • 46 posts

Posted 01 March 2012 - 06:13 PM

oh yeah.i notice the mistake there.well, when i run the program, only the form which is in enrollment.html file that appear in the browser.when i clicked the submit button,nothing was appear.i've checked the file directory, and everything is seems okay.i created a file named codes in the htdocs document and the directory looks like this C:\xampp\htdocs\Codes.why does the output doesn't come out?
  • 0

#11 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 02 March 2012 - 06:19 AM

Back up a second. What's the code for your HTML form and your PHP script that you're using for your simplified echo trial? Double check to ensure you have all your form control names correct. What's the output when you just try to echo the form fields back to the browser?
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#12 farhanyun91

farhanyun91

    CC Regular

  • Member
  • PipPipPip
  • 46 posts

Posted 05 March 2012 - 08:15 PM

Back up a second. What's the code for your HTML form and your PHP script that you're using for your simplified echo trial? Double check to ensure you have all your form control names correct. What's the output when you just try to echo the form fields back to the browser?


the output from echo the form is the form that i named it as enrolment. the form is displayed successfully. however, when im trying to submit the data in to the database, nothing comes out. didn't display anything.i've checked everything and it is seems okay.does it got anything to do with something that is out of code?my server or something?Im using xampp anyway.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download