Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data from PHP form into MySQL Database

Status
Not open for further replies.

likelylad

IS-IT--Management
Jul 4, 2002
388
GB
Hi

I am completely new to this area.
I have a database on a MySQL Server.
I also have a PHP form to allow me to add data to the Database.

I can read data from the database using PHP files.

When I execute the code I am getting no error messages.

Can some tell me what is wrong

<html>

<body>



<?php



if ($submit) {

// process form

$db = mysql_connect(&quot;localhost&quot;, &quot;root&quot;);

mysql_select_db(&quot;mydb&quot;,$db);

$sql = &quot;INSERT INTO employees (first,last,address,position) VALUES ('$first','$last','$address','$position')&quot;;

$result = mysql_query($sql);

echo &quot;Thank you! Information entered.\n&quot;;

} else{



// display form



?>



<form method=&quot;post&quot; action=&quot;<?php echo $PHP_SELF?>&quot;>

First name:<input type=&quot;Text&quot; name=&quot;first&quot;><br>

Last name:<input type=&quot;Text&quot; name=&quot;last&quot;><br>

Address:<input type=&quot;Text&quot; name=&quot;address&quot;><br>

Position:<input type=&quot;Text&quot; name=&quot;position&quot;><br>

<input type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Enter information&quot;>

</form>



<?php



} // end if



?>



</body>



</html>

 
Depends. Can you tell us what the code is or is not doing? ______________________________________________________________________
TANSTAAFL!
 
Hi sleipnir214

The database is called mydb
It contains a table called employees
The employee table contains 5 fields.
1)id (this is the primary field, auto incremented)
2)first
3)last
4)address
5)position

I want to use the form to enter data into this table.

When I hit the submit button, the form is cleared, no error messages are given but the database is not updated.



 
You're only going to get errors sent to the output stream if you have major errors.

An error of, for example, a malformed SQL statement will output no errors. You must explicitly check for them yourself.

What is the value of $result after the update statement? If it's FALSE, then you have some error with your SQL statement. Output the return of mysql_error() to see what it was.

Print out the value of $sql and inspect the string to see where your error is. The most common cause of errors where you are building the string from variable value concatenation is that one of your variables doesn't have the value you think it should. If the value of $sql looks okay, copy the string and paste it to whatever MySQL tool you use and execute it. See what return it produces. ______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top