Link to home
Start Free TrialLog in
Avatar of mgtm3
mgtm3Flag for Israel

asked on

i have a problem in inserting table

o.k i am tring to to this code and i get this problem

Unknown column 'gfdg' in 'field list'      


but the problem that i did not try to insert in a field named gfdg so why is that happning?
$f = "INSERT INTO poll3 (op1,op2,op3,op4,op5,subject,gen) VALUES ($op1,$op2,$op3,$op4,$op5,$subject,$gen)";
mysqli_query($link,$f);
 
echo mysqli_error($link);

Open in new window

Avatar of glcummins
glcummins
Flag of United States of America image

Can you post more complete code than this? The error is not based on the query that you provided to us. Perhaps something else is changing the $f variable before you run the query?
Avatar of mgtm3

ASKER


<?php
 
include 'cpoll.php';
 
 
$subject = $_POST['subject'];
$op1 = $_POST['op1'];
$op2 = $_POST['op2'];
$op3 = $_POST['op3'];
$op4 = $_POST['op4'];
$op5 = $_POST['op5'];
$gen=$_POST['gen'];
 
 
$sql = 'CREATE TABLE `poll3` (
`id` int( 255 ) NOT NULL AUTO_INCREMENT,
  `subject` VARCHAR( 700 )NOT NULL,
        `op1` VARCHAR( 700 )NOT NULL,
        `op2` VARCHAR( 700 ) NOT NULL,
        `op3` VARCHAR( 700 ) NOT NULL,
        `op4` VARCHAR( 700 ) NOT NULL,
        `op5` VARCHAR( 700 ) NOT NULL,
		`date` VARCHAR( 700 ) NOT NULL,
		`user` VARCHAR( 700 ) NOT NULL,
		`gen` VARCHAR( 700 ) NOT NULL,
		
        PRIMARY  KEY ( `id` )
       )';
 
 
mysqli_query($link,$sql);
 
mysqli_close($link);
unset($link);
include 'cpoll.php';
 
$f = "INSERT INTO poll3 (op1,op2,op3,op4,op5,subject,gen) VALUES ($op1,$op2,$op3,$op4,$op5,$subject,$gen)";
mysqli_query($link,$f);
 
echo mysqli_error($link);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bernard Savonet
Bernard Savonet
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Note: you define date and user as not null, while your insert does not provide data for them...
Note 2: at line 33 you close the connection, which your line 38 assumes to be open. was it opened in the second copy of cpoll.php that you include at line 35?
I will second fibo's comment that you need single quotes around each of the pieces of data. In general, you will want to put backticks around each of the field names so that they do not interfere with MySQL reserved words.

$f = "INSERT INTO poll3 (`op1`,`op2`,`op3`,`op4`,`op5`,`subject`,`gen`) VALUES ('$op1','$op2','$op3','$op4','$op5','$subject','$gen')";

Also, on a note unrelated to your original problem, you will want to change your CREATE TABLE query to:

 $sql = 'CREATE TABLE IF NOT EXISTS `poll3` (

This will help you to avoid problems if the table has already been created.
Avatar of mgtm3

ASKER

problem fiexd thanks
Thx for the points.
And I also support glcummins' suggestions. "Defensive programming" does not take a lot more time when writing the programs... but saves lots of debugging time now and in some time from noiw, when your program will move to a different server!