INSERT INTO not working

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
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

INSERT INTO not working

Post by robertb13 »

The connection shows "Heck Yes"
The INSERT INTO isn't working

Not sure what I'm missing...

Code: Select all

<?php
include ('config/connection.php'); //Add configuration file

if (!$dbc)
  {
  die('Could not connect: ' . mysql_error());
  }
  else {
	  echo "Heck Yes";
  }
  
mysql_select_db("survey_answers", $dbc);

$sql="INSERT INTO survey_answers (survey_id,user_id,answer_body) VALUES (3,1,'ID03 ANS01')";

if (!mysql_query($sql,$dbc))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($dbc);

?>
Last edited by robertb13 on Tue Aug 28, 2012 1:54 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: INSERT INTO not working

Post by requinix »

I don't know either, but I bet I'd have a good idea if you showed us the error message you're getting.
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Re: INSERT INTO not working

Post by robertb13 »

All it says is...
Heck Yes Error
User avatar
mixa2000
Forum Newbie
Posts: 18
Joined: Fri Jun 22, 2012 4:50 pm

Re: INSERT INTO not working

Post by mixa2000 »

There could be the problem with the variables or with something else in the connection.php ; "include ('config/connection.php'); //Add configuration file."
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Re: INSERT INTO not working

Post by robertb13 »

The variable for $dbc is

Code: Select all

<?php

/* ################################ */
/* ##### Connection Document ###### */
/* ################################ */



DEFINE ('DB_USER'
DEFINE ('DB_PASSWORD
DEFINE ('DB_HOST'
DEFINE ('DB_NAME'

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if (!$dbc) {
	die ('Could not connect to the database');
	}
	echo 'Alright!';

?>
This returns Alright! so it is connecting.
It also returns Heck Yes 03 in this code

Code: Select all

<?php
include ('config/config.php'); //Add configuration file

if (!$dbc)
  {
  die('Could not connect: ' . mysqli_error($dbc));
  }
  else {
	  echo "Heck Yes 03";
  }
  
mysqli_select_db("survey_answers", $dbc);

$sql="INSERT INTO survey_answers (survey_id,user_id,answer_body) VALUES (3,1,'ID03 ANS01')";

if (!mysqli_query($sql,$dbc))
  {
  die('Error: ' . mysqli_error());
  }
echo "1 record added";

mysqli_close($dbc);

?>
I ran -- INSERT INTO survey_answers (survey_id,user_id,answer_body) VALUES (3,1,'ID03 ANS01')
in myphpadmin and it added the row correctly

I am not sure what's happening :banghead:
Last edited by robertb13 on Mon Aug 27, 2012 11:30 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: INSERT INTO not working

Post by requinix »

Duuude, don't post your database information on a public forum! Edit it out of your post!
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Re: INSERT INTO not working

Post by robertb13 »

Thanks. Didn't even think of that. I'm a newby for real :wink:
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Re: INSERT INTO not working

Post by robertb13 »

I'm pulling my hair out over here.

If I put ...
INSERT INTO survey_answers (survey_id,user_id,answer_body) VALUES (3,1,567);
...into SQL of myPHPadmin, the database entry is created.

When I run this page...

Code: Select all

<?php
include ('config/config.php'); //Add configuration file

if (!$dbc) {
	die ('Could not connect to the database');
	}
	echo '....Yes it is!';

!mysql_query("INSERT INTO survey_answers (survey_id,user_id,answer_body) VALUES (3,1,567)");

echo "....1 record add";

mysql_close ($dbc);

?>
It echos
....Yes it is....1 record add

But the data is NOT in the database. WTF?
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Re: INSERT INTO not working

Post by robertb13 »

I gave up on this one.
Did it a different way.
Post Reply