August 16, 200917 yr $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
August 16, 200917 yr 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
August 16, 200917 yr 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 Ben
August 16, 200917 yr 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
August 27, 200917 yr 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