Jump to content

mysql not working!

jameshumphries47

I have spent all night trying to fix phpmyadmin and i finally have took me 4 hours D; so i made this code up to check that i can add stuff to the database. but nope. is this problem with mysql or the code i wrote, its identical format to other code

<?phpinclude 'sqllogin.php';          if($_SERVER["REQUEST_METHOD"] == "POST"){              $sql="INSERT INTO `shopping`(`Item`,`Shop`,`Priority`,`Status`,`Cost`,`Quant`)VALUES('".$_POST['Item']."','".$_POST['Shop']."','".$_POST['Priority']."','".$_POST['Status']."','".$_POST['Cost']."'.'".$_POST['Quant']."')";#inset he details from the field in to the table                        if (mysqli_query($con,$sql)){                                            # this checks the data has been added              echo "data added";                                                   # this displays "data added"            }else{              echo"could not add data";                                            # this displays "could not add data"            }          }?>

and i just keep getting, could not add data ? 

 

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
Share on other sites

Link to post
Share on other sites


<?phpinclude 'sqllogin.php';

if($_SERVER["REQUEST_METHOD"] == "POST"){

$sql="INSERT INTO `shopping`(`Item`,`Shop`,`Priority`,`Status`,`Cost`,`Quant`)VALUES('".$_POST['Item']."','".$_POST['Shop']."','".$_POST['Priority']."','".$_POST['Status']."','".$_POST['Cost']."'.'".$_POST['Quant']."')";

try{

var_dump($con); //See what's really going on with the connection;

if (mysqli_query($con,$sql)){

var_dump(mysqli_query($con,$sql)); //See whats going on if the query executes

}

}

catch(Exception $e)

{

//If we catch an exception then display it and find out what the problem is

echo "EXCEPTION CAUGHT: ".$e->getMessage();

}

?>

This will provide basic debugging to you so that you can try and gain a better understanding of what is happening! var_dump is your friend!!

P.S I would recommend switching to PHP's PDO to handle your database operations in future. You can thank me later! :)

// Gigabyte 990FXA-UD3 // AMD FX-8320 CPU @ 4.3 Ghz (7-21.5 Multiplier) 200.90mhz FSB CPU-Z Validated // Kraken X40 AIO - 2x140mm Push-Pull // 4GB Corsair Vengeance LP - 8GB Avexir Core Series Red 1760Mhz // Sapphire R9 Fury Nitro 1130mhz/4GB 1025mhz (Effective) GPU-Z Validation // Corsair SP2500 2.1 & Microlab Solo 9C Speakers // Corsair K90 Silver - Cherry MX Red & Blue LEDs // EVGA SuperNova 850w G2

Link to comment
Share on other sites

Link to post
Share on other sites

perhaps the comment after the sql query is missing a space before # ?

Link to comment
Share on other sites

Link to post
Share on other sites

<?phpinclude 'sqllogin.php';if($_SERVER["REQUEST_METHOD"] == "POST"){$sql="INSERT INTO `shopping`(`Item`,`Shop`,`Priority`,`Status`,`Cost`,`Quant`)VALUES('".$_POST['Item']."','".$_POST['Shop']."','".$_POST['Priority']."','".$_POST['Status']."','".$_POST['Cost']."'.'".$_POST['Quant']."')";try{var_dump($con); //See what's really going on with the connection;if (mysqli_query($con,$sql)){ var_dump(mysqli_query($con,$sql)); //See whats going on if the query executes}}catch(Exception $e){//If we catch an exception then display it and find out what the problem isecho "EXCEPTION CAUGHT: ".$e->getMessage();}?>This will provide basic debugging to you so that you can try and gain a better understanding of what is happening! var_dump is your friend!!P.S I would recommend switching to PHP's PDO to handle your database operations in future. You can thank me later! 

tried that and now i recive this 

object(mysqli)[1]

  public 'affected_rows' => null

  public 'client_info' => null

  public 'client_version' => null

  public 'connect_errno' => null

  public 'connect_error' => null

  public 'errno' => null

  public 'error' => null

  public 'error_list' => null

  public 'field_count' => null

  public 'host_info' => null

  public 'info' => null

  public 'insert_id' => null

  public 'server_info' => null

  public 'server_version' => null

  public 'stat' => null

  public 'sqlstate' => null

  public 'protocol_version' => null

  public 'thread_id' => null

  public 'warning_count' => null

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
Share on other sites

Link to post
Share on other sites

-snip-

good ol'

echo $sql="INSERT INTO `shopping`(`Item`,`Shop`,`Priority`,`Status`,`Cost`,`Quant`)VALUES('".$_POST['Item']."','".$_POST['Shop']."','".$_POST['Priority']."','".$_POST['Status']."','".$_POST['Cost']."'.'".$_POST['Quant']."')";
Link to comment
Share on other sites

Link to post
Share on other sites

 

good ol'

echo $sql="INSERT INTO `shopping`(`Item`,`Shop`,`Priority`,`Status`,`Cost`,`Quant`)VALUES('".$_POST['Item']."','".$_POST['Shop']."','".$_POST['Priority']."','".$_POST['Status']."','".$_POST['Cost']."'.'".$_POST['Quant']."')";

i dont understand i just get INSERT INTO `shopping`(`Item`,`Shop`,`Priority`,`Status`,`Cost`,`Quant`)VALUES('ghf','ggfh','56','54','654'.'564')could not add data

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
Share on other sites

Link to post
Share on other sites

i dont understand i just get INSERT INTO `shopping`(`Item`,`Shop`,`Priority`,`Status`,`Cost`,`Quant`)VALUES('ghf','ggfh','56','54','654'.'564')could not add data

well, take that query, go to phpmyadmin, and run it

 

or just echo mysql_error() instead of "could not add data"

 

edit: there is a dot instead of a comma between the last two values

Link to comment
Share on other sites

Link to post
Share on other sites

well, take that query, go to phpmyadmin, and run it

 

or just echo mysql_error() instead of "could not add data"

 

edit: there is a dot instead of a comma between the last two values

wow... thankyou!! i should really wear my glasses. i feel so stupid D: i swear this is my final question because ive nearly finished all my homework assignments! how to i stop getting an error, which then goes when a value has been entered, the code we ended up making doesnt seem to be doing the trick.

<?phpsession_start();    if (isset($_SESSION['curtoat']) == false){                                       	$_SESSION['curtoat'] = 0;	$_SESSION['curav'] = 0;            				}?><html><body><form method="post">Enter a number: <input type="text" name="curnum"><br>Enter c for continue, q for quit, f for find an average <input type="text" name="instruction"><br><input type="submit"></form></body></html><?phpecho $_POST['curnum'];if ($_POST['instruction'] == "c"){$_SESSION['curtoat'] = $_SESSION['curtoat'] + $_POST['curnum'];$_SESSION['curav'] = $_SESSION['curav']+1;} elseif ($_POST['instruction'] == "f") {	$result = $_SESSION['curtoat'] / $_SESSION['curav'];	echo"the average for the numbers you selected are",$result;}else {	echo"ENDED";}?>

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
Share on other sites

Link to post
Share on other sites

wow... thankyou!! i should really wear my glasses. i feel so stupid D: i swear this is my final question because ive nearly finished all my homework assignments! how to i stop getting an error, which then goes when a value has been entered, the code we ended up making doesnt seem to be doing the trick.

<?phpsession_start();    if (isset($_SESSION['curtoat']) == false){                                       	$_SESSION['curtoat'] = 0;	$_SESSION['curav'] = 0;            				}?><html><body><form method="post">Enter a number: <input type="text" name="curnum"><br>Enter c for continue, q for quit, f for find an average <input type="text" name="instruction"><br><input type="submit"></form></body></html><?phpecho $_POST['curnum'];if ($_POST['instruction'] == "c"){$_SESSION['curtoat'] = $_SESSION['curtoat'] + $_POST['curnum'];$_SESSION['curav'] = $_SESSION['curav']+1;} elseif ($_POST['instruction'] == "f") {	$result = $_SESSION['curtoat'] / $_SESSION['curav'];	echo"the average for the numbers you selected are",$result;}else {	echo"ENDED";}?>

so... what's the error?

oh well it says notice undefined index

that's because when you first open the page you use the HTTP GET method, so the $_POST superglobal is empty, and when you access it you get errors. be sure that the variables you access are defined, using the method you already learned

Link to comment
Share on other sites

Link to post
Share on other sites

 

so... what's the error?

 

that's because when you first open the page you use the HTTP GET method, so the $_POST superglobal is empty, and when you access it you get errors. be sure that the variables you access are defined, using the method you already learned

 

like the code i just commented? it doesnt work? its to do with that if statement but i don't know what?

yes undefined index

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
Share on other sites

Link to post
Share on other sites

 

so... what's the error?

 

that's because when you first open the page you use the HTTP GET method, so the $_POST superglobal is empty, and when you access it you get errors. be sure that the variables you access are defined, using the method you already learned

 

OMG i did some debugging and i fixed it myself! Thank you for ALL your help you have been so useful!

here this is how i did it 

session_start();    if (isset($_POST['curnum']) == False){                                       	$_SESSION['curtoat'] = "";	$_SESSION['curav'] = ""; 	$_POST['curnum'] = "";	$_POST['instruction'] = "";}

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
Share on other sites

Link to post
Share on other sites

-snip-

glad you solved it :)

 

here is the somewhat nicer solution, for reference

<?phpsession_start();    if (isset($_SESSION['curtoat']) == false){	$_SESSION['curtoat'] = 0;	$_SESSION['curav'] = 0;            				}?><html>	<body>		<form method="post">			Enter a number: <input type="text" name="curnum" /><br />			Enter c for continue, q for quit, f for find an average <input type="text" name="instruction" /><br />		<input type="submit" />		</form>	</body></html><?phpif(isset($_POST['instruction'])){ // did the user submit data from the form? if yes, let's work on it	if ($_POST['instruction'] == "c"){		$_SESSION['curtoat'] += $_POST['curnum'];		$_SESSION['curav']++;	} elseif ($_POST['instruction'] == "f") {		$result = $_SESSION['curtoat'] / $_SESSION['curav'];		echo "the average for the numbers you selected are ", $result;	}	else {		echo "ENDED";	}}?>

the only line of code that i added is the one with the comment, that's pretty much the usual way to deal with form data with bare php.

i tried to make the rest of the code more readable, that's your bonus for finishing your homeworks ^^ hopefully you will come to the joyful side of programming where we indent things

Link to comment
Share on other sites

Link to post
Share on other sites

glad you solved it :)

 

here is the somewhat nicer solution, for reference

<?phpsession_start();    if (isset($_SESSION['curtoat']) == false){	$_SESSION['curtoat'] = 0;	$_SESSION['curav'] = 0;            				}?><html>	<body>		<form method="post">			Enter a number: <input type="text" name="curnum" /><br />			Enter c for continue, q for quit, f for find an average <input type="text" name="instruction" /><br />		<input type="submit" />		</form>	</body></html><?phpif(isset($_POST['instruction'])){ // did the user submit data from the form? if yes, let's work on it	if ($_POST['instruction'] == "c"){		$_SESSION['curtoat'] += $_POST['curnum'];		$_SESSION['curav']++;	} elseif ($_POST['instruction'] == "f") {		$result = $_SESSION['curtoat'] / $_SESSION['curav'];		echo "the average for the numbers you selected are ", $result;	}	else {		echo "ENDED";	}}?>

the only line of code that i added is the one with the comment, that's pretty much the usual way to deal with form data with bare php.

i tried to make the rest of the code more readable, that's your bonus for finishing your homeworks ^^ hopefully you will come to the joyful side of programming where we indent things

yeah i see what you did, i see i can see what it does, i do indent i promise :D:D:D:D Just afterwards, because i tend to normally shift lots and lots of code around, 6 tasks i had to complete with about 30 hours of php knowledge. the working out averages, an email validator, a pig latin one, shopping list (not finished but nearly), times table thingy, and binary/hexadecimal/octal/decimal (8bit) converter i cheated on this and just added 255 things on to a table :( took forever 

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
Share on other sites

Link to post
Share on other sites

yeah i see what you did, i see i can see what it does, i do indent i promise :D:D:D:D Just afterwards, because i tend to normally shift lots and lots of code around, 6 tasks i had to complete with about 30 hours of php knowledge. the working out averages, an email validator, a pig latin one, shopping list (not finished but nearly), times table thingy, and binary/hexadecimal/octal/decimal (8bit) converter i cheated on this and just added 255 things on to a table :( took forever 

heh doesn't sound fun, yep :rolleyes:

 

your signature says you're 14, are you? it looks very early to do these things in school

Link to comment
Share on other sites

Link to post
Share on other sites

heh doesn't sound fun, yep :rolleyes:

 

your signature says you're 14, are you? it looks very early to do these things in school

yeah i started last year in python. yeah im 14. ive always been good on computers, but only started coding last year.

its really not fun :( i like coding for my own ideas but i hate doing tasks other people set.

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
Share on other sites

Link to post
Share on other sites

yeah i started last year in python. yeah im 14. ive always been good on computers, but only started coding last year.

its really not fun :( i like coding for my own ideas but i hate doing tasks other people set.

wow, my school didn't show me a line of code until i was 16. UK FTW

 

the trick is to not give up to such bad solutions. there is probably a quick and easy solution for most of those basic problems. surely there is for base conversion in PHP. by doing things right at least you learn something useful, even if the problem wasn't interesting in the first place

Link to comment
Share on other sites

Link to post
Share on other sites

wow, my school didn't show me a line of code until i was 16. UK FTW

 

the trick is to not give up to such bad solutions. there is probably a quick and easy solution for most of those basic problems. surely there is for base conversion in PHP. by doing things right at least you learn something useful, even if the problem wasn't interesting in the first place

that is true, but would it be hard to convert hexadecimal, decimal,octal,binary? without using a table. i have insructions, but it involves using lots of appends and my one is so much easier

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
Share on other sites

Link to post
Share on other sites

wow, my school didn't show me a line of code until i was 16. UK FTW

Well that's much better than the school refusing to have a programming class because they were too lazy to find a teacher.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

that is true, but would it be hard to convert hexadecimal, decimal,octal,binary? without using a table. i have insructions, but it involves using lots of appends and my one is so much easier

using base_convert() you do everything with one function call. but then probably your teacher wanted you to practise on some algorithm so you needed to do the necessary maths. oh well.

 

Well that's much better than the school refusing to have a programming class because they were too lazy to find a teacher.

fascinating. i hope it wasn't a computer/programming oriented school

Link to comment
Share on other sites

Link to post
Share on other sites

fascinating. i hope it wasn't a computer/programming oriented school

Nah just a normal highschool that didn't feel like replacing a teacher after he retired.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Nah just a normal highschool that didn't feel like replacing a teacher after he retired.

you missed on great stuff. like, handwriting 4 pages of html and php for every test. loved high school programming /s

Link to comment
Share on other sites

Link to post
Share on other sites

Get yourself an IDE for PHP with Syntax highlighting. It will save you so much time and bother and inform you of those annoying commas out of place, and rogue full stops!

 

Check out Netbeans or Eclipse they are both great for PHP development.

 

Even NotePad++ would be a good place to start if you don't want a fully fledged IDE

// Gigabyte 990FXA-UD3 // AMD FX-8320 CPU @ 4.3 Ghz (7-21.5 Multiplier) 200.90mhz FSB CPU-Z Validated // Kraken X40 AIO - 2x140mm Push-Pull // 4GB Corsair Vengeance LP - 8GB Avexir Core Series Red 1760Mhz // Sapphire R9 Fury Nitro 1130mhz/4GB 1025mhz (Effective) GPU-Z Validation // Corsair SP2500 2.1 & Microlab Solo 9C Speakers // Corsair K90 Silver - Cherry MX Red & Blue LEDs // EVGA SuperNova 850w G2

Link to comment
Share on other sites

Link to post
Share on other sites

Get yourself an IDE for PHP with Syntax highlighting. It will save you so much time and bother and inform you of those annoying commas out of place, and rogue full stops!

 

Check out Netbeans or Eclipse they are both great for PHP development.

 

Even NotePad++ would be a good place to start if you don't want a fully fledged IDE

i use sublime text 2  currently. i have heard good things about Eclipse i will try it.

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
Share on other sites

Link to post
Share on other sites

you missed on great stuff. like, handwriting 4 pages of html and php for every test. loved high school programming /s

EHHH... tell me about it, still like that now in the UK.

 

yeah i will try using base convert later :D he gave us a "cheat sheet" which en English tells you one way to go round it its probably the hardest way to do it, adding subtracting and appending numbers, and a huge amount of if statements :/

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×