Jump to content

[SOLVED] simple php entry of data into sql database.. why doesn't it work?


ethan6

Recommended Posts

here's the code, any ideas why it doesn't work? you can see it at http://myipodbroke.com/ADMIN-database.php

 

<form action="ADMIN-database.php" method="post">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
Phone: <input type="text" name="address1"><br>
Mobile: <input type="text" name="address2"><br>
Fax: <input type="text" name="city"><br>
E-mail: <input type="text" name="state"><br>
Web: <input type="text" name="zip"><br>
Web: <input type="text" name="sendemail"><br>
Web: <input type="text" name="phone"><br>
Web: <input type="text" name="refer"><br>
Web: <input type="text" name="refer2"><br>
Web: <input type="text" name="generation"><br>
Web: <input type="text" name="promo"><br>
Web: <input type="text" name="problems"><br>
<input type="Submit">
</form>

<?php
$username = "user";
$password = "pass";
$hostname = "localhost";
$database = "database";
mysql_connect($hostname, $username, $password)
or die("Unable to connect to database.");
mysql_select_db($database) 
or die("Could not select myipodbr_ipodusers");
$query="CREATE TABLE 
contacts 
(id int(6) NOT NULL auto_increment,
fname varchar(15) NOT NULL,
lname varchar(15) NOT NULL,
address1 varchar(20) NOT NULL,
address2 varchar(20) NOT NULL,
city varchar(20) NOT NULL,
state varchar(10) NOT NULL,
zip varchar(10) NOT NULL,
sendemail varchar(20) NOT NULL,
phone varchar(20) NOT NULL,
refer varchar(20) NOT NULL,
refer2 varchar(20) NOT NULL,
generation varchar(20) NOT NULL,
promo varchar(20) NOT NULL,
problems varchar(20) NOT NULL,
PRIMARY KEY (id),
UNIQUE id (id),
KEY id_2 (id))";
mysql_query($query);

$query = "INSERT INTO contacts VALUES ('','$fname','$lname','$address1','$address2','$city','$state','$zip','$sendemail','$phone','$refer','$refer2','$generation','$promo','$problems')";
mysql_query($query);

$query="SELECT * FROM contacts";
$result=mysql_query($query);

$num=mysql_numrows($result);
echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$id=mysql_result($result,$i,"id");
$fname=mysql_result($result,$i,"fname");
$lname=mysql_result($result,$i,"lname");
$address1=mysql_result($result,$i,"address1");
$address2=mysql_result($result,$i,"address2");
$city=mysql_result($result,$i,"city");
$state=mysql_result($result,$i,"state");
$zip=mysql_result($result,$i,"zip");
$sendemail=mysql_result($result,$i,"sendemail");
$phone=mysql_result($result,$i,"phone");
$refer=mysql_result($result,$i,"refer");
$refer2=mysql_result($result,$i,"refer2");
$generation=mysql_result($result,$i,"generation");
$promo=mysql_result($result,$i,"promo");
$problems=mysql_result($result,$i,"problems");

echo "<b>$id</b><br>
<b>$fname $lname</b><br>
$address1<br>
$address2<br>
$city $state $zip<br>
$sendemail<br>
$phone<br>
$refer $refer2<br>
$generation<br>
$promo<br>
$problems<br>
<hr><br>";

$i++;
}

mysql_close();
?>

Link to comment
Share on other sites

Ok I did some changes (i had forgotten to add the POST, but it's there now and still doens't seem to add to database):

 

<form action="ADMIN-database2.php" method="post">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
<input type="submit">
</form>

<?php
$fname = $_POST['fname'];
$lname = $_POST['lname'];

$username = "username";
$password = "pass";
$hostname = "localhost";
$database = "database";
mysql_connect($hostname, $username, $password)
or die("Unable to connect to database.");
mysql_select_db($database) 
or die("Could not select myipodbr_ipodusers");
$query="CREATE TABLE 
contacts 
(id int(6) NOT NULL auto_increment,
fname varchar(15) NOT NULL,
lname varchar(15) NOT NULL,
PRIMARY KEY (id),
UNIQUE id (id),
KEY id_2 (id))";
mysql_query($query);

$query = "INSERT INTO contacts (fname, lname) VALUES ('','$fname','$lname')";
mysql_query($query);

$query="SELECT * FROM contacts";
$result=mysql_query($query) or die(mysql_error());

echo "<b><center>Database Output</center></b><br><br>";

while ($row = mysql_fetch_assoc($result))
{
echo "<b>{$row['fname']} {$row['lname']}</b><br>";
}

mysql_close();
?>

Link to comment
Share on other sites

1. You're script is trying to run the database portion everytime (even when you just load the page and only want the form)

 

2. You don't secure your input

 

3. You attempt to create a new db table everytime the page loads

 

4. You're insert query is wrong. "INSERT INTO contacts (fname, lname) VALUES ('','$fname','$lname')" should be "INSERT INTO contacts (fname, lname) VALUES ('$fname','$lname')"

 

5. No need to close the db connetion at the end of the script.

Link to comment
Share on other sites

I found that so many times when my inserts didn't work it was due to having a fieldname out of order, you have to put all the info in there exactly as they are laid out in the table like if it's name, dob, address you can't do dob, name, address on the insert...

Link to comment
Share on other sites

I found that so many times when my inserts didn't work it was due to having a fieldname out of order, you have to put all the info in there exactly as they are laid out in the table like if it's name, dob, address you can't do dob, name, address on the insert...

 

You can put them in any order as long as the required fields are there

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.