Jump to content

Php form will not send to db.


alexmark

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/265666-php-form-will-not-send-to-db/
Share on other sites

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>";
	 }

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";
}


	}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.