Not inputting data my to mySQL from database

checked this 100 times.

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 2708 Views - Last Post: 11 August 2010 - 09:14 PM Rate Topic: -----

#1 TechSupport  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 09-May 09

Not inputting data my to mySQL from database

Posted 11 August 2010 - 09:08 AM

Before we start, ive been using PHP for about 3 days, and im now trying to create a social networking platform. Ive done this time and time again, but for some reason its not working this time.HELP!?!

Hey, im sure sure why this isnt working, ive tried echoing out all the variables in sending through right before

if(isset ($_POST['submit'])) {
					
	$id = $_SESSION['id'];
	$friend = $_POST['friend'];
	$startDate = $_POST['startDate'];
	$startTime = $_POST['startTime'];
	$endDate = $_POST['endDate'];
	$endTime = $_POST['endTime'];
	$description = $_POST['description'];
					
					
	$add_hang = "INSERT INTO hangs (hangID, userID, friendID, startDate, startTime, endDate, endTime, description, rating, ratingAmount) VALUES ('', '.$id', '$friend', '$startDate', '$startTime', '$endDate', '$endTime', '$description'', '0', '0')";				



and all the variables were outputting the information needed, but it was followed with "Couldn't execute query."

Here is the all of the code
if(isset ($_POST['submit'])) {
					
	$id = $_SESSION['id'];
	$friend = $_POST['friend'];
	$startDate = $_POST['startDate'];
	$startTime = $_POST['startTime'];
	$endDate = $_POST['endDate'];
	$endTime = $_POST['endTime'];
	$description = $_POST['description'];
					
					
	$add_hang = "INSERT INTO hangs (hangID, userID, friendID, startDate, startTime, endDate, endTime, description, rating, ratingAmount) VALUES ('', '.$id', '$friend', '$startDate', '$startTime', '$endDate', '$endTime', '$description'', '0', '0')";
        $result2 = mysqli_query($cxn, $add_hang) or die("Couldn't execute query.");
	        while ($row = mysqli_fetch_assoc($result)) {
		echo "The Hang was added.";
	        }
        } else {
        THE FORM
        }



Thanks guys, im truly stumped!

Is This A Good Question/Topic? 0
  • +

Replies To: Not inputting data my to mySQL from database

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: Not inputting data my to mySQL from database

Posted 11 August 2010 - 09:14 AM

Might I suggest adding the following:

$result2 = mysqli_query($cxn, $add_hang) or die("Couldn't execute query $add_hang: " . mysql_error());


so that maybe you can get some more info on the error itself, as well as printing the query as it's being passed to MySQL?
Was This Post Helpful? 2
  • +
  • -

#3 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Not inputting data my to mySQL from database

Posted 11 August 2010 - 09:24 AM

Quote

$add_hang = "INSERT INTO hangs (hangID, userID, friendID, startDate, startTime, endDate, endTime, description, rating, ratingAmount) VALUES ('', '.$id', '$friend', '$startDate', '$startTime', '$endDate', '$endTime', '$description'', '0', '0')";


Assuming userID is a numeric type, such as INTEGER, that period you have before $id in the VALUES portion might give you some issues.
Was This Post Helpful? 1
  • +
  • -

#4 TechSupport  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 09-May 09

Re: Not inputting data my to mySQL from database

Posted 11 August 2010 - 07:09 PM

removed perion, same outcome, then i implemented JackOfAllTrades' idea....

and i got this,

Couldn't execute query INSERT INTO hangs (hangID, userID, friendID, startDate, startTime, endDate, endTime, description, rating, ratingAmount) VALUES ('NULL', '18', '25', '2010-7-3', '12:30', '2010-7-3', '4:00', 'sdfasdfasdf'', '0', '0'):

which still doesnt tell me much.

actaully now im getting this:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in D:\wamp\www\hangs\add.php on line 41

ROW 41 is while ($row = mysqli_fetch_assoc($result)) {

This post has been edited by TechSupport: 11 August 2010 - 07:09 PM

Was This Post Helpful? 0
  • +
  • -

#5 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Not inputting data my to mySQL from database

Posted 11 August 2010 - 07:12 PM

Why is hangID NULL? That'd be an issue, as I assume hangID is an INTEGER type.

The error on line 41 is a side effect of the query failing. Because the query failed, $result is boolean false, which is an invalid argument to use with mysql_fetch_assoc(). That's why die() should be used, to prevent displaying such an error.
Was This Post Helpful? 0
  • +
  • -

#6 TechSupport  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 09-May 09

Re: Not inputting data my to mySQL from database

Posted 11 August 2010 - 07:14 PM

View PostValek, on 11 August 2010 - 06:12 PM, said:

Why is hangID NULL? That'd be an issue, as I assume hangID is an INTEGER type.

The error on line 41 is a side effect of the query failing. Because the query failed, $result is boolean false, which is an invalid argument to use with mysql_fetch_assoc(). That's why die() should be used, to prevent displaying such an error.


I was seeing if it would do anyhting, sorry for pasting that, its nto what i am generally using.

And where exactly should I use die();?
Was This Post Helpful? 0
  • +
  • -

#7 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Not inputting data my to mySQL from database

Posted 11 August 2010 - 07:16 PM

View PostJackOfAllTrades, on 11 August 2010 - 12:14 PM, said:

Might I suggest adding the following:

$result2 = mysqli_query($cxn, $add_hang) or die("Couldn't execute query $add_hang: " . mysql_error());


That's where you'd add die. Also, having mysql_error() in there will tell you whether it's syntax, mismatched column/value counts, etc. That will seriously narrow down the possibilities and make the error easier to locate.
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6552
  • View blog
  • Posts: 30,682
  • Joined: 10-May 07

Re: Not inputting data my to mySQL from database

Posted 11 August 2010 - 07:16 PM

I would visually verify your SQL string...

    $add_hang = "INSERT INTO hangs (hangID, userID, friendID, startDate, startTime, endDate, endTime, description, rating, ratingAmount) VALUES ('', '.$id', '$friend', '$startDate', '$startTime', '$endDate', '$endTime', '$description'', '0', '0')";
    $result2 = mysqli_query($cxn, $add_hang);
    if(!$result2) die("The following SQL failed <br>".$add_hang;



None of your POST strings are being verified, so we can't see what you are inputting to the script, nor can we see the database values. It would be like me asking you which of my four tires is low on air pressure.
Was This Post Helpful? 0
  • +
  • -

#9 TechSupport  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 09-May 09

Re: Not inputting data my to mySQL from database

Posted 11 August 2010 - 07:23 PM

View Postno2pencil, on 11 August 2010 - 06:16 PM, said:

I would visually verify your SQL string...

    $add_hang = "INSERT INTO hangs (hangID, userID, friendID, startDate, startTime, endDate, endTime, description, rating, ratingAmount) VALUES ('', '.$id', '$friend', '$startDate', '$startTime', '$endDate', '$endTime', '$description'', '0', '0')";
    $result2 = mysqli_query($cxn, $add_hang);
    if(!$result2) die("The following SQL failed <br>".$add_hang;



None of your POST strings are being verified, so we can't see what you are inputting to the script, nor can we see the database values. It would be like me asking you which of my four tires is low on air pressure.


with this added, i got this again.

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in D:\wamp\www\hangs\add.php on line 40
Couldn't execute query

Here are lines 40-42:

while ($row = mysqli_fetch_assoc($result2) or die("Couldn't execute query")) {
	echo "The Hang was added.";
}

Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6552
  • View blog
  • Posts: 30,682
  • Joined: 10-May 07

Re: Not inputting data my to mySQL from database

Posted 11 August 2010 - 07:25 PM

Shouldn't while be it's own command & die it's own...
while ($row = mysqli_fetch_assoc($result2)) or die("Couldn't execute query") {
	echo "The Hang was added.";
}


Was This Post Helpful? 0
  • +
  • -

#11 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Not inputting data my to mySQL from database

Posted 11 August 2010 - 07:32 PM

You put the "or die()" part in the complete wrong place. You should be checking for query failure after the query runs, not while you're trying to get results from it. If it failed, you've got not result resource to get data from!
Was This Post Helpful? 0
  • +
  • -

#12 TechSupport  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 09-May 09

Re: Not inputting data my to mySQL from database

Posted 11 August 2010 - 07:32 PM

View Postno2pencil, on 11 August 2010 - 06:25 PM, said:

Shouldn't while be it's own command & die it's own...
while ($row = mysqli_fetch_assoc($result2)) or die("Couldn't execute query") {
	echo "The Hang was added.";
}



I had that before, with that, i get:

Parse error: parse error in D:\wamp\www\hangs\add.php on line 39

line 36-41 are listed below:
$add_hang = "INSERT INTO hangs (hangID, userID, friendID, startDate, startTime, endDate, endTime, description, rating, ratingAmount) VALUES ('', '$id', '$friend', '$startDate', '$startTime', '$endDate', '$endTime', '$description', '0', '0')";
						
$result2 = mysqli_query($cxn, $add_hang) or die("Couldn't execute query $add_hang: " . mysql_error());
while ($row = mysqli_fetch_assoc($result2)) or die("Couldn't execute query") {
echo "The Hang was added.";
}


This post has been edited by TechSupport: 11 August 2010 - 07:33 PM

Was This Post Helpful? 0
  • +
  • -

#13 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Not inputting data my to mySQL from database

Posted 11 August 2010 - 07:36 PM

Take the or die("Couldn't execute query") off your while loop, it's invalid syntax.
Was This Post Helpful? 0
  • +
  • -

#14 TechSupport  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 09-May 09

Re: Not inputting data my to mySQL from database

Posted 11 August 2010 - 07:37 PM

View PostValek, on 11 August 2010 - 06:36 PM, said:

Take the or die("Couldn't execute query") off your while loop, it's invalid syntax.


Hah, yeah i did, and I get this again.

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in D:\wamp\www\hangs\add.php on line 39
Was This Post Helpful? 0
  • +
  • -

#15 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6552
  • View blog
  • Posts: 30,682
  • Joined: 10-May 07

Re: Not inputting data my to mySQL from database

Posted 11 August 2010 - 07:38 PM

My bad... I should have seen that it shouldn't be there, like Valek suggested.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3