Solve my Database Insert error?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sharmahemal
    New Member
    • Jul 2012
    • 19

    Solve my Database Insert error?

    Code:
    if(isset($_REQUEST['submit']))
    {
    date_default_timezone_set("Asia/Calcutta");
    $date = date('d/m/Y');
    $in=$_POST['in'];
    $out=$_POST['out'];
    $project1=$_POST['project1'];
    $hrs1=$_POST['hrs1'];
    $project2=$_POST['project2'];
    $hrs2=$_POST['hrs2'];
    $otherremark=$_POST['otherremark'];
    $hrs3=$_POST['hrs3'];
    $total=$_POST['total'];
    
    
    $result=mysql_query("INSERT into user_tasksheet(today_date,in,out,project1,hrs1,project2,hrs2,otherremark,total) values('".$date."','$in','$out','$project1','$hrs1','$project2','$hrs2','$otherremark','$hrs3',$total)") or die(mysql_error());
    if(mysql_num_rows()==1)
    {
    echo "insert data";	
    }
    else
    {
    	
    echo "not insert data";	
    }
    	
    }
    else
    {
    	return false;
    	}
    
    ?>
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'in,out,project 1,hrs1,project2 ,hrs2,otherrema rk,total) values('19/08/2012','','',' at line 1
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #2
    You have 9 fields (columns) and 10 values.

    I also noted that your variable values after the date were submitted as constants, in that you did not concatenate the variables with the .

    Just a suggestion, if your database is MySQL you may want to use this alternate syntax of name/value pairs. I think it is easier to match up the field with the value like this

    INSERT INTO tbl_name SET col_name1=expr1 , col_name2=expr2 , ...

    So your code would look something like this.

    $result=mysql_q uery("INSERT into user_tasksheet set today_date = '".$date."', in=".$in.", out=".$out.',.. .

    Comment

    • sharmahemal
      New Member
      • Jul 2012
      • 19

      #3
      Thnk You very much

      thnks for suggestion

      Comment

      • sharmahemal
        New Member
        • Jul 2012
        • 19

        #4
        You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'in='4', out='2', project1='ygb', hrs1='2', project2='grtg' , hrs2='3', othe' at line 3
        Code:
        <?php include('conn.php')?>
        <?php
        if(isset($_REQUEST['submit']))
        {
        date_default_timezone_set("Asia/Calcutta");
        $date = date('d/m/Y');
        $in=$_POST['in'];
        $out=$_POST['out'];
        $project1=$_POST['project1'];
        $hrs1=$_POST['hrs1'];
        $project2=$_POST['project2'];
        $hrs2=$_POST['hrs2'];
        $other_remarks=$_POST['other_remark'];
        $hrs3=$_POST['hrs3'];
        $total=$_POST['total'];
        
        $query="INSERT into user_tasksheet SET 
        today_date='".$date."',
        in='".$in."',
        out='".$out."',
        project1='".$project1."',
        hrs1='".$hrs1."',
        project2='".$project2."',
        hrs2='".$hrs2."',
        other_remark='".$other_remarks."',
        hrs3='".$hrs3."',
        total='".$total."'";
        
        $result=mysql_query($query) or die(mysql_error());
        if($result)	
        {
        echo "insert data";	
        }
        else
        {
        	
        echo "not insert data";	
        }
        	
        }
        else
        {
        	return false;
        }
        
        ?>

        Comment

        Working...