Page 1 of 2

Missing mysql Insert Column Entry

Posted: Mon Jul 27, 2009 6:38 pm
by tobimichigan
Hi Gurus,
Please code gurus, I need a sharp pointer to the php code below; whenever I click submit button of the html form, it inserts all data except for 'loanrepaid', 'Year', 'Session'. I've double checked the variable names on the html-form with the insert variables and also checked by using 'addslahes', or 'mysql_real_escape_string' but all to no avail as 'loanrepaid', 'Year', 'Session' colums are not inserted into the table.


Here's the table structure:

-- Table structure for table `yearly_reports`
--

CREATE TABLE `yearly_reports` (
`id` int(11) NOT NULL auto_increment,
`name` char(10) NOT NULL,
`department` char(6) NOT NULL,
`pfno` char(10) NOT NULL,
`savings` varchar(255) NOT NULL,
`shares` varchar(255) NOT NULL,
`outloanbal` varchar(255) NOT NULL,
`loanrepaid` varchar(255) NOT NULL,
`loanint` varchar(255) NOT NULL,
`essenco` text NOT NULL,
`month` text NOT NULL,
`Year` varchar(234) NOT NULL,
`date` date NOT NULL,
`session` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

Code: Select all

//Here's the Html-form
 
<body>
<?php echo("<p align='center'><img src='/LasustaffCams/images/LasustaffCoop2.jpg' alt='' name='LasuStaffCams_Logo' width='700' height='150' id='LasuStaffCams_Logo' /></p>");?>
<?php echo ("<p>Admin | Admin Post | Summary Post | Year Summary | Logout</p>");?>
<?php echo ("<form action='Yearly_Entry_Action.php' method='post' enctype='multipart/form-data' name='register'>
  <table width='28%' border='0' align='center' cellpadding='0' cellspacing='0'>
    <tr>
      <td colspan='2'><div align='center'>
        <p><font size='2' face='verdana'>ADMIN_YEARLY_POST</font></p>
        </div></td>
    </tr>
    <tr>
      <td width='31%'>&nbsp;</td>
      <td width='69%'>&nbsp;</td>
    </tr>
    <tr>
      <td colspan='2'><hr></td>
    </tr>
    <tr>
      <td height='26'><font size='2' face='verdana'>MEMBER NAME</font></td>
      <td><font size='2' face='verdana'>
        <input type='text' name='name'>
        </font></td>
    </tr>
    <tr>
      <td height='28'><font size='2'>DEPARTMENT</font></td>
      <td><font size='2' face='verdana'>
        <input type='text' name='department'>
        </font></td>
    </tr>
    <tr>
      <td height='25'><font size='2'>PF. NO.</font> </td>
      <td><font size='2' face='verdana'>
        <input type='text' name='pfno'>
        </font></td>
    </tr>
   <tr>
      <td height='25'><font size='2'>SAVINGS</font><font size='2' face='verdana'></td>
      <td><font size='2' face='verdana'>
        <input type='text' name='savings'>
        </font></td>
    </tr>
    <tr>
      <td height='25'><font size='2' face='verdana'>SHARES</font></td>
      <td><font size='2' face='verdana'>
        <input type='text' name='shares'>
        </font></td>
    </tr>
    <tr>
      <td height='25'><font size='2' face='verdana'>OUTSTANDING LOAN BAL.</font></td>
      <td><font size='2' face='verdana'>
        <input type='text' name='outloanbal'>
        </font></td>
    </tr>
    <tr>
      <td height='25'><font size='2' face='verdana'>LOAN REPAID IN YEAR</font></td>
      <td><font size='2' face='verdana'>
        <input type='text' name='loan_repaid'>
        </font></td>
    </tr>
    <tr>
      <td height='25'><font size='2' face='verdana'>LOAN INTEREST</font></td>
      <td><font size='2' face='verdana'>
        <input type='text' name='loanint'>
        </font></td>
    </tr>
    <tr>
      <td height='25'><font size='2' face='verdana'>ESSENCO</font></td>
      <td><font size='2' face='verdana'>
        <input type='text' name='essenco'>
        </font></td>
    </tr>
    <tr>
      <td height='25'><font size='2' face='verdana'>MONTH</font></td>
      <td><font size='2' face='verdana'>
        <input type='text' name='month'>
        </font></td>
    </tr>
    <tr>
      <td height='25'><font size='2' face='verdana'>YEAR</font></td>
      <td><font size='2' face='verdana'>
        <input type='text' name='year'>
        </font></td>
    </tr>
   <tr>
      <td height='25'><font size='2' face='verdana'></font></td>
      <td><font size='2' face='verdana'>
        <input type='hidden' name='date'>
        </font></td>
    </tr>
   <tr>
      <td height='25'><font size='2' face='verdana'></font></td>
      <td><font size='2' face='verdana'>
        <input type='hidden' name='session_id'>
        </font></td>
    </tr>
    </tr>   
      <td>&nbsp;</td>
      <td><font size='2' face='verdana'>
        <input type='submit' name='Submit' value='Record Yearly Report'>
        </font></td>
    </tr><tr>
      <td colspan='2'><hr></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form><div align='center'></div>
<td colspan='2'><div align='center'><font size='1' face='verdana'>TM</font></div></td>
<td colspan='2'>&nbsp;</td>");
?>
 
//Here's the php action script&#058;
 
<?php
include("cn.php");
 
 
$date= date("now"); 
$department = addslashes($_POST['department']);
$essenco=addslashes($_POST['essenco']);
$loanint =addslashes($_POST['loanint']);
$loan_repaid =addslashes($_POST['loanrepaid']);
$month=addslashes($_POST['month']);
$name=addslashes($_POST['name']);
$outloanbal=addslashes($_POST['outloanbal']);
$pfno=addslashes($_POST['pfno']);
$savings=addslashes($_POST['savings']);
$session=addslashes($_POST['session']);
$shares=addslashes($_POST['shares']);
$year=addslashes($_POST['Year']);       
            
 
$sql= "Insert into yearly_reports(date, department,essenco,loanint,loanrepaid,month,name,outloanbal,pfno,savings,session,shares,Year) values('now','$department','$essenco','$loanint','$loan_repaid','$month','$name','$outloanbal','$pfno','$savings','','$shares','$year')" or die .mysql_error();  
 
//$result=mysql_query($sql) or die .mysql_error();                                  
if (!mysql_query($sql))
 {
  die('Error: ' . mysql_error()."  SQL: ".$sql);
  }
echo ("1 record added");
?>
                                 
                                                         
 
<html>
<body>
 
</body>
</html>
 
Any sharp pointer would be highly appreciated.

Re: Missing mysql Insert Column Entry

Posted: Mon Jul 27, 2009 6:49 pm
by requinix
Have you checked the form to make sure the fields are there? Did you make sure they're spelled correctly? Have you accounted for the fact that field names are case-sensitive?

Re: Missing mysql Insert Column Entry

Posted: Mon Jul 27, 2009 6:53 pm
by jackpf
You're getting data from loanrepaid when it's called loan_repaid. Cba to check the others - it's probably similiar mistakes.

var_dump() each variable to see if it's what you expect.

Re: Missing mysql Insert Column Entry

Posted: Mon Jul 27, 2009 7:14 pm
by tobimichigan
jackpf wrote:You're getting data from loanrepaid when it's called loan_repaid. Cba to check the others - it's probably similiar mistakes.

var_dump() each variable to see if it's what you expect.
Sir jack, I'm using loan_repaid as the name in the html-form, this applies also to the $loan_repaid variable in the post values. Then in the insert, the sql name is loanrepaid. Thus variable name $loan_repaid is supposed to insert into sql column name loanrepaid. Please could you run through it on your own?
Why is it not inserting?

Re: Missing mysql Insert Column Entry

Posted: Mon Jul 27, 2009 8:08 pm
by jackpf
No, look - read your own code :P

You're putting name="loan_repaid" and $_POST['loanrepaid']. Can you not notice the difference?

I did run it on my own pc. That's how I noticed.

Re: Missing mysql Insert Column Entry

Posted: Tue Jul 28, 2009 5:24 am
by tobimichigan
Ok Sir Jack, u win.. You're right! That did almost slip me by. One more thing though, suppose I wanted to insert session number value like:

Code: Select all

<?php 
 
session_start();
mt_srand((double)microtime()*1000000
$session=mt_rand();//create uniqiue sessionid
Along side other variables. How do I do that?

Thanks a bunch.

Re: Missing mysql Insert Column Entry

Posted: Tue Jul 28, 2009 5:49 am
by jackpf
Sorry...I don't quite understand what you mean by "alongside other variables".

Re: Missing mysql Insert Column Entry

Posted: Tue Jul 28, 2009 6:07 am
by tobimichigan
jackpf wrote:Sorry...I don't quite understand what you mean by "alongside other variables".


Here's what I mean,

Code: Select all

<?php
 
session_start();
 
include("cn.php");
 
mt_srand((double)microtime()*1000000
$session=mt_srand();//create uniqiue sessionid
 
$date = mysql_real_escape_string($_POST['date']);
$email=mysql_real_escape_string($_POST['email']);
$firstname=mysql_real_escape_string($_POST['firstname']);
$lastname =mysql_real_escape_string($_POST['lastname']);
$login =mysql_real_escape_string($_POST['login']);
$oname=mysql_real_escape_string($_POST['oname']);
$password=mysql_real_escape_string($_POST['password']);
$session=mysql_real_escape_string($_POST['session']);
$sex=mysql_real_escape_string($_POST['sex']);
$telephone=mysql_real_escape_string($_POST['telephone']);
$user_level=mysql_real_escape_string($_POST['user_level']);
 
 
        
            
 
$sql= "Insert into admin_table(date,email,firstname,lastname,login,oname,password,session,sex,telephone,user_level) values(SYSDATE(),'$email','$firstname','$lastname','$login','$oname','$password','session','$sex','$telephone','$user_level')";  
                                    
if (!mysql_query($sql))
 {
  die('Error: ' . mysql_error()."  SQL: ".$sql);
  }
echo ("1 record added");
?>
                                 
                                                         
 
<html>
<body>
 
</body>
</html>
After initializing the session start (); then I generate a unique session id with

mt_srand((double)microtime()*1000000

Then equating the $session variable to the random value mt_rand

$session=mt_rand();//create uniqiue sessionid

In the post values, I want to insert $session as a value of the equation into the sql column. Each time I try this it says

Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\LasustaffCams\Admin\Admin.Register_Action.php on line 8

where line 8 is the
$session=mt_srand();//create uniqiue sessionid

How do I go about this?

Re: Missing mysql Insert Column Entry

Posted: Tue Jul 28, 2009 6:13 am
by jackpf
1. You're missing a bracket.
2. You're missing a semi colon after the bracket
3. You're not assigning the random value to any variable

Look at the code...does it look right to you? :P

Re: Missing mysql Insert Column Entry

Posted: Tue Jul 28, 2009 6:43 am
by tobimichigan
jackpf wrote:1. You're missing a bracket.
2. You're missing a semi colon after the bracket
3. You're not assigning the random value to any variable

Look at the code...does it look right to you? :P
Here's my modified code:

Code: Select all

<?php
session_start();
 
include("cn.php");
 
mt_srand((double)microtime())*1000000;
$session=mt_srand();//create uniqiue sessionid
?>

The code is now corrected but it now returns a "0" value in the sql table instead of the generated random variable, why?

Re: Missing mysql Insert Column Entry

Posted: Tue Jul 28, 2009 6:55 am
by jackpf
You're insterting 'session', not '$session'. Since it's an integer value, it's probably being converted to 0. I'm guessing you have an old version of mysql, because a newer version would just throw an error.

You really need to learn to read your own code and debug it. I'm not trying to be mean...but these are simple errors that I've spotted within a few seconds of reading your code. I honestly believe you could spot them too if you just paused and read your code for a couple of minutes to see what was wrong.

Re: Missing mysql Insert Column Entry

Posted: Tue Jul 28, 2009 3:52 pm
by tobimichigan
jackpf wrote:You're insterting 'session', not '$session'. Since it's an integer value, it's probably being converted to 0. I'm guessing you have an old version of mysql, because a newer version would just throw an error.

You really need to learn to read your own code and debug it. I'm not trying to be mean...but these are simple errors that I've spotted within a few seconds of reading your code. I honestly believe you could spot them too if you just paused and read your code for a couple of minutes to see what was wrong.
Sir Jack, don't forget u're the guru and I'm the newbie. A rookie 4 that matter. Could you please tell me further on what to do practically so the return value would not return 0 but the exact session id into the databse?

Re: Missing mysql Insert Column Entry

Posted: Wed Jul 29, 2009 6:22 am
by jackpf
I'm not a guru! I'm 16 years old and I've only been coding for about a year :P

Well, you've assigned the value of mt_rand() to $session, so just insert '$session' into the database. Is that not working?

Re: Missing mysql Insert Column Entry

Posted: Wed Jul 29, 2009 6:53 pm
by tobimichigan
jackpf wrote:I'm not a guru! I'm 16 years old and I've only been coding for about a year :P

Well, you've assigned the value of mt_rand() to $session, so just insert '$session' into the database. Is that not working?
Its still giving a 0 value in the table, here's my corrected code:

Code: Select all

<?php
 
session_start();
 
include("cn.php");
 
mt_srand((double)microtime())*1000000;
$session=mt_srand();//create uniqiue sessionid
 
$date = mysql_real_escape_string($_POST['date']);
$email=mysql_real_escape_string($_POST['email']);
$firstname=mysql_real_escape_string($_POST['firstname']);
$lastname =mysql_real_escape_string($_POST['lastname']);
$login =mysql_real_escape_string($_POST['login']);
$oname=mysql_real_escape_string($_POST['oname']);
$password=mysql_real_escape_string($_POST['password']);
$session=mysql_real_escape_string($_POST['session']);
$sex=mysql_real_escape_string($_POST['sex']);
$telephone=mysql_real_escape_string($_POST['telephone']);
$user_level=mysql_real_escape_string($_POST['user_level']);
 
 
        
            
 
$sql= "Insert into admin_table(date,email,firstname,lastname,login,oname,password,session,sex,telephone,user_level) values(SYSDATE(),'$email','$firstname','$lastname','$login','$oname','$password','$session','$sex','$telephone','$user_level')";  
                                    
if (!mysql_query($sql))
 {
  die('Error: ' . mysql_error()."  SQL: ".$sql);
  }
echo ("1 record added");
?>
                                 
                                                         
 
<html>
<body>
 
</body>
</html>
Do you have any pointers as to why this might be so Sir Jack?

Re: Missing mysql Insert Column Entry

Posted: Thu Jul 30, 2009 6:46 am
by jackpf
Try putting

Code: Select all

var_dump($session); die();
just before the query.

Also try inserting a number. As in 10 or something, and see if that works.

Also, this line "mt_srand((double)microtime())*1000000;" is completely useless. You're not assigning it to anything.