PHP Insert command error

Hello Everyone,

I have an insert.php that could not insert data from php form into my mysql database.

Can anyone point out my mis-takes out or what i should correct from my insert command. The code is here below:



// Connect to server and select database.
mysql_connect(“$host”, “$username”, “$password”)or die(“cannot connect”);
mysql_select_db(“$db_name”)or die(“cannot select DB”);

// Get values from form
$option1=$_POST[‘I like orange’];
$option2=$_POST[‘I like apple’];
$option3=$_POST[‘I like banana’];

$sd=$_POST[‘strongly disagree’];
$d=$_POST[‘disagree’];
$nad=$_POST['$neither agree or disagree '];
$a=$_POST[‘agree’];
$sa=$_POST[‘strongly agree’];
$evd=$_POST[‘never’];

$sql=“insert into $Questions (Question_NO, Question) values (‘$I like orange’, ‘$I like apple’, ‘$Ilike banana’)”;

$sql2=“insert into $AnswerKeys(AnswerKey_ID, AnswerKey) values (‘$strongly disagree’, ‘$disagree’, ‘$neither agree or disagree’, ‘$agree’, ‘$strongly agree’, ‘$never’)”;

$result=mysql_query($sql);
$result=mysql_query($sql2);

// if successfully insert data into database, displays message “Successful”.
if($result){
echo “Successful”;
echo “<BR>”;
echo “<a href=‘index.php’>Back to main page</a>”;
}

else {
echo “ERROR”;
}

// close connection
mysql_close();
?>
[ /]

$sql2=“insert into $AnswerKeys(AnswerKey_ID, AnswerKey) values (‘$strongly disagree’, ‘$disagree’, ‘$neither agree or disagree’, ‘$agree’, ‘$strongly agree’, ‘$never’)”;

The column count doesn’t match :slight_smile: Right after your table statement, you define TWO columns that you’re inserting data into, then in the VALUES() bit you insert SIX columns :o

hi,my friend.SQL insert parse,one column should insert one value.if you once insert into more values,you can code below.


$sql2="insert into $AnswerKeys(AnswerKey_ID, AnswerKey) values ('$strongly disagree', '$disagree'), ('$neither agree or disagree', '$agree'),( '$strongly agree', '$never')"; 

If you get stuck with a query not working you can use the mysql_error function to output the mysql server’s response to the query. In this case it would point clearly to the column count issue.

Thanks. But after making the corrections above, the php still gives error on the page.
What else is wrong with the code above?

If you do:

error_reporting(E_ALL);

then you will see the problem.

Also, you don’t need the double-quotes around variables here:


mysql_connect("$host", "$username", "$password")or die("cannot  connect");
mysql_select_db("$db_name")or die("cannot select DB");

Maybe my .sql is wrong. This is the sample .sql below:

[ /]
DROP TABLE IF EXISTS Questions;

CREATE TABLE Questions (
Question_NO int(11) NOT NULL AUTO_INCREMENT,
Question varchar(1000) NOT NULL,
PRIMARY KEY (Question_NO)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

/*Data for the table Questions */

insert into Questions(Question_NO,Question) values (null,‘I like orange’),(null,‘I like apple’),(null,‘I Banana’)";
/*Table structure for table AnswerKeys */

DROP TABLE IF EXISTS AnswerKeys;

CREATE TABLE AnswerKeys (
AnswerKey_ID int(11) NOT NULL AUTO_INCREMENT,
AnswerKey varchar(50) NOT NULL,
PRIMARY KEY (AnswerKey_ID)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

/*Data for the table AnswerKeys */

insert into AnswerKeys(AnswerKey_ID,AnswerKey) values ((‘$strongly disagree’, ‘$disagree’, ‘$neither agree or disagree’, ‘$agree’, ‘$strongly agree’, ‘$never’)";

/*Table structure for table Answers */

DROP TABLE IF EXISTS Answers;

CREATE TABLE Answers (
Answer_ID int(11) NOT NULL AUTO_INCREMENT,
Question_NO int(11) NOT NULL,
AnswerKey_ID int(11) NOT NULL,
UserID varchar(50) NOT NULL,
PRIMARY KEY (Answer_ID)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

[ /]

don’t mean to be patronising or mean but if you don’t know why this is wrong you are totally out your depth:

insert into AnswerKeys(AnswerKey_ID,AnswerKey) values ((‘$strongly disagree’, ‘$disagree’, ‘$neither agree or disagree’, ‘$agree’, ‘$strongly agree’, ‘$never’)";

And I don’t mean to be critical of your reply

But could you please elaborate as to what the problems are with it (or give a resource link) so that those that are just learning can benefit? Some of us wade in and some jump in over our head, but most can learn to “swim”. :wink: