Adding User input to database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yoursanjay
Forum Newbie
Posts: 17
Joined: Sat Feb 23, 2008 12:21 pm

Adding User input to database

Post by yoursanjay »

I am a new in PHP. It is a script to adding user data to the database but it is not working, no data is being stored into database as well as no error msg is being shown. I have attached the sql file also. plz help :)

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Adding data to the database</title>
</head>
<body>
<form action="bluebell.php" method="POST">
<input type='text' name='user_name'>Your User Name:
<p><input type='password' name='password'>Your Password:</p>
<p><select name='prefix'>
<option value='Mr.'>Mr</option>
<option value='Mrs.'>Mrs</option>
<option value='Dr.'>Dr</option>
</select></p>
<p><input type='text' name='first_name'>Your First Name:</p>
<p><input type='text' name='last_name'>Your Last Name:</p>
<p><input type='text' name='email'>Your Email Id:</p>
<p><input type='text' name='address1'>Your Address Line1:</p>
<p><input type='text' name='address2'>Your Address Line2:</p>
<p><input type='text' name='city'>Your City:</p>
<p><input type='text' name='state'>Your State:</p>
<p><input type='text' name='code'>Your Postal Code:</p>
<p><input type='text' name='country'>Your Country:</p>
<p><input type='text' name='phone'>Your Phone Number:</p>
   
<p><input type='submit' value='Submit'></p>
 
</form>
 
 
<?php 
$database="bluebell";
$username="root";
$password="";
 
$connect=mysql_connect("localhost",$username,$password);
   if(!$connect)
   {
     die("Couldn't connect:" . mysql_error());
   }
   
$prefix=$_POST['prefix'];
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$email=$_POST['email'];
$address1=$_POST['address1'];
$address2=$_POST['address2'];
$city=$_POST['city'];
$state=$_POST['state'];
$code=$_POST['code'];
$country=$_POST['country'];
$phone=$_POST['phone'];
   
   $db=mysql_select_db($database,$connect);
   if(!$db)
   {
    die("Couldn't select database:" . mysql_error());
   }
          
  mysql_query("INSERT INTO user (user_name,password,prefix,first_name,last_name,email,address1,address2,city,state,code,country,phone)  values ('$user_name','$password','$prefix','$first_name','$last_name','$email','$address1','$address2','$city','$state','$code','$country','$phone')");
  mysql_close($connect);
 
?> 
 </body>
</html>
 
 
------------------------------------------------------------------------------
SQL
------------------------------------------------------------------------------

-- phpMyAdmin SQL Dump
-- version 2.10.3
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jul 19, 2008 at 04:34 AM
-- Server version: 5.0.45
-- PHP Version: 5.2.3

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `bluebell`
--

-- --------------------------------------------------------

--
-- Table structure for table `user`
--

CREATE TABLE `user` (
`user_Id` int(10) NOT NULL default '0',
`user_name` varchar(250) collate latin1_general_ci NOT NULL,
`password` varchar(250) collate latin1_general_ci NOT NULL,
`prefix` char(5) collate latin1_general_ci default NULL,
`first_name` varchar(250) collate latin1_general_ci default NULL,
`last_name` varchar(250) collate latin1_general_ci default NULL,
`email` varchar(250) collate latin1_general_ci default NULL,
`address1` text collate latin1_general_ci,
`address2` text collate latin1_general_ci,
`city` varchar(250) collate latin1_general_ci default NULL,
`state` varchar(250) collate latin1_general_ci default NULL,
`code` varchar(250) collate latin1_general_ci NOT NULL,
`country` varchar(250) collate latin1_general_ci default NULL,
`phone` varchar(25) collate latin1_general_ci NOT NULL,
PRIMARY KEY (`user_Id`),
KEY `user_name` (`user_name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

--
-- Dumping data for table `user`
--

INSERT INTO `user` (`user_Id`, `user_name`, `password`, `prefix`, `first_name`, `last_name`, `email`, `address1`, `address2`, `city`, `state`, `code`, `country`, `phone`) VALUES
(0, '', '', '', '', '', '', '', '', '', '', '', '', '0'); :?:
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Adding User input to database

Post by jaoudestudios »

Just had a quick glance, I think all your entries are trying to go into the first row every time as there is no auto increment and they are empty because you have not closed the html input tags! (they are self closing - use />)

Change your database table to have an auto increment on the user_id

No offense but think you need to read up on the basics! :wink: Boring I know, but you will get a lot further as you wont stumble on school girl errors.

To get more information from apache, change the php.ini file to show notices as well as errors (testing/development mode)
yoursanjay
Forum Newbie
Posts: 17
Joined: Sat Feb 23, 2008 12:21 pm

Re: Adding User input to database

Post by yoursanjay »

Thanx.. I have solved it and the script is running.
Post Reply