473,574 Members | 2,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can i reuse function

luckysanj
69 New Member
Hello Sir,

I have an one problme when inserting data.

I want to insert data through function cuz of reuse function.

Expand|Select|Wrap|Line Numbers
  1. function add_rec($tbl,$flds,$val)
  2.     {
  3.     $sql="insert into $tbl ($flds) values($val)";
  4.     $sql_result=mysql_query($sql) or die(mysql_error());
  5.     return($sql_result); 
  6.     }
I have create this function on one file.

& i want to use this function when we insert data from multiple tables or different fields.

So can u guide me How can i reuse this function when we insert multiple data from multiple fields.

Like: Employee Records from one table to one database table.tbl_emp_r ecord
CompanyInfromat ion from one table to another database table eg: tbl_company
Jul 23 '09 #1
20 3418
dlite922
1,584 Recognized Expert Top Contributor
A function cannot magically guess your SQL format, you have to pass it all the values, like you have. Except in your example you don't passin the MySQL link (a database resource created by calling mysql_connect() )

pass that in like mysql_query($sq l,$dbLink) and you'll be good to go.

You should look into a DA (Data Accessor) class and design your app with OOP. But that's after you have a good understanding of PHP.



Dan

PS -- You $fld and $val, I assume will be arrays, need to be implode()ed with a comma and quotes like this: $fieldStr = "'" . implode("','",$ fld) . "'"; THEN insert it into the query. same with $val.
Jul 23 '09 #2
Canabeez
126 New Member
As dlite922 said, you should use a mySQL link for this to work, I'd do something like:

Expand|Select|Wrap|Line Numbers
  1. $dbLink = mysql_connect('YOUR_HOST','YOUR_USER','YOUR_PASS');
  2. mysql_select_db('YOUR_DATABASE', $dbLink);
  3.  
  4. function addData($TableName,$Fields,$Values)
  5. {
  6.     global $dbLink;
  7.     $query = "
  8.         INSERT INTO {$TableName} (".(is_array($Fields)?implode(",",$Fields):$Fields).") 
  9.                           VALUES (".(is_array($Values)?implode(",",$Values):$Values).");
  10.     ";
  11.     return ($result = mysql_query($query, $dbLink)) ? $result : false;
  12. }
  13.  
Jul 23 '09 #3
luckysanj
69 New Member
Expand|Select|Wrap|Line Numbers
  1. $link=mysql_connect("localhost","root","") or die("Connection Failed");
  2. mysql_select_db("query",$link) or die("Failed Connecting To Database");
  3. $tblname='tbl1';
  4. $field=array('name','address');    
  5. $data=array($name,$address);
  6. echo $success=InsertData($tblname,$field,$data);
  7. function InsertData($TblName,$Fields,$Values)
  8. {
  9. global $link;
  10.  $query="INSERT INTO $TblName(".(is_array($Fields)?implode(",",$Fields):$Fields).") VALUES(".(is_array($Values)?implode(",",$Values).")";
  11. return ($result=mysql_query($query,$link))?$result:false;
  12. }
I Use this one function but can not work. plz where is my problem ..
Jul 24 '09 #4
luckysanj
69 New Member
I used this above method like:

Expand|Select|Wrap|Line Numbers
  1. $fieldStr = "'" . implode("','",$fld) . "'"; 
And It works very nicely & i feel happy. But have done this above second method too. but it doesn't work means it gives error on line number 10 like this ";" error.

I want to know or solve through Canabeez Newbie method for my good programming. So plz as possible give us error free codes.

Thank You.
Jul 24 '09 #5
luckysanj
69 New Member
Expand|Select|Wrap|Line Numbers
  1. $field=array('name','address');    
  2. echo $new=is_array($field)?implode(",",$field : $field);
In the above code this error is occur.

Expand|Select|Wrap|Line Numbers
  1. Parse error: syntax error, unexpected ':' in D:\xampp\htdocs\my\test\query\check.php on line 2
.

Plz where is my problem..
Jul 24 '09 #6
Dormilich
8,658 Recognized Expert Moderator Expert
",",$field : $field is not a correct call for implode(). you probaby meant
Expand|Select|Wrap|Line Numbers
  1. $new=is_array($field)?implode(",",$field) : $field;
Jul 25 '09 #7
luckysanj
69 New Member
I can't understand what have suggest...plz can u clear me plz
Jul 25 '09 #8
Dormilich
8,658 Recognized Expert Moderator Expert
you're simply mixing up the ternary conditional operator and the implode() function call.

essentially, it all is described in the error message (colons have a special meaning in PHP (unless they are part of a string))
Jul 25 '09 #9
luckysanj
69 New Member
Expand|Select|Wrap|Line Numbers
  1.     <?php
  2.     foreach($_POST as $key=>$value)
  3.         { 
  4.         $key.":-".$$key=$value;
  5.         }
  6.     $link=mysql_connect("localhost","root","") or die("Failed Connecting to Database");
  7.     mysql_select_db("query",$link) or die("Failed Connecting To Database");    
  8.  
  9.     $tblname='tbl1';
  10.     $field=array('name','address');    
  11.     $value=array($name,$address);  
  12.  
  13.     echo $success=InsertData($tblname,$field,$data); 
  14.  
  15.     function InsertData($TblName,$Fields,$Values) 
  16.         { 
  17.         global $link; 
  18.         $query="INSERT INTO $TblName(".(is_array($Fields)?implode(",",$Fields):$Fields).")
  19.                 VALUES(".(is_array($Values)? "'".implode("','",$Values).")";
  20.  
  21.                 return ($result=mysql_query($query,$link))?$result:false; 
  22.         } 
  23.  
  24.     ?>
Plz i m frustration from this problem. so plz as you don't mind plz rectify my error & give me errror free code. plz plz
Jul 25 '09 #10

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

Similar topics

3
6884
by: DPfan | last post by:
What's exactly the meaning of "code reuse" in C++? Why such kind of reuse have more advantages over the counterpart in other language like in C? How is "code reuse" realized in C++? By composition mainly? What're others? Thanks in advance for your comments!
0
4059
by: integragreg | last post by:
I apologize in advance if I am posting to the wrong group, but at least one of my questions is related to Platform Invoke in C#. I am using .NET Framework 1.1, and for improved performance, I need to be able to reuse a client socket by placing it into a pool. Unfortunately, with .NET Framework 1.1 I have no way to close a socket's...
3
1827
by: Simon | last post by:
Hi all, I'm hoping that some of you clever chaps could offer me some advice on code reuse. You see, whenever I make applications, I typically only find very limited
5
1589
by: apandapion | last post by:
I'm working with csharp and .net for the first time, and I've had a fair amount of luck. I started with the MSDN "Walkthrough : Creating a Distributed Application" tutorial and expanded from there, adding state management and other assorted things. So I have a lot of code that looks like this: private void DataGrid1_UpdateCommand(object...
16
2311
by: sailor.gu | last post by:
Hi all guys, As an embeded programmer with five year C experience, I did read many great books related with design, coding, test,debug,algorithms, compiler, design, os, pm and others. I always dream to achieve a refined and reusable design and implementation. I am very interested with code reuse.
8
2302
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR types. The problem: I have a number of mixed-mode functions which I want reuse. These functions revolve around converting a CLR String to a C++...
19
2365
by: jacob navia | last post by:
There is an interesting discussion running in Slashdot now, about code reuse. The thema of the discussion is here: < quote > Susan Elliot Sim asks: "In the science fiction novel, 'A Deepness in the Sky,' Vernor Vinge described a future where software is created by 'programmer archaeologists' who search archives for existing pieces of...
8
2192
by: WebSnozz | last post by:
I have an application written in C that does a lot of low level stuff. It does a lot of things like casting from void*'s. I want to create a new GUI for it in either C# or MC++, but reuse the existing code. The options I've considered so far: 1. Create a new MC++ GUI project and add the *.c files to them and mark them with pragma...
13
3944
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help is much appreciated. JD
0
7726
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8053
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7817
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5301
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3741
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3752
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1060
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.