twilitegxa Posted June 3, 2011 Share Posted June 3, 2011 I'm trying to make an editable form that populates with the fields from a database, but my textarea field isn't working properly. To display the data from the textarea's field, I'm using the following syntax: while($image=mysql_fetch_array($all_records_res)){ $id = $image['id']; $cut = $image['cut']; $color = $image['color']; $carats = $image['carats']; $clarity = $image['clarity']; $size = $image['size']; $type = $image['type']; $other = $image['other']; $total = $image['total']; $certificate = $image['certificate']; $value = $image['value']; $name = $image['name']; $desc = $image['desc']; $item_no = $image['item_no']; $metal = $image['metal']; $angle1 = $image['image']; $angle2 = $image['image2']; echo "<tr> <td>Item Number:</td> <td><input type='text' id='item_no' name='item_no' value='$item_no' /></td> </tr> <tr> <td>Item Name:</td> <td><input type='text' id='item_name' name='item_name' value='$name' /></td> </tr> <tr> <td>Item Description:</td> <td><textarea id='desc' name='desc' rows='10' cols='40'>$desc</textarea></td> </tr> <tr> <td>Item Type:</td> <td><input type='text' id='type' name='type' value='$type' /></td> </tr> <tr> <td>Value:</td> <td><input type='text' id='value' name='value' value='$value' /></td> </tr> <tr> <td>Gem Cut:</td> <td><input type='text' id='cut' name='cut' value='$cut' /></td> </tr> <tr> <tr> <td>Gem Color:</td> <td><input type='text' id='color' name='color' value='$color' /></td> </tr> <tr> <td>Gem Carats:</td> <td><input type='text' id='carats' name='carats' value='$carats' /></td> </tr>... And the php page that is supposed to update is shown here: <?php $cut = $_POST['cut']; $color = $_POST['color']; $carats = $_POST['carats']; $clarity = $_POST['clarity']; $size = $_POST['size']; $metal = $_POST['metal']; $other = $_POST['other']; $total = $_POST['total']; $certificate = $_POST['certificate']; $value = $_POST['value']; $angle1 = $_POST['angle1']; $name = $_POST['item_name']; $desc = $_POST['desc']; $item_no = $_POST['item_no']; $type = $_POST['type']; $angle2 = $_POST['angel2']; $dbhost = 'localhost'; $dbuser = 'jhrevell_jewelry'; $dbpass = 'jewelry123'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'jhrevell_jewelry'; mysql_select_db($dbname); mysql_query ("UPDATE gallery SET cut = '$cut', color = '$color', carats = '$carats', clarity = '$clarity', size = '$size', metal = '$metal', other = '$other', total = '$total', certificate = '$certificate', value = '$value', image = '$angle1', name = '$name', item_no = '$item_no', type = '$type', image2 = '$angle2', desc = '$desc' WHERE id = '$_GET[id]'"); header('Location: http://midwestcreativeconsulting.com/jhrevell/manage'); ?> But the desc field (the textarea one) is not working. I'm thinking it's because I have the php variable set on the form, but I'm not sure. Is there another way to do this? Please, can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/238278-editable-form-using-php-variables-textarea/ Share on other sites More sharing options...
kenrbnsn Posted June 3, 2011 Share Posted June 3, 2011 Why do you think it's not working? Errors? Since you don't check to make sure the query is working... Also, never trust user input. You don't validate any input. At the very least process all string data through mysql_real_escape_string Ken Quote Link to comment https://forums.phpfreaks.com/topic/238278-editable-form-using-php-variables-textarea/#findComment-1224461 Share on other sites More sharing options...
twilitegxa Posted June 3, 2011 Author Share Posted June 3, 2011 What is not working is when I try to type something else into the textarea to save it, it is not updating in the database. Sorry if I miscommunicated that. And thank you for reminding me about the validation. I'm going to have to work on that next ;-) By the way, everything else is being updated in the database, the only one that doesn't work in the desc one. Not sure exactly why except for what I already stated. Quote Link to comment https://forums.phpfreaks.com/topic/238278-editable-form-using-php-variables-textarea/#findComment-1224465 Share on other sites More sharing options...
Drummin Posted June 3, 2011 Share Posted June 3, 2011 As it's more than likely being saved to "text" database field you should use mysql_real_escape_string() to escape input before adding to DB as already suggested. Quote Link to comment https://forums.phpfreaks.com/topic/238278-editable-form-using-php-variables-textarea/#findComment-1224469 Share on other sites More sharing options...
kenrbnsn Posted June 3, 2011 Share Posted June 3, 2011 If you had done some error checking on the query like this, <?php $q = "UPDATE gallery SET cut = '$cut', color = '$color', carats = '$carats', clarity = '$clarity', size = '$size', metal = '$metal', other = '$other', total = '$total', certificate = '$certificate', value = '$value', image = '$angle1', name = '$name', item_no = '$item_no', type = '$type', image2 = '$angle2', desc = '$desc' WHERE id = '$_GET[id]'"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); ?> You would have gotten an error on the query because "desc" is a reservered word in MySQL. You need to either rename the field or put backticks ` around the word: <?php $q = "UPDATE gallery SET cut = '$cut', color = '$color', carats = '$carats', clarity = '$clarity', size = '$size', metal = '$metal', other = '$other', total = '$total', certificate = '$certificate', value = '$value', image = '$angle1', name = '$name', item_no = '$item_no', type = '$type', image2 = '$angle2', `desc` = '$desc' WHERE id = '$_GET[id]'"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/238278-editable-form-using-php-variables-textarea/#findComment-1224481 Share on other sites More sharing options...
twilitegxa Posted June 12, 2011 Author Share Posted June 12, 2011 kenrbnsn, Thank you so much for your help! I am terrible at not setting up error checking. I will try to remember to start using it more often. Your suggestion worked perfectly! Thank you again sooo much!!!! Quote Link to comment https://forums.phpfreaks.com/topic/238278-editable-form-using-php-variables-textarea/#findComment-1228710 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.