Page 1 of 1

Unable to Process Query

Posted: Fri May 18, 2007 3:07 pm
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

Posted: Fri May 18, 2007 3:10 pm
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);; 
}

Posted: Fri May 18, 2007 3:16 pm
by TaGmAn
Column count doesn't match value count at row 1
query was: INSERT INTO main (col1, col2, col3) VALUES ('', '', '')

Posted: Fri May 18, 2007 6:18 pm
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.

Posted: Sat May 19, 2007 6:39 am
by feyd
for($i = 1...
for($i = 0...

Something is fishy.