Jump to content

PHP insert into not working


jaykappy
Go to solution Solved by gizmola,

Recommended Posts

I am trying to insert a record from values from a FORM.

I run the INSERT INTO and nothing happens, no error not new record...maybe another pair of eyes might help...

 

My confusion is that I have an "id" field in the table...Its set to autoNumber so I dont know if I have to reference it in the Queries below?????  If so I would passa a null value because it woudl autoNUmber....

 

Any thoughts?  I have the update working fine and using the same login in code.


    // First we execute our common code to connection to the database and start the session 

    require("commonMapLibrary.php");  


 

 

 

MainPageIndex.php


<form class="CreateFormRow" id="CreateDataform" action="CreateNewRecord.php?Dept=<?php echo $_GET['Dept'] ?>" method="post" style="display:none">
<legend>Add Record</legend>  
<ol style="float:left">
<li>
<label for=name>id</label>
<input id=name type="text" name="Cid" value="" placeholder="ID" />
</li>
<li >
<label for=name>Year</label>
<input id=name type="text" name="CYr" value="" placeholder="Year of Project" required autofocus /> </li>
<li>
<input id=name type="text" name="CProjectName" value="" placeholder="Project Name"  />
</li>
<li>
<label for=phone>Department</label>
<input id=name type="text" name="CDepartment" value="" placeholder="Department"/>
</li>
<li>
<label for=name>Owner</label>
<input id=name type="text" name="COwner" value="" placeholder="Owner" />
</li>
<li>
<label for=email>Maplibrary Name</label>
<input id=name type="text" name="CMaplibraryName" value="" placeholder="Maplibrary Name"  />
</li>
<li>
<label for=email>Description</label>
<input id=name type="text" name="CDescription" value="" placeholder="Description"  />
</li>
<li>
<label for=phone>Maplibrary Path</label>
<input id=name type="text" name="CMaplibraryPath" value="" placeholder="Maplibrary Path"  />
</li>
</ol>
<ol style="float:right">
<li>
<label for=phone>Page Size</label>
<input id=name type="text" name="CPageSize" value="" placeholder="Page Size"  />
</li>
<li>
<label for=phone>Scale</label>
<input id=name type="text" name="CScale" value="" placeholder="Scale"  />
</li>
<li>
<label for=phone>Last Modified</label>
<input id=name type="text" name="CLastModified" value="" placeholder="Last Modified"  />
</li>
<li>
<label for=phone>Notes</label>
<input id=name type="text" name="CNotes" value="" placeholder="Notes"  />
</li>
<li>
<label for=phone>Modified By</label>
<input id=name type="text" name="CModifiedBy" value="" placeholder="Modified By"  />
</li>
<li>
<label for=phone>Full Path</label>
<input id=name type="text" name="CFullPath" value="" placeholder="Full Path"  />
</li>
<li>
<label for=phone>Path    jpg</label>
<input id=name type="text" name="CPath_jpg" value="" placeholder="Path_jpg"  />
</li>
</ol>
<legend></legend>
<button id="" class="Submitbutton" type=submit>Submit</button>
<button id="CloseButtonAdd" onClick="CloseForm()">Close</button><br>
</form>

 

 

CreateNewRecord.php


<?php 
 
    // First we execute our common code to connection to the database and start the session 
    require("commonMapLibrary.php");  
 
// At the top of the page we check to see whether the user is logged in or not 
    if(empty($_SESSION['user'])) 
    { 
        // If they are not, we redirect them to the login page. 
        header("Location: login.php"); 
        die("Redirecting to login.php"); 
    }else{
 
   }
 
    if(!empty($_POST)) 
    { 
        if(empty($_POST['CProjectName'])) 
        { 
            die("Please enter a Project Name."); 
        } 
       
      $query = "INSERT INTO tbl_maplibrary ( 
                Yr,
                ProjectName,
                Department, 
                Owner, 
                MaplibraryName,
                Description, 
                MaplibraryPath,
                PageSize, 
                Scale, 
                LastModified, 
                Notes, 
                ModifiedBy, 
                FullPath,
                Path_jpg 
            ) VALUES (
                :Yr,
               :ProjectName,
               :Department,
               :Owner,
               :MaplibraryName,
               :Description,
               :MaplibraryPath,
               :PageSize,
               :Scale,
               :LastModified,
               :Notes,
               :ModifiedBy,
               :FullPath,
               :Path_jpg
            ) 
        "; 
 
$query_params = array(
            ':Yr' => $_POST['CYr'], 
            ':ProjectName' => $_POST['CProjectName'],
            ':Department' => $_POST['CDepartment'], 
            ':Owner' => $_POST['COwner'], 
            ':MaplibraryName' => $_POST['CMaplibraryName'],
            ':Description' => $_POST['CDescription'],
            ':MaplibraryPath' => $_POST['CMaplibraryPath'],
            ':PageSize' => $_POST['CPageSize'],
            ':Scale' => $_POST['CScale'],
            ':LastModified' => $_POST['CLastModified'],
            ':Notes' => $_POST['CNotes'],
            ':ModifiedBy' => $_POST['CModifiedBy'],
            ':FullPath' => $_POST['CFullPath'],
            ':Path_jpg' => $_POST['CPath_jpg'] 
        ); 
 
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
 
?> 
Link to comment
Share on other sites

I added this die in the Try statement and I get the return text... die("You got to the query");

so its in the right area and the EXECUTE is firing (well the code is going right passed it) but its not saving the new record...

 

Is there anything I can test?  Same connection I am using for the update statement, user has insert and update permissions.

No errors nothing....just dosent update the table in the db

 

WHY?

 



       try 
        { 
            // Execute the query to create the user
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
  die("You got to the query");
        } 
        catch(PDOException $ex) 
        { 
            // Note: On a production website, you should not output $ex->getMessage(). 
            // It may provide an attacker with helpful information about your code.  
            die("Failed to run query: " . $ex->getMessage()); 
        }

Edited by jaykappy
Link to comment
Share on other sites

Yes I meant Auto Increment.....I have removed the id from the Query and Query Params Array...it was not shown in the first post here...

 

 

I added this:  and get this in return

 

PDOStatement::errorInfo():

 

can I print the actual query? and verifiy it looks right?  DONT know what else to do

 

$stmt = $db->prepare($query); 
 
$result = $stmt->execute($query_params); 
 
echo "\nPDOStatement::errorInfo():\n";
$arr = $sth->errorInfo();
print_r($arr);
Edited by jaykappy
Link to comment
Share on other sites

I changed the name of the table in the database and I didnt get an error...very weird...

 

From

 

$query = " INSERT INTO tbl_maplibrary ( 
 
To
$query = " INSERT INTO tbl_maplibrary99 ( 

 

This error should have been caught right? and the "Failed to run Query" shodl have been displayed

 

 

        try 
        { 
            // Execute the query to create the user  
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
            echo "\nPDOStatement::errorInfo():\n";
        } 
        catch(PDOException $ex) 
        { 
            die("Failed to run query: " . $ex->getMessage()); 
        } 
Link to comment
Share on other sites

OK I can get all the Fields to INSERT INTO except the one that was (i guess causing the error).

The field is a DATE field....i entered 5/5/2013 as a value...

 

Any thoughts as to why I would be blowing up on the date field INSERT INTO

 

Why cant I add the Date Field?

Edited by jaykappy
Link to comment
Share on other sites

 P5system - how do I do that...sorry for the stupid question...still new to this but believe this is very important...

 

Gizmola - That was it...it worked in that format....but that's such a a weird format for someone to put in....Is there any way to add some sort of calendar control that will push the value to the input box in that format?

Edited by jaykappy
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.