Getting error need help

got this error

C:\wamp64\www\site\admin\teset.php:31:string ‘insert into subcategory (cid, scatname) values (1, sdds)’ (length=56)

That’s not an error, that’s the output from var_dump(). Add the quotes back in around the subcategory name. Does that query look similar to the query you can enter in the back end that does work?

If that doesn’t help, then change this line

echo "Please Check your query";

to read

echo "Problem with query " . mysqli_error($con);

C:\wamp64\www\site\admin\teset.php:31:string ‘insert into subcategory (‘cid’, ‘scatname’) values (‘1’, ‘sdds’)’ (length=64)

added this also

echo "Problem with query " . mysqli_error($con);

And what does it say alongside “Problem with query” when you run the code? It should give more information on what caused the error.

ETA - Sorry, forgot to add - remove the exit(); from the code, so it actually executes the query again.

EFTA - I only meant put queries around the subcategory name, not the category id. I don’t know whether quoting a number will cause a problem when you try to insert it into an integer column. I use prepared statements which takes a lot of that trouble away.

C:\wamp64\www\site\admin\teset.php:31:string ‘insert into subcategory (‘cid’, ‘scatname’) values (‘1’, ‘dssd’)’ (length=64)

Notice: Undefined variable: result in C:\wamp64\www\site\admin\teset.php on line 34
Call Stack

# Time Memory Function Location
1 0.0002 404880 {main}( ) ...\teset.php **:** 0

Problem with query

Can you show that section of code again please? And indicate which is line 34.

				// after this line in your code, however it looks now, but back to the correct table

$query = “insert into subcategory (‘cid’, ‘scatname’) values (‘$_POST[cat]’, ‘$subcat’)”;
// *** ADD THESE LINES
var_dump($query);

				if ( $result ) {
					echo '<span style="color: green;">SubCategory Added Successfully</span>';
				} else {
			echo "Problem with query " . mysqli_error($con);
				}
			}
		}

its - if ( $result ) one

You seem to have removed the line where you actually execute the query, hence the result variable is never created.

i have done what you said what i missed n where please tell

Have a look at the code you posted in post #17, you have removed the line that starts $result = mysqli_query() so the query doesn’t run any more, and you don’t create a $result variable, hence this error message. I just said to remove the exit() line, not the one after it. I’d copy/paste the line, but I’m having a lot of trouble doing copy/paste operations since the recent forum software upgrade - it either copies the wrong thing, or nothing at all.

One thought - I am presuming that your “header.php” is the code that connects to the database and returns the connection in $con - is that the case?

here’s what i get now after adding them

Problem with query You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘‘cid’, ‘scatname’) values (‘1’, ‘sddsds’)’ at line 1

What kind of quotes are you using? On the forum display, it looks as if the quote characters are different for open and close, but I think that’s the forum software making things pretty. I don’t think you need to put quotes around the column names, and I think if you do (because they’re reserved words, or contain spaces) then you use back-ticks, not quotes.

ETA - you said if you run the query from the back end it works correctly - compare that query (in terms of quotes and so on) to the above, and see how it varies.

after removing the quotes getting this
C:\wamp64\www\site\admin\teset.php:31:string ‘insert into subcategory (cid, scatname) values (1, sdd)’ (length=55)
Problem with query Unknown column ‘sdd’ in ‘field list’

here is the code

<?php

			if ( isset($_POST[ 'submit' ] ) ) {
				if ( empty($_POST[ 'subcat' ] ) ) {
					echo '<span style="color: red;"> Please Fill in the Category Name</span> ';
				} else {
					$subcat = clean( $_POST[ 'subcat' ] );
					
					
					
						// after this line in your code, however it looks now, but back to the correct table
$query = "insert into subcategory (cid, scatname) values ($_POST[cat], $subcat)";
// *** ADD THESE LINES
var_dump($query);
					$result = mysqli_query( $con, $query );

					if ( $result ) {
						echo '<span style="color: green;">SubCategory Added Successfully</span>';
					} else {
					echo "Problem with query " . mysqli_error($con);
					}
				}
			}



			?>

Off Topic:

@srsingh83: when you post code on the forums, you need to format it so it will display correctly. (I’ve edited your earlier posts for you.)

You can highlight your code, then use the </> button in the editor window, or you can place three backticks ``` (top left key on US/UK keyboards) on a line above your code, and three on a line below your code. I find this approach easier, but unfortunately some European and other keyboards don’t have that character.

No. Just remove the quotes from the column names, NOT the values. Do this

insert into subcategory (cid, scatname) values (1, 'sdd')

Exactly what quotes did you have, and where, in the working query that you were able to run directly? If you created it from the form, I think it shows the query when it is executed.

are you asking me to put the code this way

$query = insert into subcategory (cid, scatname) values ($_POST[cat], $subcat);

I think you still need single-quotes around the $subcat variable. Take a second to try it both ways though.

yes thanks
putting this works

$query = “insert into subcategory (cid, scatname) values ($_POST[cat], ‘$subcat’)”;

now i can add the subcategory

Excellent.

Now read up on prepared statements. As well as security improvements, they do away with all this messing around with quotes in queries.

ok thanks for the help

1 Like