Unable to Process Query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
TaGmAn
Forum Newbie
Posts: 2
Joined: Fri May 18, 2007 3:00 pm
Location: The Seven Seas Of Rhye
Contact:

Unable to Process Query

Post by TaGmAn »

I have created a database. In that database is a table called 'main'. In table main are 4 columns, ['index', 'col1', 'col2', 'col3']. I would like to grab those column names (['index', 'col1', 'col2', 'col3']) and use them to scan my form for elements with those given names. Like say I have a textfield named 'col1',I would want that data to be taken and put into the DB. As long as they remain the same, I am hoping this will work. Here is the code I have so far:

Code: Select all

<?PHP
     $table = "main";
//get column headers
//http://www.phpfreaks.com/quickcode/Get-field-list-from-a-MySQL-table/181.php
     function GetFieldList($DB, $Table) {
              $fldlist = mysql_list_fields($DB, $Table);
              $columns = mysql_num_fields($fldlist);
              for ($i = 0; $i < $columns; $i++) {
	          $Listing[] = mysql_field_name($fldlist, $i);
              }
              Return ($Listing);
     }
//connects to database
     include 'mysqlconnect.php';  //also contains '$database' variable
     $result = mysql_query("SELECT * FROM " . $table, $connect) or die( mysql_error() );
     $num_rows = mysql_num_rows($result);
     $column_headers = GetFieldList($database, $table);
     $col_count = count($column_headers);
     for ($i = 1; $i < $col_count; $i++){
         $index = $column_headers[$i];
         $value[$i - 1] =  isset($_POST[$index])  ? $_POST[$index]   : "";
     }
     $headers = "";
     for ($i = 1; $i < $col_count; $i++) {
         $headers .= ($column_headers[$i] . ", ");
     }   $headers = substr($headers, 0, strlen($headers) - 2);
     $values = "";
     print $headers . "<BR>";
     for ($i = 0; $i < $col_count; $i++) {
         $values .= ("'" . $value[$i] . "', ");
     }   $values = substr($values, 0, strlen($values) - 2);
     print $values . "<BR>";
     $query = mysql_query("INSERT INTO main ($headers) VALUES ($values)");
     if  ($query) print "SUCCESS!";
     else print "Unable to process request.";
?>
When I run the script, it just says "Unable to process request."
And I will add anti-SQL injection features later, once this is fixed.
I am using PHP Version 4.4.1
Is my concept all wrong? I just wanted an easy way to grab form data once my database was setup, but I'm thinking it might not work ='(

Thanks =D
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

$query = "INSERT INTO main ($headers) VALUES ($values)";
$result = mysql_query($query);
if  ($result) {
	print "SUCCESS!";
}
else {
	print "Unable to process request. <br />\n" . mysql_error() . "<br />\nquery was: " . htmlentities($query);; 
}
User avatar
TaGmAn
Forum Newbie
Posts: 2
Joined: Fri May 18, 2007 3:00 pm
Location: The Seven Seas Of Rhye
Contact:

Post by TaGmAn »

Column count doesn't match value count at row 1
query was: INSERT INTO main (col1, col2, col3) VALUES ('', '', '')
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Are any of those colX fields autoincrement? Also, based on that query, your data is not being assigned as you think it is. Unless you intend to have complete blank data sets.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

for($i = 1...
for($i = 0...

Something is fishy.
Post Reply