Log in

View Full Version : Insertion is not happening while using PDO codes along with plain php script



megha_3000
06-17-2013, 09:27 AM
Hello everybody.

Got a problem at inserting data to database. i.e when i m using the pdo script with
a php script having session. the data is not inserted. but data insertion executes
successfully while i use that PDO script individually.

you may view the link http://www.codeofaninja.com/2013/05/crud-with-php-jquery.html

which i followed. Now my

language.php contains :




<?php
session_start();
error_reporting(E_ALL | E_STRICT);
/*****************************************************************************

/*****************************************************************************/
include_once('includes/queryfunctions.php');
include_once('includes/functions.php');
$conn=mysql_connect(HOST . ":" . PORT , USER, PASS);
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db(DB);

//check if user is logged in
SignedIn();

//check if user has clicked on logout button
if(isset($_POST["submit"]) && $_POST["submit"]=='Logout') LogOut();

if(isset($_GET["search"]) && !empty($_GET["search"])){
//have this as a search function
$id=$_GET["search"];
$_POST["submit"]=$_GET["action"];
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>language</title>
<link rel="stylesheet" type="text/css" href="css/internal.css"/><link href="css/main.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="css/epoch_styles.css"/>
<?php headericon(); ?>

</head>
<body bgcolor="#FFFFFF">
<form action="language.php" method="post" name="language" id="language" enctype="multipart/form-data">
<div align="center">
<table width="92%">
<tr>
<td colspan="3" align="right"><?php mainheader(); ?></td>
</tr>
<tr align="center">
<th colspan="3" align="right"><img src="images/head.png" width="100%" height="95px" /></th>
</tr>
<tr>
<th><?php loginheader(); ?></th>
<th colspan="2"><?php profileheader(); ?></th>
</tr>
<tr>
<td width="20%" valign="top" align="left"><?php leftmenu(); ?></td>
<td colspan="2" valign="top" align="left"><table border="0" width="100%">
<tr>
<th colspan="3" bgcolor="#CCCCCC"><strong>LANGUAGE</strong></th>
</tr>
<tr>
<td colspan="2"><em>Currently no data exist! Please click on the following button to add language information.</em></td>
</tr>
<tr>
<td colspan="3" bgcolor="#fff">
<div style='margin:0 0 .5em 0;'>
<!-- when clicked, it will load the add user form -->
<div id='addLanguage' class='customBtn'>Add New</div>

<!-- when clicked, it will show the user's list -->
<div id='viewLanguages' class='customBtn'>View Existing</div>



<!-- this is the loader image, hidden at first -->
<div id='loaderImage'><img src='images/ajax-loader.gif' /></div>

<div style='clear:both;'></div>
</div>
<!-- this is wher the contents will be shown. -->
<div id='pageContent'></div>

<script src='js/jquery-1.9.1.min.js'></script>

<script type='text/javascript'>
$(document).ready(function(){

// VIEW USERS on load of the page
$('#loaderImage').show();
showUsers();

// clicking the 'VIEW USERS' button
$('#viewLanguages').click(function(){
// show a loader img
$('#loaderImage').show();

showUsers();
});

// clicking the '+ NEW USER' button
$('#addLanguage').click(function(){
showCreateUserForm();
});

// clicking the EDIT button
$(document).on('click', '.editBtn', function(){

var user_id = $(this).closest('td').find('.userId').text();
console.log(user_id);

// show a loader image
$('#loaderImage').show();

setTimeout("$('#pageContent').load('update_form.php?user_id=" + user_id + "', function(){ $('#loaderImage').hide(); });",1000);

});


// when clicking the DELETE button
$(document).on('click', '.deleteBtn', function(){
if(confirm('Are you sure?')){

// get the id
var user_id = $(this).closest('td').find('.userId').text();

// trigger the delete file
$.post("delete.php", { id: user_id })
.done(function(data) {
// you can see your console to verify if record was deleted
console.log(data);

$('#loaderImage').show();

// reload the list
showUsers();

});

}
});


// CREATE FORM IS SUBMITTED
$(document).on('submit', '#addLanguageForm', function() {

// show a loader img
$('#loaderImage').show();

// post the data from the form
$.post("create_lang.php", $(this).serialize())
.done(function(data) {
// 'data' is the text returned, you can do any conditions based on that
showUsers();
});

return false;
});

// UPDATE FORM IS SUBMITTED
$(document).on('submit', '#updateUserForm', function() {

// show a loader img
$('#loaderImage').show();

// post the data from the form
$.post("update.php", $(this).serialize())
.done(function(data) {
// 'data' is the text returned, you can do any conditions based on that
showUsers();
});

return false;
});

});

// READ USERS
function showUsers(){
setTimeout("$('#pageContent').load('read.php', function(){ $('#loaderImage').hide(); });", 1000);
}

// CREATE USER FORM
function showCreateUserForm(){
// show a loader image
$('#loaderImage').show();

setTimeout("$('#pageContent').load('create_form_lang.php', function(){ $('#loaderImage').hide(); });",1000);
}
</script>

</td>
</tr>
</table></td>
<tr><td height="26" colspan="3" align="center"><?php footer(); ?></td></tr>
</table>
</div>
</form>
</body>
</html>

megha_3000
06-20-2013, 04:53 AM
Please help.

traq
06-20-2013, 05:43 AM
Please help.

Biggest problem I see here is that you're asking how to fix code using PDO, but you're showing us code that doesn't use PDO.

Your question is unclear.
Please provide more information, and be as specific as possible.
What do you want to accomplish? What have you already tried? What problems did you encounter?
Please be sure that you have included all relevant code and/or a link to the page in question.
You might also consider making a reduced test case (http://css-tricks.com/reduced-test-cases/).

megha_3000
06-23-2013, 11:23 AM
i m trying to make it clear to you. i.e a couple of days ago i wrote a forum content at javascript How do i process the list and option field form element in CRUD with jQuery and PHP. Their accomplished a CRUD operation using PDO.


What do you want to accomplish?
i want to use that code with this form language_new1.php which is using a session.

What have you already tried?
Now i m trying to use that code with a form which is using a session. i examined that when i use session at the begining of that code then insertion doesn't happend. like in this code. where session is used at first.

What problems did you encounter?
insertion is not happening. as well as edit and delete also not occuring.

my sample codes are as folows:

queryfunctions.php

<?php
//session_start();
require_once("config.php");
error_reporting(E_ALL | E_STRICT);


function db_connect($HOST,$USER,$PASS,$DB)
{
$conn = mysql_connect($HOST, $USER, $PASS);
mysql_select_db($DB);
return $conn;
}

//close a connection
function db_close($conn)
{
mysql_close($conn);
}

function query($strsql,$conn)
{
$rs = mysql_query($strsql,$conn);
return $rs;
}

function num_rows($rs)
{
return @mysql_num_rows($rs);
}

function fetch_array($rs)
{
return mysql_fetch_array($rs);
}

function fetch_object($rs)
{
return mysql_fetch_object($rs);
}

function free_result($rs)
{
@mysql_free_result($rs);
}

function data_seek($rs,$cnt)
{
@mysql_data_seek($rs, $cnt);
}

function error()
{
return mysql_error();
}
?>
<?php
define("HOST", "hostname");
define("USER", "username");
define("PASS", "");
define("DB", "databasename");
/* define("smptserver",'smtpserver');
define("supportemail",'emailadress');
define("bcc",'emailaddress');
*/?>

traq
06-24-2013, 04:22 AM
so, no changes to the records in your database. Is there an error? Are your SQL statements being sent? Is your form submission being processed successfully? Is your form submission being submitted successfully?

When you say no data is being "inserted," are you talking about when you use this function to submit the form?
// CREATE FORM IS SUBMITTED
$(document).on('submit', '#addLanguageForm', function() {

// show a loader img
$('#loaderImage').show();

// post the data from the form
$.post("create_lang.php", $(this).serialize())
.done(function(data) {
// 'data' is the text returned, you can do any conditions based on that
showUsers();
});

return false;
});

In your .done() method, you can use the server's response to your form submission to debug: just add lots of messages about what the script is doing, and then echo them. It will come back to your script through the AJAX response.

// CREATE FORM IS SUBMITTED
$(document).on('submit', '#addLanguageForm', function() {

// show a loader img
$('#loaderImage').show();

// post the data from the form
$.post("create_lang.php", $(this).serialize())
.done(function(data) {

// change your PHP script to echo/print your debugging info
// (e.g., "parsed SQL statement: $sql"
// or "mysqli->query returned TRUE").
// error messages should come back in the response as well.
console.log( data );
// chrome has the console.log method; I don't know what other browsers support.
// you could even use alert(), if you want.

// 'data' is the text returned, you can do any conditions based on that
showUsers();
});

return false;
});

megha_3000
06-24-2013, 10:25 AM
Hello,

In your .done() method, you can use the server's response to your form submission to debug
yes i added that but didn't got any message. https://gist.github.com/megha3000/e9f6f98a5e7a57a08491
have a look at this

traq
06-24-2013, 02:04 PM
Right after you start your session, print "something" - that will let you know if you're even getting to the page:
<?php
//include database connection
session_start();

print "hello";

//include_once('includes/queryfunctions.php');
include 'libs/db_connect.php';

/* snip */

You could add a fail method in your js, too:
// CREATE FORM IS SUBMITTED
$(document).on('submit', '#addLanguageForm', function() {

// show a loader img
$('#loaderImage').show();

// post the data from the form
$.post("create_lang.php", $(this).serialize())
.done(function(data) {
// 'data' is the text returned, you can do any conditions based on that
showUsers();
})
.fail(function(jqXHR, textStatus, errorThrown) {
alert( errorThrown );
});

return false;
});

megha_3000
06-26-2013, 08:59 AM
Right after you start your session, print "something" - that will let you know if you're even getting to the page:
Tried as you stated but fails to print.

You could add a fail method in your js, too:
That also doesn't show anything.