alexmark Posted July 14, 2012 Share Posted July 14, 2012 Hi all I have a form please see code and it will not send to db. each time I try it it sends back the error message that no information has been entered in form. <?php if ( $_SESSION['logged_in'] ): ?> (1) <?php endif; ?> <?php if(isset($_POST['formSubmit'])) { $errorMessage = ""; if(empty($_POST['social'])) { $errorMessage .= "<li>You forgot to enter a Social Media Platform!</li>"; } if(empty($_POST['url'])) { $errorMessage .= "<li>You forgot to enter a URL!</li>"; } $varsocial = $_POST['formsocial']; $varurl = $_POST['formurl']; if(empty($errorMessage)) { include '/dbc.php'; $sql = "INSERT INTO social (id, social, url) VALUES (". PrepSQL($id) . ", " . PrepSQL($varsocial) . ", " . PrepSQL($varurl) . ") " ; mysql_query($sql); header("Location: thankyou.html"); exit(); } } // function: PrepSQL() // use stripslashes and mysql_real_escape_string PHP functions // to sanitize a string for use in an SQL query // // also puts single quotes around the string // function PrepSQL($value) { // Stripslashes if(get_magic_quotes_gpc()) { $value = stripslashes($value); } // Quote $value = "'" . mysql_real_escape_string($value) . "'"; return($value); } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Admin Add social Media</title> <link href="/csssheet.css" rel="stylesheet" type="text/css"> </head> <body> <center><div style="width:785px"> <table td class="background1" valign="top" width="785px" colspan="1" border=0 cellpadding=0 cellspacing=0 align="center"> <tr> <td width="100%"><center><br><a href= "../admin.php" class="creamlink11">Log Out</a></center></td> </tr> <td></td> </table> <?php include("../menu/admin_menu.php") ?> </td> </tr> <tr> <td bgcolor="#F6F6F6" height='1'></td> </tr> <tr> <td > <table width="785" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="785" bgcolor="#232B34" class="bodyfontred"> <table border=0 width='100%' cellspacing=0 cellpadding=0> <tr> <td valign="top" align=center> </td> </tr> <tr> <td align=center> <center> <img src="/images/linebreak.jpg" width="350" height="1"> </center> <?php if(!empty($errorMessage)) { echo("<p>There was an error with your form:</p>\n"); echo("<ul>" . $errorMessage . "</ul>\n"); } ?> <form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post" enctype="multipart/form-data"> <table> <tr> <td class="bodyfontwhite" align="right"> <label for='formsocial'>Social Media Name</label></td> <td><input type="text" name="formsocial" size="40" maxlength="100" value="<?=$varsocial;?>"/></td> <td><label for='formurl'>URL</label></td> <td><input type="text" name="formurl" size="40" maxlength="250" value="<?=$varurl;?>"/></td> </tr> <tr> </tr> </table> <center><input type="submit" name="formSubmit" value="Submit"/></center> </form> <tr><td class=bodyfontwhite> <center><b><a name='help'></a>H E L P</b></center><br> <p align=justify>To Add a Social Net Work type the text you wish to display. Add the URL address Then click the Submit button. </p> </td></tr></table> </td> </tr> </table> </td> </tr> <tr> <center><td width="785" align="center" bgcolor="#000000" class="bodyfont" height="40"><a href="http://www.improvedinternetmarketing.com/" title="Improved Internet Marketing">Web Design</a> by Improved Internet Marketing</TD></center> </tr> </table> </div></center> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/265666-php-form-will-not-send-to-db/ Share on other sites More sharing options...
lordshoa Posted July 14, 2012 Share Posted July 14, 2012 Missing form infront of your post Vars if(empty($_POST['social'])) { $errorMessage .= "<li>You forgot to enter a Social Media Platform!</li>"; } if(empty($_POST['url'])) { $errorMessage .= "<li>You forgot to enter a URL!</li>"; } if(empty($_POST['formsocial'])) { $errorMessage .= "<li>You forgot to enter a Social Media Platform!</li>"; } if(empty($_POST['formurl'])) { $errorMessage .= "<li>You forgot to enter a URL!</li>"; } Quote Link to comment https://forums.phpfreaks.com/topic/265666-php-form-will-not-send-to-db/#findComment-1361507 Share on other sites More sharing options...
alexmark Posted July 14, 2012 Author Share Posted July 14, 2012 lordshoa you are the star! I have another question though now that it submits correctely. How do i echo out a statment saying that it has entered the info correctely in the db? Would the following work? mysql_query($sql); // if successfully insert data into database, displays message "Successful". if($result){ echo "Successful"; } else { echo "ERROR"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/265666-php-form-will-not-send-to-db/#findComment-1361517 Share on other sites More sharing options...
lordshoa Posted July 14, 2012 Share Posted July 14, 2012 look at the function mysql_affected_rows() Quote Link to comment https://forums.phpfreaks.com/topic/265666-php-form-will-not-send-to-db/#findComment-1361520 Share on other sites More sharing options...
alexmark Posted July 14, 2012 Author Share Posted July 14, 2012 Thank you for pointing me in the right direction. But how do I use this statment to echo result back to the same page as the form? Quote Link to comment https://forums.phpfreaks.com/topic/265666-php-form-will-not-send-to-db/#findComment-1361533 Share on other sites More sharing options...
lordshoa Posted July 14, 2012 Share Posted July 14, 2012 somethig like that mysql_query($sql); if(mysql_affected_rows($sql)){ print 'inserted correctly'; } Quote Link to comment https://forums.phpfreaks.com/topic/265666-php-form-will-not-send-to-db/#findComment-1361534 Share on other sites More sharing options...
alexmark Posted July 15, 2012 Author Share Posted July 15, 2012 Thank you for your help. I was able to sort teh problem out and now have it working. Quote Link to comment https://forums.phpfreaks.com/topic/265666-php-form-will-not-send-to-db/#findComment-1361751 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.