View previous topic :: View next topic |
Author |
Message |
biggersm -
Joined: 28 Jan 2004 Posts: 5
|
Posted: Tue Jul 06, 2004 12:05 am Post subject: Would anyone mind some hand holding help... |
|
|
Can anyone give me the most basic code examples for making my html form post to a mysql database.. All I need is a first name, last name, email, submit... That's it. I am so much more of a " monkey see moneky do" learner.
I have downloaded and followed the tutorials for PHP, myphpadmin, and MySql. ( they were very good by the way...http://os17fan.cjb.net ) I have a green light, php is installed correctly ( I know because I use it to send the web form information to my email..) and phpAdmin is working because I can use it too make dbases.
I can make a simple database without too much trouble. It's writing the PHP that is confusing me.
Thank you sooooo much!! |
|
Back to top |
|
 |
biggersm -
Joined: 28 Jan 2004 Posts: 5
|
Posted: Tue Jul 06, 2004 3:37 am Post subject: |
|
|
ok...
here is MY code, and why doesn't it work...
Code: | <?php
$DBhost = "localhost";
$DBuser = "me";
$DBpass = "mypwd";
$DBName = "example";
$table = "details";
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
@mysql_select_db("$DBName") or die("Unable to select database $DBName");
$sqlquery = "INSERT INTO $table VALUES('$id','$name','$telephone','$birthday')";
$results = mysql_query($sqlquery);
mysql_close();
print "<html><body><center>";
print "<p>You have just entered this record<p>";
print "Name : $name<br>";
print "Telephone : $telephone<br>";
print "Birthday :$birthday";
print "</body></html>";
?>
|
And here is the html code
Code: | <html>
<head>
</head>
<center>
<form method="post" action="script.php">
<input type="hidden" name="id" value="null">
<table>
<tr><td align="left">Name</td>
<td><input type="text" name="name"></td>
</tr>
<tr><td align="left">Telephone</td>
<td><input type="text" name="telephone" size="20"></td>
</tr>
<tr><td align="left">Birthday</td>
<td><input type="text" name="birthday" size="20"></td>
</tr>
<tr><td colspan="2">
<p align="center">
<input type="submit" value="Enter record">
</td>
</tr>
</table>
</form>
</center>
</html>
|
I know that the db is there.. I created it in myphpadmin and verified it was there in the command line...
help... PLEASE!!! |
|
Back to top |
|
 |
roganty -
Joined: 08 Jun 2004 Posts: 357 Location: Bristol, UK
|
Posted: Tue Jul 06, 2004 10:38 am Post subject: |
|
|
Lets looks at the following lines:
Code: | <?php
$DBhost = "localhost";
$DBuser = "me";
$DBpass = "mypwd";
$DBName = "example";
$table = "details";
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
@mysql_select_db("$DBName") or die("Unable to select database $DBName");
$sqlquery = "INSERT INTO $table VALUES('$id','$name','$telephone','$birthday')";
$results = mysql_query($sqlquery);
mysql_close();
...
|
This is how i would connect to a db:
Code: |
<?php
$DBuser = "me";
$DBpass = "mypwd";
$DBName = "example";
global $link;
$link = mysql_connect( "localhost", $DBuser, $DBpass );
if ( ! $link ) die( "Couldn't connect to MySQL" );
mysql_select_db( $DBName, $link ) or die ( "Couldn't open $DBName: ".mysql_error() );
?>
|
First i would save the above as a seperate file, and include it at the top of every page
Then i would use the following code to insert the values into the table
Code: |
<?php
include("db_connect.php"); //name of file that connects to db
$table = "details";
$sqlquery = "INSERT INTO $table ( ID, Name, Telephone, Birthday)"; //These are the table headings
$sqlquery .= "values '$id', '$name', '$telephone', '$birthday')"; //These are the values that are inserted into the table
mysql_query( $sqlquery, $link ) or die( "Couldn't add data to \"$table\" table: " .mysql_error() );
mysql_close( $link );
?>
|
Thats it. the rest of your code is alright |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|