Why record is not inserted in the DB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Muhammad Qaiser
    New Member
    • Dec 2010
    • 6

    Why record is not inserted in the DB

    Dears
    I am new to OOP Php. for now i am entangled at very initial level i.e. data insertion into db. I simply passed the insert query to a variable in simple php file, which i further get in class function executeQuery(). Although, on echoing $query variable shows right output yet, i cannot find out why data is not going to database. The class code is as under:

    Code:
    <?php
    class DBCon
    {
    	var $con;
    	function getCon()
    	{
    		$hostname = 'localhost';
    		$dbuser = 'root';
    		$dbpassword = '';
    		
    		$con=mysql_connect($hostname,$dbuser,$dbpassword);
    		if(!$con)
    		{
    			die('cannot connect db'.mysql_error());
    		}
    		mysql_select_db("studyco",$con);
    		return $con;
    	}
    
            //Queries
    	function executeQuery($query)
    	{
    		if($query!= null)
    		{
    			mysql_query($query);
    			echo 'record added'.$query;
    		} 
    			else 
    			{
    				echo 'not added';
    			}
    	}	
    	function destruct()
    	{
    		mysql_close();
    	}
    }
    ?>
    i would be extremely obliged for anyone's help in this regard.

    Thanks
    Qaiser
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    #2
    i cannot see your code anywhere this code is for the class how are you calling the function and declaring object for the class no one is going to write the code for you we can suggest fixing.
    regards,
    Omer Aslam

    Comment

    • Muhammad Qaiser
      New Member
      • Dec 2010
      • 6

      #3
      Dear Umer Butt
      Thanks alot for your quick response. I actually have this code in Php file which is as follows: sorry for inconvenience
      Moreover, html code is is also placed at the end of this thread i.e. after php code
      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" />
      <?php require_once('classes/db_con.class.php'); ?>
      <?php require_once('classes/parentinfo.class.php'); ?>
      </head>
      
      <body>
      <?php
      $stdbcon = new DBCon();
      if(!$stdbcon->getCon())
      {
      	echo 'Connection not established with the DB';
      } 	else
      	{
      		
      		$parentdetail=new ParentInfo();
      		
      		if(isset($_POST['btnSubmit']))
      		{				
      			$parenttype = $_POST['comboParentType'];
      			$firstName = $_POST['txtFName'];
      			$lastName = $_POST['txtLName'];
      			$FHName = $_POST['txtFHName'];
      			$gender = $_POST['comboGender'];
      			$profession = $_POST['txtProfession'];
      			$contactoff = $_POST['txtContactOff'];
      			$contacthome = $_POST['txtContactHome'];
      			$mobile = $_POST['txtContactMobile'];
      			$email = $_POST['txtEmail'];
      			$address = $_POST['txtAddress'];
      			$snap = $_POST['image'];
      			
      			//$parentdetail->addStudentInfo();
      			
      			$query="INSERT INTO parent (firstname,lastname,gender,father_husbandname,profession,contactoffice,contacthome,mobile,email,address,snap,alt_parentid) 
      			VALUES ('".$firstName."', '".$lastName."', '".$gender."', '".$FHName."', '".$profession."', '".$contactoff."', '".$contacthome."', '".$mobile."', '".$address."', '".$snap."', '".$parenttype."')";
      			$stdbcon->executeQuery($query);
      			
      		}
      		
      	}
      ?>
      </body>
      </html>

      html code
      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>Parent Information</title>
      <link type="text/css" href="css/style.css" media="all" rel="stylesheet"/>
      </head>
      
      <body>
      <form action="parent.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
        <table width="80%" align="center">
          <tr>
            <td colspan="3"><div align="center" class="pageHeading">PARENT DETAIL</div></td>
          </tr>
          <tr>
            <td colspan="3">&nbsp;</td>
          </tr>
          <tr>
            <td width="33%"><div align="right">Parent Type</div></td>
            <td width="4%">&nbsp;</td>
            <td width="63%"><select name="comboParentType" id="comboParentType">
            <option value="1" selected="selected">Father</option>
            <option value="2">Mother</option>
            <option value="3">Guardian</option>
            <option value="4">Other</option>
            </select></td>
          </tr>
          <tr>
            <td><div align="right">First Name</div></td>
            <td>&nbsp;</td>
            <td><input type="text" name="txtFName" id="txtFName" /></td>
          </tr>
          <tr>
            <td><div align="right">Last Name</div></td>
            <td>&nbsp;</td>
            <td><input type="text" name="txtLName" id="txtLName" /></td>
          </tr>
          <tr>
            <td><div align="right">Father / Husband Name</div></td>
            <td>&nbsp;</td>
            <td><input type="text" name="txtFHName" id="txtFHName" /></td>
          </tr>
          <tr>
            <td><div align="right">Gender</div></td>
            <td>&nbsp;</td>
            <td><select name="comboGender" id="comboGender">
            <option value="1" selected="selected">Male</option>
            <option value="2">Female</option>
            <option value="3">Eunuch</option>
            </select></td>
          </tr>
          <tr>
            <td><div align="right">Profession</div></td>
            <td>&nbsp;</td>
            <td><input type="text" name="txtProfession" id="txtProfession" /></td>
          </tr>
          <tr>
            <td><div align="right">Contact (Office)</div></td>
            <td>&nbsp;</td>
            <td><input type="text" name="txtContactOff" id="txtContactOff" /></td>
          </tr>
          <tr>
            <td><div align="right">Contact (Home)</div></td>
            <td>&nbsp;</td>
            <td><input type="text" name="txtContactHome" id="txtContactHome" /></td>
          </tr>
          <tr>
            <td><div align="right">Contact (Mobile)</div></td>
            <td>&nbsp;</td>
            <td><input type="text" name="txtContactMobile" id="txtContactMobile" /></td>
          </tr>
          <tr>
            <td><div align="right">Email</div></td>
            <td>&nbsp;</td>
            <td><input type="text" name="txtEmail" id="txtEmail" /></td>
          </tr>
          <tr>
            <td><div align="right">Residental Address</div></td>
            <td>&nbsp;</td>
            <td><input type="text" name="txtAddress" id="txtAddress" /></td>
          </tr>
          <tr>
            <td><div align="right">Snap</div></td>
            <td>&nbsp;</td>
            <td><label>
              <input type="file" name="image" id="image" />
            </label></td>
          </tr>
          <tr>
            <td colspan="3">&nbsp;</td>
          </tr>
          <tr>
            <td height="45" colspan="2"><label>
              <div align="right">
                <input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" />
                </label>
            </div></td>
            <td><label>
              <input type="button" name="button2" id="button2" value="Cancel" />
            </label></td>
          </tr>
        </table>
        <div align="center"></div>
      </form>
      </body>
      </html>

      Comment

      • omerbutt
        Contributor
        • Nov 2006
        • 638

        #4
        i could not see if you are verifying your mysql query
        please replace your class with the following code
        [code=php]
        <?php
        class DBCon
        {
        var $con;
        function getCon()
        {
        $hostname = 'localhost';
        $dbuser = 'root';
        $dbpassword = '';

        $con=mysql_conn ect($hostname,$ dbuser,$dbpassw ord);
        if(!$con)
        {
        die('cannot connect db'.mysql_error ());
        }
        mysql_select_db ("studyco",$con );
        return $con;
        }

        //Queries
        function executeQuery($q uery)
        {
        if($query!= null)
        {
        mysql_query($qu ery) or die(mysql_error ());
        echo 'record added'.$query;
        }
        else
        {
        echo 'not added';
        }
        }
        function destruct()
        {
        mysql_close();
        }
        }
        ?>


        [/code]

        Comment

        • AutumnsDecay
          New Member
          • Mar 2008
          • 170

          #5
          Try this.. replace this:

          Code:
          $query="INSERT INTO parent (firstname,lastname,gender,father_husbandname,profession,contactoffice,contacthome,mobile,email,address,snap,alt_parentid) 
                      VALUES ('".$firstName."', '".$lastName."', '".$gender."', '".$FHName."', '".$profession."', '".$contactoff."', '".$contacthome."', '".$mobile."', '".$address."', '".$snap."', '".$parenttype."')";
                      $stdbcon->executeQuery($query);
          with this:

          Code:
          $query="INSERT INTO `parent` (firstname,lastname,gender,father_husbandname,profession,contactoffice,contacthome,mobile,email,address,snap,alt_parentid) 
                      VALUES ('$firstName', '$lastName', '$gender', '$FHName', '$profession', '$contactoff', '$contacthome', '$mobile', '$address', '$snap', '$parenttype')";
                      $stdbcon->executeQuery($query);

          Also, did you make sure to add a primary, auto-incrementing key to your table?

          Comment

          • Muhammad Qaiser
            New Member
            • Dec 2010
            • 6

            #6
            Dear Umer and AutumnsDecay

            Thaaaanks alot for your help. Replies from both of you indeed helped me identify and resolve the issue. Actually i was missing a variable in the query. Therefore, there was discrepancy in the table fields and values of query. Thus the record was not going to the db. Now this issue is up.
            Thanks alot once again

            Comment

            • AutumnsDecay
              New Member
              • Mar 2008
              • 170

              #7
              Ah yes, I do see now that you missed the $email variable in the query.

              Glad you got it resolved, and glad we could help.

              Comment

              • omerbutt
                Contributor
                • Nov 2006
                • 638

                #8
                Sure
                glad to help out
                regards,
                Omer Aslam

                Comment

                Working...