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 3287
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?...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
by: tammygombez |
last post by:
Hey fellow JavaFX developers,
I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: CD Tom |
last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
| |