Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with Mysql Updating and inserting blank fields
    primarykey
    data
    text
    <p>I'm building a website that allows users to edit a business info. There is 6 selects, 3 for categories and 3 for subcategories. I'm using hidden fields to get the value of what each category and subcategory was before, and then if the select isset, then insert the select/dropdown's value. Here's my code: for the MYSQL Update:</p> <pre><code>&lt;? include('../config.php'); if (isset($_GET['id']) ) { $id = (int) $_GET['id']; if (isset($_POST['submitted'])) { foreach($_POST AS $key =&gt; $value) { $_POST[$key] = mysql_real_escape_string($value); } if(isset($_REQUEST['cselect1'])){ $cselect1 = $_REQUEST['cselect1']; $query="SELECT cat FROM cat WHERE id='$cselect1'"; $result = mysql_query ($query); while($catselect=mysql_fetch_array($result)){ $catselect1 = $catselect['cat']; } } else { $catselect1 = $_REQUEST['beforecat1']; } if(isset($_REQUEST['cselect2'])){ $cselect2 = $_REQUEST['cselect2']; $query="SELECT cat FROM cat WHERE id='$cselect2'"; $result = mysql_query ($query); while($catselect=mysql_fetch_array($result)){ $catselect2 = $catselect['cat']; } } else { $catselect2 = $_REQUEST['beforecat2']; } if(isset($_REQUEST['cselect3'])){ $cselect1 = $_REQUEST['cselect3']; $query="SELECT cat FROM cat WHERE id='$cselect3'"; $result = mysql_query ($query); while($catselect=mysql_fetch_array($result)){ $catselect1 = $catselect['cat']; } } else { $catselect3 = $_REQUEST['beforecat3']; } $sql = "UPDATE `company` SET `name` = '{$_POST['name']}' , `phone` = '{$_POST['phone']}' , `cat1` = '$catselect1' , `cat2` = '$catselect2' , `cat3` = '$cselect3' , `zipcode` = '{$_POST['zipcode']}' , `city` = '{$_POST['city']}' , `address` = '{$_POST['address']}' , `address2` = '{$_POST['address2']}' , `website` = '{$_POST['website']}' , `product1` = '{$_POST['product1']}' , `product2` = '{$_POST['product2']}' , `product3` = '{$_POST['product3']}' , `product4` = '{$_POST['product4']}' , `product5` = '{$_POST['product5']}' , `product6` = '{$_POST['product6']}' , `product7` = '{$_POST['product7']}' , `subcat1` = '{$_POST['sselect1']}' , `subcat2` = '{$_POST['sselect2']}' , `subcat3` = '{$_POST['sselect3']}' WHERE `id` = '$id' "; mysql_query($sql) or die(mysql_error()); $sql = "UPDATE `company_secondary` SET `company_description` = '{$_POST['description']}' , `since` = '{$_POST['since']}' , `smoking` = '{$_POST['select3']}' , `delivery` = '{$_POST['select5']}' , `alcohol` = '{$_POST['select6']}' , `kids` = '{$_POST['select1']}' , `wheelchair` = '{$_POST['select2']}' , `twitter` = '{$_POST['twitter']}' , `facebook` = '{$_POST['facebook']}' , `youtube` = '{$_POST['youtube']}' , `premium` = '{$_POST['premium']}' , `creditcards` = '{$_POST['select4']}' , `outdoor` = '{$_POST['select7']}' , `featured` = '{$_POST['featured']}' , `shortdesc` = '{$_POST['shortdesc']}' WHERE company_id = '$id' "; mysql_query($sql) or die(mysql_error()); echo "Edited Row&lt;br/&gt;"; echo "&lt;a href='allbiz.php'&gt;Back To Listing&lt;/a&gt;"; } $row = mysql_fetch_array ( mysql_query("SELECT * FROM `company` WHERE `id` = '$id' ")); ?&gt; </code></pre> <p>and here is the form that has the hidden inputs( only posted a portion of it, can post more if needed!)</p> <pre><code>&lt;form action='' method='POST'&gt; &lt;p&gt;&lt;b&gt;Name:&lt;/b&gt;&lt;br /&gt;&lt;input type='text' name='name' value='&lt;?= $row['name'] ?&gt;' /&gt; &lt;p&gt;&lt;b&gt;Phone:&lt;/b&gt;&lt;br /&gt;&lt;input type='text' name='phone' value='&lt;?= stripslashes($row['phone']) ?&gt;' /&gt; &lt;?php $cat1 = stripslashes($row['cat1']); $cat2 = stripslashes($row['cat2']); $cat3 = stripslashes($row['cat3']); $subcat1 = stripslashes($row['subcat1']); $subcat2 = stripslashes($row['subcat2']); $subcat3 = stripslashes($row['subcat3']); ?&gt; &lt;input type='hidden' value='&lt;?php echo $cat1;?&gt; name='beforecat1'/&gt; &lt;input type='hidden' value='&lt;?php echo $cat2;?&gt; name='beforecat2'/&gt; &lt;input type='hidden' value='&lt;?php echo $cat3;?&gt; name='beforecat3'/&gt; &lt;input type='hidden' value='&lt;?php echo $subcat1;?&gt; name='beforesubcat1'/&gt; &lt;input type='hidden' value='&lt;?php echo $subcat2;?&gt; name='beforesubcat2'/&gt; &lt;input type='hidden' value='&lt;?php echo $subcat3;?&gt; name='beforesubcat3'/&gt; &lt;p&gt;&lt;b&gt;Cat1:&lt;/b&gt;&lt;br /&gt; &lt;?php $query="SELECT * FROM cat"; $result = mysql_query ($query); echo"&lt;select name='cselect1' id='cat1'&gt;&lt;option value='0'&gt;Please Select A Category&lt;/option&gt;"; // printing the list box select command while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt echo "&lt;option value=\"".htmlspecialchars($catinfo['number'])."\"&gt;".$catinfo['cat']."&lt;/option&gt;"; } echo"&lt;/select&gt;"; ?&gt; &lt;?php $query="SELECT * FROM subcat"; $result = mysql_query ($query); echo"&lt;select name='sselect1' id='subcat1'&gt;&lt;option value=''&gt;Sub Category&lt;/option&gt;"; echo $subcat1; echo"'&gt;$sucat1&lt;/option&gt;"; // printing the list box select command while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt echo "&lt;option value='".htmlspecialchars($catinfo['subcat'])."' class='".$catinfo['catnumber']."'&gt;".$catinfo['subcat']."&lt;/option&gt;"; } echo"&lt;/select&gt;"; ?&gt; &lt;p&gt;&lt;b&gt;Cat2:&lt;/b&gt;&lt;br /&gt; &lt;?php $query="SELECT * FROM cat"; $result = mysql_query ($query); echo"&lt;select name='cselect2' id='cat2'&gt;&lt;option value='0'&gt;Please Select A Category&lt;/option&gt;"; // printing the list box select command while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt echo "&lt;option value=\"".htmlspecialchars($catinfo['number'])."\"&gt;".$catinfo['cat']."&lt;/option&gt;"; } echo"&lt;/select&gt;"; ?&gt; &lt;?php $query="SELECT * FROM subcat"; $result = mysql_query ($query); echo"&lt;select name='sselect2' id='subcat2'&gt;&lt;option value=''&gt;Sub Category&lt;/option&gt;"; echo $subcat1; echo"'&gt;$sucat1&lt;/option&gt;"; // printing the list box select command while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt echo "&lt;option value='".htmlspecialchars($catinfo['subcat'])."' class='".$catinfo['catnumber']."'&gt;".$catinfo['subcat']."&lt;/option&gt;"; } echo"&lt;/select&gt;"; ?&gt; &lt;p&gt;&lt;b&gt;Cat3:&lt;/b&gt;&lt;br /&gt; &lt;?php $query="SELECT * FROM cat"; $result = mysql_query ($query); echo"&lt;select name='cselect3' id='cat3'&gt;&lt;option value='0'&gt;Please Select A Category&lt;/option&gt;"; // printing the list box select command while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt echo "&lt;option value=\"".htmlspecialchars($catinfo['number'])."\"&gt;".$catinfo['cat']."&lt;/option&gt;"; } echo"&lt;/select&gt;"; ?&gt; &lt;?php $query="SELECT * FROM subcat"; $result = mysql_query ($query); echo"&lt;select name='sselect3' id='subcat3'&gt;&lt;option value=''&gt;Sub Category&lt;/option&gt;"; // printing the list box select command while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt echo "&lt;option value='".htmlspecialchars($catinfo['subcat'])."' class='".$catinfo['catnumber']."'&gt;".$catinfo['subcat']."&lt;/option&gt;"; } echo"&lt;/select&gt;"; ?&gt; </code></pre> <p>My hope was that if a user changes the Cat and Subcat select/dropdowns then that will be updated in the mysql query. Otherwise, if they are left untouched, then the hidden inputs selected from the database will be updated in the mysql table just like before. But something isn't working, when you submit the form without touching the selects/dropdowns, then the cats and subcats are wiped clean. How do I fix this?</p> <p>Thanks for all help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload