473,395 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class DBCon
  3. {
  4.     var $con;
  5.     function getCon()
  6.     {
  7.         $hostname = 'localhost';
  8.         $dbuser = 'root';
  9.         $dbpassword = '';
  10.  
  11.         $con=mysql_connect($hostname,$dbuser,$dbpassword);
  12.         if(!$con)
  13.         {
  14.             die('cannot connect db'.mysql_error());
  15.         }
  16.         mysql_select_db("studyco",$con);
  17.         return $con;
  18.     }
  19.  
  20.         //Queries
  21.     function executeQuery($query)
  22.     {
  23.         if($query!= null)
  24.         {
  25.             mysql_query($query);
  26.             echo 'record added'.$query;
  27.         } 
  28.             else 
  29.             {
  30.                 echo 'not added';
  31.             }
  32.     }    
  33.     function destruct()
  34.     {
  35.         mysql_close();
  36.     }
  37. }
  38. ?>
  39.  
i would be extremely obliged for anyone's help in this regard.

Thanks
Qaiser
Dec 23 '10 #1
7 1509
omerbutt
638 512MB
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
Dec 23 '10 #2
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
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <?php require_once('classes/db_con.class.php'); ?>
  6. <?php require_once('classes/parentinfo.class.php'); ?>
  7. </head>
  8.  
  9. <body>
  10. <?php
  11. $stdbcon = new DBCon();
  12. if(!$stdbcon->getCon())
  13. {
  14.     echo 'Connection not established with the DB';
  15. }     else
  16.     {
  17.  
  18.         $parentdetail=new ParentInfo();
  19.  
  20.         if(isset($_POST['btnSubmit']))
  21.         {                
  22.             $parenttype = $_POST['comboParentType'];
  23.             $firstName = $_POST['txtFName'];
  24.             $lastName = $_POST['txtLName'];
  25.             $FHName = $_POST['txtFHName'];
  26.             $gender = $_POST['comboGender'];
  27.             $profession = $_POST['txtProfession'];
  28.             $contactoff = $_POST['txtContactOff'];
  29.             $contacthome = $_POST['txtContactHome'];
  30.             $mobile = $_POST['txtContactMobile'];
  31.             $email = $_POST['txtEmail'];
  32.             $address = $_POST['txtAddress'];
  33.             $snap = $_POST['image'];
  34.  
  35.             //$parentdetail->addStudentInfo();
  36.  
  37.             $query="INSERT INTO parent (firstname,lastname,gender,father_husbandname,profession,contactoffice,contacthome,mobile,email,address,snap,alt_parentid) 
  38.             VALUES ('".$firstName."', '".$lastName."', '".$gender."', '".$FHName."', '".$profession."', '".$contactoff."', '".$contacthome."', '".$mobile."', '".$address."', '".$snap."', '".$parenttype."')";
  39.             $stdbcon->executeQuery($query);
  40.  
  41.         }
  42.  
  43.     }
  44. ?>
  45. </body>
  46. </html>
  47.  

html code
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Parent Information</title>
  6. <link type="text/css" href="css/style.css" media="all" rel="stylesheet"/>
  7. </head>
  8.  
  9. <body>
  10. <form action="parent.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
  11.   <table width="80%" align="center">
  12.     <tr>
  13.       <td colspan="3"><div align="center" class="pageHeading">PARENT DETAIL</div></td>
  14.     </tr>
  15.     <tr>
  16.       <td colspan="3">&nbsp;</td>
  17.     </tr>
  18.     <tr>
  19.       <td width="33%"><div align="right">Parent Type</div></td>
  20.       <td width="4%">&nbsp;</td>
  21.       <td width="63%"><select name="comboParentType" id="comboParentType">
  22.       <option value="1" selected="selected">Father</option>
  23.       <option value="2">Mother</option>
  24.       <option value="3">Guardian</option>
  25.       <option value="4">Other</option>
  26.       </select></td>
  27.     </tr>
  28.     <tr>
  29.       <td><div align="right">First Name</div></td>
  30.       <td>&nbsp;</td>
  31.       <td><input type="text" name="txtFName" id="txtFName" /></td>
  32.     </tr>
  33.     <tr>
  34.       <td><div align="right">Last Name</div></td>
  35.       <td>&nbsp;</td>
  36.       <td><input type="text" name="txtLName" id="txtLName" /></td>
  37.     </tr>
  38.     <tr>
  39.       <td><div align="right">Father / Husband Name</div></td>
  40.       <td>&nbsp;</td>
  41.       <td><input type="text" name="txtFHName" id="txtFHName" /></td>
  42.     </tr>
  43.     <tr>
  44.       <td><div align="right">Gender</div></td>
  45.       <td>&nbsp;</td>
  46.       <td><select name="comboGender" id="comboGender">
  47.       <option value="1" selected="selected">Male</option>
  48.       <option value="2">Female</option>
  49.       <option value="3">Eunuch</option>
  50.       </select></td>
  51.     </tr>
  52.     <tr>
  53.       <td><div align="right">Profession</div></td>
  54.       <td>&nbsp;</td>
  55.       <td><input type="text" name="txtProfession" id="txtProfession" /></td>
  56.     </tr>
  57.     <tr>
  58.       <td><div align="right">Contact (Office)</div></td>
  59.       <td>&nbsp;</td>
  60.       <td><input type="text" name="txtContactOff" id="txtContactOff" /></td>
  61.     </tr>
  62.     <tr>
  63.       <td><div align="right">Contact (Home)</div></td>
  64.       <td>&nbsp;</td>
  65.       <td><input type="text" name="txtContactHome" id="txtContactHome" /></td>
  66.     </tr>
  67.     <tr>
  68.       <td><div align="right">Contact (Mobile)</div></td>
  69.       <td>&nbsp;</td>
  70.       <td><input type="text" name="txtContactMobile" id="txtContactMobile" /></td>
  71.     </tr>
  72.     <tr>
  73.       <td><div align="right">Email</div></td>
  74.       <td>&nbsp;</td>
  75.       <td><input type="text" name="txtEmail" id="txtEmail" /></td>
  76.     </tr>
  77.     <tr>
  78.       <td><div align="right">Residental Address</div></td>
  79.       <td>&nbsp;</td>
  80.       <td><input type="text" name="txtAddress" id="txtAddress" /></td>
  81.     </tr>
  82.     <tr>
  83.       <td><div align="right">Snap</div></td>
  84.       <td>&nbsp;</td>
  85.       <td><label>
  86.         <input type="file" name="image" id="image" />
  87.       </label></td>
  88.     </tr>
  89.     <tr>
  90.       <td colspan="3">&nbsp;</td>
  91.     </tr>
  92.     <tr>
  93.       <td height="45" colspan="2"><label>
  94.         <div align="right">
  95.           <input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" />
  96.           </label>
  97.       </div></td>
  98.       <td><label>
  99.         <input type="button" name="button2" id="button2" value="Cancel" />
  100.       </label></td>
  101.     </tr>
  102.   </table>
  103.   <div align="center"></div>
  104. </form>
  105. </body>
  106. </html>
  107.  
  108.  
Dec 23 '10 #3
omerbutt
638 512MB
i could not see if you are verifying your mysql query
please replace your class with the following code
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class DBCon
  3. {
  4.     var $con;
  5.     function getCon()
  6.     {
  7.         $hostname = 'localhost';
  8.         $dbuser = 'root';
  9.         $dbpassword = '';
  10.  
  11.         $con=mysql_connect($hostname,$dbuser,$dbpassword);
  12.         if(!$con)
  13.         {
  14.             die('cannot connect db'.mysql_error());
  15.         }
  16.         mysql_select_db("studyco",$con);
  17.         return $con;
  18.     }
  19.  
  20.         //Queries
  21.     function executeQuery($query)
  22.     {
  23.         if($query!= null)
  24.         {
  25.             mysql_query($query) or die(mysql_error());
  26.             echo 'record added'.$query;
  27.         } 
  28.             else 
  29.             {
  30.                 echo 'not added';
  31.             }
  32.     }    
  33.     function destruct()
  34.     {
  35.         mysql_close();
  36.     }
  37. }
  38. ?>
  39.  
  40.  
  41.  
Dec 23 '10 #4
AutumnsDecay
170 100+
Try this.. replace this:

Expand|Select|Wrap|Line Numbers
  1. $query="INSERT INTO parent (firstname,lastname,gender,father_husbandname,profession,contactoffice,contacthome,mobile,email,address,snap,alt_parentid) 
  2.             VALUES ('".$firstName."', '".$lastName."', '".$gender."', '".$FHName."', '".$profession."', '".$contactoff."', '".$contacthome."', '".$mobile."', '".$address."', '".$snap."', '".$parenttype."')";
  3.             $stdbcon->executeQuery($query);
  4.  
with this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. $query="INSERT INTO `parent` (firstname,lastname,gender,father_husbandname,profession,contactoffice,contacthome,mobile,email,address,snap,alt_parentid) 
  3.             VALUES ('$firstName', '$lastName', '$gender', '$FHName', '$profession', '$contactoff', '$contacthome', '$mobile', '$address', '$snap', '$parenttype')";
  4.             $stdbcon->executeQuery($query);

Also, did you make sure to add a primary, auto-incrementing key to your table?
Dec 23 '10 #5
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
Dec 24 '10 #6
AutumnsDecay
170 100+
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.
Dec 24 '10 #7
omerbutt
638 512MB
Sure
glad to help out
regards,
Omer Aslam
Dec 26 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Sugapablo | last post by:
Let's say I create a new record in a table like this: mysql_query("INSERT INTO table (col1) VALUES ('example')",$conn); ....that had an auto-incrementing, unique identifying column named "ID"...
4
by: David | last post by:
Hi, I have the following code which adds a record to a table: vUser = request.form("user") '(UserID = PK in table) vPass = request.form("pass") vEmail = request.form("email") uSQL =...
4
by: YFS DBA | last post by:
How do I use VBA to insert a *new* record into a subform? I have a master form with client information, and a sub form with billing information. I want to click on a button ("Add Data") and have...
3
by: Mark | last post by:
I'm using ASP.Net to accress a database, what I need to do is get the fields out of the very last record in the db. How do I do this? Actually I'm after the primary key, titled 'AdID' it'll tell...
0
by: Vincy | last post by:
Once there is any addition of record to the table in the database( insertion of the record is not from the datagrid), the datagrid in the windows form should automatically refresh. How do i do...
5
by: Jan | last post by:
Hello, Just after the new record is inserted in the database using a Detailsview control, i would like to display a short message "the record is inserted". In the aspx file, i defined a...
20
by: dav3 | last post by:
Alright folks I am in need of a lil guidance/assistance here. I have a program which reads in a txt file. This txt file contains lines of the form January 3, 2007, 85.8 Now each line of the txt...
2
by: TimSki | last post by:
Hi, In my asp page I am inserting a record in to the sql server 2005 db as follows... OpenDataConnection() oConn.BeginTrans set cm = CreateObject("ADODB.Command") set cm.ActiveConnection =...
2
by: Jim in Arizona | last post by:
I forgot what the SQL command was to return the key of the last inserted record. Does anyone happen to know what it is? Thanks, Jim --
0
by: Van Fitz | last post by:
I am importing an excel worksheet into an existing access 2000 table using TransferSpreadsheet. Im using a timestamp field as well in this table. This is going well, however, once the data is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.