I am trying to link my php form and my sql but having difficulties
Posted
by
user1912599
on Stack Overflow
See other posts from Stack Overflow
or by user1912599
Published on 2012-12-18T11:01:32Z
Indexed on
2012/12/18
11:02 UTC
Read the original article
Hit count: 1217
I am not sure what I am doing wrong as far as my php goes but I can't get my form to link with my sql. Here are the codes for my form and php code for my link to sql
<?php
echo displayform();
function displayForm() {
$r = '';
//build it
$r .='<form action="database.php" method="post">';
//table
$r .=displayNiceFormBegin();
$r .=displayRow('FirstName:', '<input type="text" name="fname" id="fname"/>');
$r .=displayRow('LastName:', '<input type="text" name="lname" id="lname"/>');
$r .=displayRow('Address:', '<input type="text" name="address" id ="address"/>');
$r .=displayRow('Phone:', '<input type="text" name="phone" id ="phone"/>');
$r .=displayRow('Deparment:', '<input type="text" name="department"id="department"/>');
$r .=displayRow('', '<input type="submit" value="Submit Registration" />');
$r .=displayNiceFormEnd();
$r .='</form>';
return $r;
}
function displayRow($left, $right) {
$r .= '';
//build it
$r .='<tr>';
$r .= '<td>' . $left . '</td>';
$r .= '<td>' . $right . '</td>';
$r .='</tr>';
return $r;
}
function displayNiceFormBegin(){
$r .='';
//build it
$r .= '<table style="background-color: beige; border: 1px dashed #999"><tr><td>';
$r .='<table style="margin:10px">';
return $r;
}
function displayNiceFormENd() {
$r .='';
//build it
$r .='</table>';
$r .='</td></tr><table>';
return $r;
}
?>
<?php
$host="localhost"; // Host name
$username="695788_ogems"; // Mysql username
$password="opd69715"; // Mysql password
$db_name="ottawaglandorfems_zzl_ogems"; // Database name
$tbl_name=".*"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$address=$_POST['address'];
$phone=$_POST['phone'];
$department=$_POST['deparment'];
// Insert data into mysql
$sql="INSERT INTO $tbl_name(FirstName,LastName,Address,Phone,Department)VALUES('$fname', '$lname', '$address','$phone','$deparment')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='ottawa-glandorfems.org/form3.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysql_close();
?>
I keep getting an error. Thank you!!!!
© Stack Overflow or respective owner