I’ve used pre-made PHP code before to communicate with MySQL databases before, but now I am trying to dig a little deeper and write my own codes to broaden my scope of abilities and possibly create some helpful tools using my own code.
I am trying to create a basic contacts database. I am stumped to why this code isn’t working. Here is what I have so far:
My Input Page:
<html>
<head>
<title>Insert a Contact</title>
</head>
<body>
<form action="insert.php" method="post">
First Name: <input type="text"
name="first"><br>
Last Name: <input type="text"
name="last"><br>
Phone: <input type="text" name="phone"><br>
Mobile: <input type="text"
name="mobile"><br>
Fax: <input type="text" name="fax"><br>
E-mail: <input type="text" name="email"><br>
Web: <input type="text" name="web"><br>
Comments: <input type="text"
name="misc"><br>
<input type="Submit">
</form>
</body>
</html>
Server Side Post PHP:
<?
$username="XXXXXXXXX";
$password="XXXXXXX";
$database="db345";
$first=$_POST['first'];
$last=$_POST['last'];
$phone=$_POST['phone'];
$mobile=$_POST['mobile'];
$fax=$_POST['fax'];
$email=$_POST['email'];
$web=$_POST['web'];
$misc=$_POST['misc'];
mysql_connect("dbxxxxx.pxxxx.net",$username,$password);
@mysql_select_db($database) or die( "Unable to select
database");
$query = "INSERT INTO contacts VALUES
('','$first','$last','$phone','$mobile','$fax','$email','
$web','$misc')";
mysql_query($query);
mysql_close();
?>
My Output Page:
<?
$username="xxxxxxx";
$password="xxxxxxxxxxxx";
$database="xxxxxxxx";
mysql_connect("dbxxxxx.pxxxx.net",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo "<b><center>Database
Output</center></b><br><br>";
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
$misc=mysql_result($result,$i,"misc");
echo "<b>$first $last</b><br>Phone:
$phone<br>Mobile: $mobile<br>Fax:
$fax<br>E-mail: $email<br>Web:
$web<br>Comments: $misc<hr><br>";
$i++;
}
?>
I’m pretty sure the problem is in the PHP that facilitates the post but I’m not too sure.
Any advice will be greatly appreciated.