Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Mysql error

Featured Replies

$sql = 'INSERT INTO category (id, cat_name, cat_desc) VALUES (NULL, $cat_name, $cat_desc)';

 

That is my MySql code to insert into table,

 

But I'm getting an error when i submit the form which is...

 

ErrorUnknown column '$cat_name' in 'field list'Query.

 

If i "" the variables it inserts the variables as they appear, but i cant help think what i am doing wrong?

 

Many Thanks

 

Ben

Hi Ben

 

$sql = 'INSERT INTO category (id, cat_name, cat_desc) VALUES (NULL, "'.$cat_name.'", "'.$cat_desc.'")';

 

As you have used single quotes at the beginning of your string you need to use them to escape any variables within it, the full stop is used as a string / variable joiner. Then the double quotes just encapsulate the variable you require inserting.

 

Try that

 

Andy

  • Author

Hi Ben

 

$sql = 'INSERT INTO category (id, cat_name, cat_desc) VALUES (NULL, "'.$cat_name.'", "'.$cat_desc.'")';

 

As you have used single quotes at the beginning of your string you need to use them to escape any variables within it, the full stop is used as a string / variable joiner. Then the double quotes just encapsulate the variable you require inserting.

 

Try that

 

Andy

Thank you very much, worked like a charm :D

 

Ben

  • Author

As this is along the same lines,

 

having a problem with making a drop down box retrieve a list of categories from the database.

 

<select name="existing">
       	<option value="0">Select One</option>
     	<?php //retrive all catagories and add to pull down menu
	$sql = 'SELECT * FROM category'; 
	$r = mysqli_query($dbc,$sql);

	if (mysql_num_rows($r) > 0) {
		while ($row = mysql_fetch_array($r,MYSQL_ASSOC)) {
echo "<option value=\"{$row[cat_id]}\">{$row['cat_name']}</option>\n";
		}
	} else {
		echo '<option>Please add and new category</option>';
	}
	mysqli_close($dbc); //close connection

		?>   
           </select>

My $dbc variable connects to the database etc

 

I'm also getting this

<b>Warning</b>:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in <b>/home2/irn3rdc/public_html/gorsehill/public_html/user1/index.php</b> on line <b>111</b><br />

 

When I check the code behind the scenes.

 

As always help is much appreciated.

 

P.S used this as a guide, My link

  • 2 weeks later...
  • Author

Got another problem with this snippet of code...

 

 

Its working on page load and on a form submit with errors. but when i submit for with correct information it decides it doesn't want to work..

 

<b>Warning</b>:  mysqli_query() [<a href='function.mysqli-query'>function.mysqli-query</a>]: Couldn't fetch mysqli in <b>/home2/irn3rdc/public_html/gorsehill/public_html/user1/index.php</b> on line <b>146</b><br />

<br />
<b>Warning</b>:  mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in <b>/home2/irn3rdc/public_html/gorsehill/public_html/user1/index.php</b> on line <b>147</b><br />
<option>Please add and new category</option><br />
<b>Warning</b>:  mysqli_close() [<a href='function.mysqli-close'>function.mysqli-close</a>]: Couldn't fetch mysqli in <b>/home2/irn3rdc/public_html/gorsehill/public_html/user1/index.php</b> on line <b>154</b><

 

Errors output in source code.

 

Current state of code.

<select name="existing">
       	<option value="0">Select One</option>
     		<?php //retrive all catagories and add to pull down menu
		$sql = 'SELECT * FROM category'; 
		$r = mysqli_query($dbc,$sql);
			if (mysqli_num_rows($r)) {
				while ($row = mysqli_fetch_array($r)) {
					echo "<option value=\"{$row['id']}\">{$row['cat_name']}</option>\n";
				}
			} else {
				echo '<option>Please add and new category</option>';
			}
	mysqli_close($dbc); //close connection
		?>   
           </select>

 

Is there something simple I have missed?

 

Ben

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.