Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
| Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 29th, 2004, 09:44 AM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default dynamitic sql statement

I am using the following code to build a dynamitic sql statement based on all the fields in a table. From here I need to pass in matching values from a form which is also dynamiticly created containing all fields in the table. I can get the name(field1...fieldx) but am not sure how to pass in the field values to value($var1...$varx)

<?php
echo "name(";
$result=mysql_list_fields("msds","msdsmasterlist") ;
for($i=1; $i<mysql_num_fields($result)-1; $i++){
echo mysql_field_name($result,$i);
echo ",";
}
//last field
$result=mysql_list_fields("msds","msdsmasterlist") ;
$lastfield=mysql_num_fields($result)-1;
echo mysql_field_name($result,$lastfield);
//end section
echo ")";
?>
<?php
//echo "<BR><BR><BR>$ADACPartNum,$RevisionDate,$ProductNa me,$ProductDescription<BR><BR><BR>";
echo "value($";
$result=mysql_list_fields("msds","msdsmasterlist") ;
for($i=1; $i<mysql_num_fields($result)-1; $i++){
echo mysql_field_name($result,$i);
echo ",$";
}
//last field
$result=mysql_list_fields("msds","msdsmasterlist") ;
$lastfield=mysql_num_fields($result)-1;
echo mysql_field_name($result,$lastfield);
//end section
echo ")";
?>

  #2 (permalink)  
Old April 29th, 2004, 02:04 PM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

found a solution

<?php
include ("header.php");
?>
<html>
<body>
<?php
$name='';
$value='';
if($HTTP_POST_VARS){
    while(list($lvar,$lvalue)=each($HTTP_POST_VARS)){
        if ((ereg("[^1]$",$lvar)) && (ereg("[^\d\w]$",$lvalue))){ //if var does not end in a 1 and

                                        //value not null
        //echo "$lvar=$lvalue<br>"; //echo the var and its value
        //echo "$lvar ";
        //$statement=$lvar;
        //echo "$statement<br>";
        $name=$name . $lvar . ","; //built var string
        $value=$value . $lvalue . "','";
        }
        //echo "$lvalue ";
    }
}
$length=strlen($name); //get length of string
$name=Substr($name,0,($length-1));
$name="($name)"; //strip out last comma
echo "$name<br>";
$length2=strlen($value);
$value=Substr($value,0,($length2-2));
$value="('$value)";
echo "$value<br>";
$sql="INSERT into msdsmasterlist $name VALUES $value ";
$result=mysql_query($sql);
echo $sql;
$num_rows=mysql_affected_rows();
echo $num_rows;
$errors=mysql_error();
echo $errors;













Similar Threads
Thread Thread Starter Forum Replies Last Post
convert a SQL Statement from MS Access to a SQL Corey Access 6 March 28th, 2007 12:33 PM
SQL Statement??? carswelljr SQL Server 2000 2 August 25th, 2006 12:40 PM
SQL Statement help johnman Access 1 October 22nd, 2004 10:20 AM
sql statement sand133 VB Databases Basics 2 September 29th, 2004 09:48 AM
Build a dynamitic sql statement skicrud Pro PHP 0 April 29th, 2004 09:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.