PHP loops to mysql
Posted: Thu Mar 27, 2008 4:31 pm
Ok,
this is what I have put together and it is fetching all the fields, could someone point me in the direction on how to write the information to the database:
I know this is wrong, but maybe you guys will understand a bit more what I am trying to do.
<?php
include '/var/www/web7/web/network/db/mysql.php';
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_db_query ("tenant", "select * from extension")
or die ("Query failed");
# get column metadata
$i = 0;
while ($i < mysql_num_fields ($result)) {
$meta = mysql_fetch_field ($result);
if (!$meta) {
echo "No information available<BR>\n";
}
$test=$_POST[$meta->name];
echo " $test " ;
$rows=$meta->name;
$i++;
}
mysql_free_result ($result);
mysql_select_db($database);
$query = "INSERT INTO tenant ($rows) VALUES ($test)";
echo $test ;
echo "<BR>";
echo $rows ;
mysql_query($query) or die('Error, insert query failed');
?>
This code outputs all the information from the form but getting this error Error, insert query failed so its not building the mysql query correctly.
The form I have got reads the rows from the database and allows you to fill it in, this code needs to be able to add that information to the database, but the idea is that the user will be able to add more columns and the script that writes the information to the database will be updated and write all the fields. This is so I don't have to hard code the fields in.
Many Thanks
this is what I have put together and it is fetching all the fields, could someone point me in the direction on how to write the information to the database:
I know this is wrong, but maybe you guys will understand a bit more what I am trying to do.
<?php
include '/var/www/web7/web/network/db/mysql.php';
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_db_query ("tenant", "select * from extension")
or die ("Query failed");
# get column metadata
$i = 0;
while ($i < mysql_num_fields ($result)) {
$meta = mysql_fetch_field ($result);
if (!$meta) {
echo "No information available<BR>\n";
}
$test=$_POST[$meta->name];
echo " $test " ;
$rows=$meta->name;
$i++;
}
mysql_free_result ($result);
mysql_select_db($database);
$query = "INSERT INTO tenant ($rows) VALUES ($test)";
echo $test ;
echo "<BR>";
echo $rows ;
mysql_query($query) or die('Error, insert query failed');
?>
This code outputs all the information from the form but getting this error Error, insert query failed so its not building the mysql query correctly.
The form I have got reads the rows from the database and allows you to fill it in, this code needs to be able to add that information to the database, but the idea is that the user will be able to add more columns and the script that writes the information to the database will be updated and write all the fields. This is so I don't have to hard code the fields in.
Many Thanks