Hello Sir,
I have an one problme when inserting data.
I want to insert data through function cuz of reuse function. - function add_rec($tbl,$flds,$val)
-
{
-
$sql="insert into $tbl ($flds) values($val)";
-
$sql_result=mysql_query($sql) or die(mysql_error());
-
return($sql_result);
-
}
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_record
CompanyInfromation from one table to another database table eg: tbl_company
20 3396
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($sql,$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.
As dlite922 said, you should use a mySQL link for this to work, I'd do something like: -
$dbLink = mysql_connect('YOUR_HOST','YOUR_USER','YOUR_PASS');
-
mysql_select_db('YOUR_DATABASE', $dbLink);
-
-
function addData($TableName,$Fields,$Values)
-
{
-
global $dbLink;
-
$query = "
-
INSERT INTO {$TableName} (".(is_array($Fields)?implode(",",$Fields):$Fields).")
-
VALUES (".(is_array($Values)?implode(",",$Values):$Values).");
-
";
-
return ($result = mysql_query($query, $dbLink)) ? $result : false;
-
}
-
- $link=mysql_connect("localhost","root","") or die("Connection Failed");
-
mysql_select_db("query",$link) or die("Failed Connecting To Database");
-
$tblname='tbl1';
-
$field=array('name','address');
-
$data=array($name,$address);
-
echo $success=InsertData($tblname,$field,$data);
-
function InsertData($TblName,$Fields,$Values)
-
{
-
global $link;
-
$query="INSERT INTO $TblName(".(is_array($Fields)?implode(",",$Fields):$Fields).") VALUES(".(is_array($Values)?implode(",",$Values).")";
-
return ($result=mysql_query($query,$link))?$result:false;
-
}
I Use this one function but can not work. plz where is my problem ..
I used this above method like: - $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.
- $field=array('name','address');
-
echo $new=is_array($field)?implode(",",$field : $field);
In the above code this error is occur. - Parse error: syntax error, unexpected ':' in D:\xampp\htdocs\my\test\query\check.php on line 2
.
Plz where is my problem..
",",$field : $field is not a correct call for implode(). you probaby meant - $new=is_array($field)?implode(",",$field) : $field;
I can't understand what have suggest...plz can u clear me plz
- <?php
-
foreach($_POST as $key=>$value)
-
{
-
$key.":-".$$key=$value;
-
}
-
$link=mysql_connect("localhost","root","") or die("Failed Connecting to Database");
-
mysql_select_db("query",$link) or die("Failed Connecting To Database");
-
-
$tblname='tbl1';
-
$field=array('name','address');
-
$value=array($name,$address);
-
-
echo $success=InsertData($tblname,$field,$data);
-
-
function InsertData($TblName,$Fields,$Values)
-
{
-
global $link;
-
$query="INSERT INTO $TblName(".(is_array($Fields)?implode(",",$Fields):$Fields).")
-
VALUES(".(is_array($Values)? "'".implode("','",$Values).")";
-
-
return ($result=mysql_query($query,$link))?$result:false;
-
}
-
-
?>
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
@luckysanj
First off, 'plz' is spelled 'please'. Adding extra 'z's to the end of this word only makes you look less intelligent. Secondly, please do not beg for code (see point 3). Thirdly, we, as a community, do not just hand out code. After reading your posts, you show little intuition, and I would not be surprised if you got 0 responses from now on.
I apologise for the rant - been a long (but beautiful (weather-wise)) day.
Now, if you'd like to explain what errors you're getting (what is being reported), in clear, concise English, I'm sure we can solve this problem.
Mark (apologies again if I sounded rude before).
ok, I want to Select, Insert, Update & Delete data through one function by passing values through array. which function is constant. & I am inserting data through function variable like $TblName,$Fields,$Values.
The above code line number 18 have one problem. I didn't correct this problem yet. & How can i correct the error.
@luckysanj
First with the error, here's the fix I can see: -
function InsertData($TblName,$Fields,$Values)
-
{
-
global $link;
-
$query="INSERT INTO `{$TblName}` (`".(is_array($Fields)?implode("`,`",$Fields):$Fields)."`)
-
VALUES (".(is_array($Values)? "'".implode("','",$Values)."')";
-
-
return ($result=mysql_query($query,$link))?$result:false;
-
}
Now, about mySQL function, there are tons of classes and additions to the regular, built-in mysql functions in PHP, which are probably the best way to work with mysql.
Here's an Example.
Still I have got error .
This is my index.php page: -
<!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>Multiple Data entry with one query</title>
-
</head>
-
<body>
-
-
<form id="form1" name="form1" method="post" action="check.php">
-
<table width="200" border="1">
-
<tr>
-
<td colspan="2">Pesonal Info Table 1 </td>
-
</tr>
-
<tr>
-
<td width="72">Name:</td>
-
<td width="112"><input name="name" type="text" id="name" value="Ashok" /></td>
-
</tr>
-
<tr>
-
<td>Address</td>
-
<td><input name="address" type="text" id="address" value="Kathmandu" /></td>
-
</tr>
-
<tr>
-
<td><input name="submit1" type="submit" id="submit1" value="Submit" /></td>
-
<td><input type="reset" name="Submit2" value="Reset" /></td>
-
</tr>
-
</table>
-
</form>
-
</br>
-
</br>
-
</body>
-
</html>
-
This is my another page: check.php -
<?php
-
foreach($_POST as $key=>$value)
-
{
-
$key.":-".$$key=$value;
-
}
-
$link=mysql_connect("localhost","root","") or die("Failed Connecting to Database");
-
mysql_select_db("query",$link) or die("Failed Connecting To Database");
-
-
$tblname='tbl1';
-
$field=array('name','address');
-
$value=array($name,$address);
-
-
echo $result=InsertData($tblname,$field,$value);
-
-
function InsertData($TblName,$Fields,$Values)
-
{
-
global $link;
-
$query="INSERT INTO `{$TblName}` (`".(is_array($Fields)?implode("`,`",$Fields):$Fields)."`)
-
VALUES (".(is_array($Values)? "'".implode("','",$Values)."')";
-
-
return ($result=mysql_query($query,$link))?$result:false;
-
}
-
?>
-
-
-
Error is :Parse error: syntax error, unexpected ';' in D:\xampp\htdocs\my\test\query\check.php on line 20
Try this -
$query="INSERT INTO `{$TblName}` (`".(is_array($Fields)?implode("`,`",$Fields):$Fields)."`)
-
VALUES (".(is_array($Values)? "'".implode("','",$Values))."')";
-
Now this error occur on the above php code: -
Parse error: syntax error, unexpected ')' in D:\xampp\htdocs\my\test\query\check.php on line 21
-
On - $query="INSERT INTO `{$TblName}` (`".(is_array($Fields)?implode("`,`",$Fields):$Fields)."`)
-
VALUES (".(is_array($Values)? "'".implode("','",$Values))."')";
Sorry, my bad -
$query="INSERT INTO `{$TblName}` (`".(is_array($Fields)?implode("`,`",$Fields):$Fields)."`) VALUES (".(is_array($Values)?"'".implode("','",$Values)."'":$Values).")";
I think now it should work ;)
Ya Sir, It works. Thank you very much for ur cooperation.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |