Hi every one

I have a very simple form and it just will not submit to the db. Could some one please tell me where I am going wrong?

<?php
        if ( $_SESSION['logged_in'] ):
    ?>
    (1)
        <?php
        endif;
    ?>
    <?php
    if($_POST['formSubmit'] == "Submit") 
    {
        $errorMessage = "";

        if(empty($_POST['social_media'])) 
        {
            $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_media = $_POST['formsocial_media'];
        $varurl = $_POST['formurl'];

        if(empty($errorMessage)) 
        {
            include '/dbc.php';

            $sql = "INSERT INTO social (id, social_media, url) VALUES (".
                            PrepSQL($varsocial_media) . ", " .
                            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_media'>Social Media Name</label></td>
                <td><input  type="text" name="formsocial_media"  size="40" maxlength="100" value="<?=$varsocial_media;?>"/></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>

Recommended Answers

All 6 Replies

Hi,

This code

if($_POST['formSubmit'] == "Submit") 

will eventually give you an error like .... error Undefined index: formSubmit, because it is not supported in newer version of php.

The most common and acceptable is

if(isset($_POST['formSubmit']))
{

## rest of your code here if the form has been set

}

Thank you for the reply. The problem seems to be when I click on submit it comes back adn states that I did not enter any information in the boxes. I am using autoincrement on teh db table for id. Which I understand does not need to be addressed in teh code as it will automaticaly add the id.

Hi,

Try, removing the id in

$sql = "INSERT INTO social (id, social_media, url) VALUES (".
PrepSQL($varsocial_media) . ", " .
PrepSQL($varurl) . ") " ;

Auto-increment will take care of it for you.. the key here is that all column names you have in insert statement, MUST be matched in the values statement.

e.g. INSERT INTO someTable(colName1, colName2, colName3) VALUES('colVal1',colVal2',colVal3').

In my example above, I ignored the fact or the existence of the column named id, because if it is set to auto-increment, then mysql will do it automatically.

There is a big CASE of IF here, if the mysql data has been imported from another existing table, the id may have some lower limit and max limit on it. This is something to watch out in the future.

Well I have tried that adn it still will not submit. I must have something else wrong in teh code.

$sql = "INSERT INTO social (id, social, url) VALUES (".
                            PrepSQL($id) . ", " .
                            PrepSQL($varsocial) . ", " .
                            PrepSQL($varurl) . ") " ;

just remove the id like this..

$sql = "INSERT INTO social (social, url) VALUES ('".
PrepSQL($varsocial) ."', '".
PrepSQL($varurl) ."') " ;

Thank you everyone for your help. I am not teh owner of a working form! lol

but once again thank you all for teh help. Some times the answer is staring you in teh face but you just can not see it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.