bullbreed Posted February 14, 2010 Share Posted February 14, 2010 Hi guys Please could you take a look at this code and tell me why it won't put the data from the form in to the database. I don't get an error. I get "Please complete all fields" as per the echo statement in the code. Thanks in advance <?php $submit = $_POST['submit']; //Form data $prodname = mysql_real_escape_string($_POST['prod_name']); $newprodcat = mysql_real_escape_string($_POST['newprod_cat']); $existprodcat = mysql_real_escape_string($_POST['existprod_cat']); $prodptitle = mysql_real_escape_string($_POST['prod_ptitle']); $prodpdescription = mysql_real_escape_string($_POST['prod_pdescription']); $prodpkeywords = mysql_real_escape_string($_POST['prod_pkeywords']); $pfrontimage = mysql_real_escape_string($_POST['pfrontimage']); $pbackimage = mysql_real_escape_string($_POST['pbackimage']); $pcolours = mysql_real_escape_string(serialize($_POST['pcolours'])); $psizes = mysql_real_escape_string(serialize($_POST['psizes'])); $prodprice = mysql_real_escape_string($_POST['prod_price']); $proddesc = mysql_real_escape_string($_POST['prod_desc']); $date = date('d/m/Y \a\t g:i.s a'); if (isset($_POST['submit'])) { if ($existprodcat !== ""){ $pcat = $existprodcat; }else{ $pcat = $newprodcat; } //Check for existing fields if ($prodname && $prodptitle && $prodpdescription && prodpkeywords && $pfrontimage && $pbackimage && $pcolours && $psizes && $prodprice && $proddesc && $date){ $pagecheck = mysql_query("SELECT `prod_name` FROM products WHERE `prod_name` = '$prodname'");echo mysql_error(); $count = mysql_num_rows($pagecheck); if ($count != 0){ echo("<font size=\"2\" color=\"#ff0000\">Product already exists. Please edit the existing product!</font>");echo mysql_error(); }else{ //Enter into Database $queryreg = mysql_query("INSERT INTO products (`prod_name`, `prod_cat`, `prod_ptitle`, `prod_pdescription`, `prod_pkeywords`, `prod_fimage`, `prod_bimage`, `prod_colours`, `prod_sizes`, `prod_price`, `prod_desc`, `date`) VALUES ('$prodname,' '$pcat', '$prodptitle', '$prodpdescription', '$prodpkeywords', '$pfrontimage', '$pbackimage', '$pcolours', '$psizes', '$prodprice', '$proddesc', '$date')");echo mysql_error(); echo("<font size=\"2\" color=\"#00cc00\">Your product has been created! </font>");echo mysql_error(); } }else{ echo ("<font size=\"2\" color=\"#ff0000\">Please complete <b>ALL</b> fields</font>");echo mysql_error(); } } //end Check for existing fields //end if submit $sql = "SELECT * FROM `products`"; $query = mysql_query($sql);echo mysql_error(); ?> <div class="sechead"><p>Add a product</p><br /> </div> <form action="" method="post" enctype="multipart/form-data" name="addproduct" target="_self" id="addproduct"> <div class="seccont"> <div class="infocont"> <div class="genwrap"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Product Name</p></div> </div> <div class="genwrap"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Create product category</p></div> </div> <div class="genwrap"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Select existing category</p></div> </div> <div class="genwrap"> <div class="titlewrap"><input name="prod_name" type="text" id="prod_name" size="25" /> </div> </div> <div class="genwrap"> <div class="titlewrap"><input name="newprod_cat" type="text" id="newprod_cat" size="25" /> </div> </div> <div class="genwrap"> <div class="titlewrap"> <?php //Open the database mysql_connect("localhost","root",""); mysql_select_db("customfight"); //Select the database $sql = "SELECT `prod_cat` FROM products"; $query = mysql_query($sql) or die(mysql_error()); $sections = array(); while ($row = mysql_fetch_array($query)){ $sections[] = $row[0]; } $sections = array_unique($sections); ?> <select name="existprod_cat" id="existprod_cat"> <option selected></option> <?php foreach($sections as $section){ echo '<option value="'.$section.'">'.$section.'</option>'; } ?> </select> </div> </div> </div> </div> <div class="sechead"> <p>Meta Data</p> </div> <div class="seccont"> <div class="genwrapmeta"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Title</p></div> </div> <div class="genwrapmeta"> <input name="prod_ptitle" id="prod_ptitle" type="text" size="80" maxlength="100" /> </div> <div class="genwrapmeta"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Description</p></div> </div> <div class="genwrapmeta"> <input name="prod_pdescription" type="text" id="prod_pdescription" size="80" maxlength="200" /> </div> <div class="genwrapmeta"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Keywords</p></div> </div> <div class="genwrapmeta"> <input name="prod_pkeywords" type="text" id="prod_pkeywords" size="80" maxlength="200" /> </div> </div> <div class="sechead">Product image front</div> <div class="seccont"><label><input type="file" name="pfrontimage" id="pfrontimage" /></label></div> <div class="sechead">Product image back</div> <div class="seccont"><label><input type="file" name="pbackimage" id="pbackimage" /></label></div> <div class="sechead">Product Sizes</div> <div class="seccont"> <label>S1 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label> <label>S2 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label> <label>S3 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label> <label>S4 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label> <label>S5 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label> <label>S6 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label> <label>S7 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label><br /><br /> <label>S8 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label> <label>S9 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label> <label>S10 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label></div> <div class="sechead">Page Colours</div> <div class="seccont"> <div class=" colourswrap"> <div class="colours"><img src="uploads/black.jpg" alt="Black" /></div><br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="Black" /> </div> <div class=" colourswrap"> <div class="colours"><img src="uploads/white.jpg" alt="White" /></div><br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="White" /> </div> <div class=" colourswrap"> <div class="colours"><img src="uploads/blue.jpg" alt="Blue" /></div><br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="Blue" /> </div> <div class=" colourswrap"> <div class="colours"><img src="uploads/red.jpg" alt="Red" /></div><br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="Red" /> </div> <div class=" colourswrap"> <div class="colours"><img src="uploads/pink.jpg" alt="Pink" width="22" height="22" /></div> <br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="Pink" /> </div> </div> <div class="sechead">Product Price</div> <div class="seccont"> <label> <input type="text" name="prod_price" id="prod_price" /> </label> </div> <div class="sechead">Page Content</div> <div class="seccont"> <textarea name="prod_desc" cols="80" rows="20" id="prod_desc"></textarea> </div> <div class="secbtm"><input type="submit" value="Save" name="submit" /></div> </form> Quote Link to comment https://forums.phpfreaks.com/topic/192027-cant-get-this-code-to-submit-to-database/ Share on other sites More sharing options...
bullbreed Posted February 14, 2010 Author Share Posted February 14, 2010 Oh and I changed the missing $ from prodpkeywords in the //check for existing fields Quote Link to comment https://forums.phpfreaks.com/topic/192027-cant-get-this-code-to-submit-to-database/#findComment-1012131 Share on other sites More sharing options...
teamatomic Posted February 14, 2010 Share Posted February 14, 2010 if ($prodname && $prodptitle && $prodpdescription && prodpkeywords && $pfrontimage && $pbackimage && $pcolours && $psizes && $prodprice && $proddesc && $date){ If you use an editor where you can set strings and vars to different colors these errors kinda stand right out. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/192027-cant-get-this-code-to-submit-to-database/#findComment-1012338 Share on other sites More sharing options...
PFMaBiSmAd Posted February 14, 2010 Share Posted February 14, 2010 You must have a connection to the database server before you can use mysql_real_escape_string(). Make your connection first. When learning php, developing php code, or in this case debugging php code, please do it on a server with error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php will help you by displaying all the errors it detects. You will save a TON of time. Stop and start your server to get any change made to the master php.ini to take effect and confirm the settings afterwards in case the php.ini that you are changing is not the one that php is using. Quote Link to comment https://forums.phpfreaks.com/topic/192027-cant-get-this-code-to-submit-to-database/#findComment-1012343 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.