SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jun 2004
    Location
    U.S.
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    cannot insert a record?

    Hi,

    I am so confused for those code which didn't allow me to add a record into database "surpro". Can anyone help me out this problem? Much appreciated!!

    Code:
    <!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=UTF-8" />
    <title>New member regiser</title>
    <?php ob_start();?>
    </head>
    
    <body>
    <div align="center" class="style1">
      <p>ABCMedianet New users  Registration</p>
    </div>
    <?php
    //getting today date
    $today=date("M/d/Y", time());
    //connecting to MYSQL
    $host="localhost";
    $user="root";
    $pass="root";
    $dbase="surpro";
    
    
    $account="";
    $pass1="";
    $pass2="";
    $telephone="";
    $email="";
    $address="";
    $date="";
    
    //if statement data assigns to variables
    if(isset($_POST['account'])){
    	$account= $_POST['account'];
    }
    if(isset($_POST['pass1'])){
    	$pass1= $_POST['pass1'];
    }
    if(isset($_POST['pass2'])){
    	$pass2= $_POST['pass2'];
    }
    if(isset($_POST['phone'])){
    	$telephone= $_POST['telephone'];
    }
    if(isset($_POST['email'])){
    	$email= $_POST['email'];
    }
    if(isset($_POST['address'])){
    	$address= $_POST['address'];
    }
    if(isset($_POST['date'])){
    	$date= $_POST['date'];
    }
    
    //only check 4 fields to see if it is not empty
    if($account !="" && $pass1 !="" && $pass2 !="" && $email !=""){
    	//connect to MYSQL
    	$link=mysql_connect($host, $user,$pass);
    	mysql_select_db($dbase,$link);
    	
    	//setting a connection string
    	$sql_str="select * from member where account = '".$account."'";
    	$result=mysql_query($sql_str,$link);
    	
    		if(mysql_fectch_row($result) == false)
    		{
    			//checking username and password
    			if($pass1==$pass2)
    			{
    			//executing the new action using string
    			$insert_str="Insert into member(account,password,telephone,email,address,date)Values('$account','$password','telephone','$email','$address',Now())";
    			//sending a query
    			mysql_query($insert_str);
    			
    			//adding new member successful
    			header("Location:member.php");
    		}else 
    			echo "<font color=red>Input account and password are invaild! Please re-input...</font>";
    		
    		}
    		else
    			echo "<font color=red>This account has been existing! Please re-input...</font>";
    		}
    	
    ?>
    <!--calling itself-->
    <form style="background-color:#ffffcc" name="form1" method="post" action="new.php">
    	<p>Login Account:<input type="text" name="account" />*<br />
           Login Password:<input type="password" name="pass1" />*<br />
           Confirm password:<input type="password" name="pass2"  />*<br />
           Telephone: <input type="text" name="telephone" /><br />
           Email: <input type="text" name="email" /><br />
           Address:<input type="text" name="address" size="50"/>
           Date of registered: <input type="text" name="date" value="<?=$today ?>" />
           <input type="submit" name="submit" value="Register" />
           <input type="reset" name="reset" value="Reset" />
    </form>
    <a href="main.php">Index</a>
    </body>
    </html>

  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_query($insert_str) or die(mysql_error()); should tell you what's wrong.

    if(mysql_fectch_row($result) == false) should probably be:
    if(mysql_num_rows($result) == 0) presuming you only want to to the INSERT if there was no record(s) returned from the SELECT above.

  3. #3
    SitePoint Addict CVPer's Avatar
    Join Date
    Sep 2007
    Location
    Vancouver, BC, Canada
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    there are few possible reasons:
    - you may not connect to the database
    - your select query may get nothing, then the insert query won't be executed
    - the sql of insert may have some errors
    * @location Vancouver, BC, Canada
    * @name Steve
    * @job PHP/MySQL, Drupal, WordPress Developer


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •