Hi everyone,

I wrote an INSERT for an insert form but for some reason it wont save the data tot he database. In phpMyadmin the rows are taken up with blank nothings. It seems to work but nothing is being stored. code as follows...

session_start();
    
    $db_hostname = "localhost";
    $db_password = "";
    $db_username = "root";
    $db_name = "DataStore";
    
    if(isset($_POST['saveProd'])){
        $prodName = $_POST['prodName'];
        $writtenBy = $_POST['writtenBy'];
        $producedBy = $_POST['producedBy'];
        $prodRating = $_POST['prodRating'];
        $overview = $_POST['overview'];

        $_SESSION['prodName'] = $prodName;
	$_SESSION['writtenBy'] = $writtenBy;
	$_SESSION['producedBy'] = $producedBy;
	$_SESSION['overview'] = $overview;
       
        $anyErrors = false;
        
        if ($prodName == "" || $prodName= null){
            $noName  = "<span class=\"alert-red\"> <img src=\"images/icons/warning.png\" /> What is the name of this production?</span>";
            $anyErrors = true;
        }
        if ($writtenBy == "" || $writtenBy = null){
            $noWritten  = "<span class=\"alert-red\"> <img src=\"images/icons/warning.png\" /> Who wrote this production?</span>";
            $anyErrors = true;
        }
        if ($producedBy == "" || $producedBy = null){
            $noProduce  = "<span class=\"alert-red\"> <img src=\"images/icons/warning.png\" /> Who is the Producer?</span>";
            $anyErrors = true;
        }
        if ($prodRating == 0 || $prodRating = null){
            $noRating  = "<span class=\"alert-red\"> <img src=\"images/icons/warning.png\" /> Please select a rating!</span>";
            $anyErrors = true;
        }
        if ($overview == "" || $overview = null){
            $noOverview  = "<span class=\"alert-red\"> <img src=\"images/icons/warning.png\" /> Please enter an overview of the production!</span>";
            $anyErrors = true;
        }
            
        if ($anyErrors == false){
            
            $conn = mysql_connect($db_hostname, $db_username, $db_password) or die(mysql_error());
            mysql_select_db($db_name, $conn) or die(mysql_error());
            
            $prodQuery = "INSERT INTO productions (production_name, written_by, produced_by, overview, parental_advice) VALUES('$prodName', '$writtenBy', '$producedBy', '$overview', '$prodRating')";
            if (!mysql_query($prodQuery,$conn)){
                $prodMess = "<span class=\"alert-green\">Submission Failed <img src=\"images/icons/action_delete.png\" /></span>";
                die('Error: ' . mysql_error());
            }
            else{
                $prodMess = "<span class=\"alert-green\">Submission Successful <img src=\"images/icons/action_check.png\" /></span>";
            }
        }

html form code is...

<form name="addProd" method="POST" action="admin_insert.php">
                    <table>
                        <tr>
                            <td><label class="right">New Production Name</label></td>
                            <td><input type="text" name="prodName" id="prodName" class="frm-txt3" value="<?php echo $_SESSION['prodName'];?>" > <?php echo $noName;?></td>
                        </tr>
                        <tr>
                            <td><label class="right">Written By</label></td>
                            <td><input type="text" name="writtenBy" id="writtenBy" class="frm-txt3" value="<?php echo $_SESSION['writtenBy'];?>"> <?php echo $noWritten;?></td>
                        </tr>
                        <tr>
                            <td><label class="right">Produced By</label></td>
                            <td><input type="text" name="producedBy" id="ProducedBy" class="frm-txt3" value="<?php echo $_SESSION['producedBy'];?>"> <?php echo $noProduce; ?></td>
                        </tr>
                        <tr>
                            <td><label class="right">Parental Supervision</label></td>
                            <td>
                                <select name="prodRating" id="prodRating">
                                    <option value="0">Select</option>
                                    <option value="1">No parental restrictions</option>
                                    <option value="2">Parental guidance necessary</option>
                                    <option value="3">For mature audiences</option>
                                </select>
                                <?php echo $noRating;?>
                            </td>
                        </tr>
                        <tr>
                            <td valign="top"><label class="right">Overview of Production</label></td>
                            <td valign="top"> <span valign="top"><?php echo $noOverview;?></span> <textarea name="overview" id="overview" rows="20" cols="50" style="border: 1px solid #666;" value="<?php echo $_SESSION['overview'];?> "></textarea> </td>
                        </tr>
                    
                        <tr>
                            <td></td>
                            <td><input type="submit" name="saveProd" class="frm-sub2" value="SAVE  &raquo;" />     <?php echo $prodMess;?></td>
                        </tr>
                        
                    </table>
                </form>

This should be very straight forward, but I just cant figure out what is wrong? Please help.

Recommended Answers

All 2 Replies

echo the $sql statment and see if the variables $prodName, $writtenBy, $producedBy and $prodRating are being populated. If the sql statment comes out as it should then copy the statement directly into PHPmyAdmin and check if it works correctly. However, from your statement saying that the db is being poulated with blanks, it looks like the variables are empty.

echo the variables separately after you pull the variables from the $_POST and check them. You'll know the problem if they are blank. If they are not blank then there is some place in the middle where they are becoming blank. Use the same echo technique to find out where.

All the best!

Your advice worked immensely. Apparently if you look close enough, in the condition where I check if the values are blank (validation) i have the $variable = null while i should have had &variable == null.

Thanks a lot for reminding me of the echo test sudeepjd. :)

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.