SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    error in inserting record

    hi all i have been inserting records in to database using php using the below program which is saved as "insert.php".
    but it is displaying the following error
    Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\insert1.php on line 18

    dont no what went wrong.
    can u tell me how to solve it.....
    PHP Code:
    <?php
    $host
    ="localhost"// Host name 
    $username="root"// Mysql username 
    $password=""// Mysql password 
    $db_name="test"// Database name 
    $tbl_name="emp"// Table name

    // Connect to server and select databse.
    mysql_connect("$host""$username""$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");

    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);

    // Count table rows 
    $count=mysql_num_rows($result);

    <?
    php
    while($rows=mysql_fetch_array($result))
    {
    ?>
    <tr>
    <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="y" /></td>
    <td align="center"><input name="empno[]" type="text" id="name" value="<? echo $rows['empno'];?>"></td>
    <td align="center"><input name="empname[]" type="text" id="empname" value="<? echo $rows['empname'];?>"></td>
    <td align="center"><input name="desig[]" type="text" id="desig" value="<? echo $rows['desig'];?>"></td>
    </tr>

    <?php
    }
    ?>
    <input type="submit" name="Submit" value="Submit">

    <?php

    // Get values from form 
    $no=$_POST['empno'];
    $name=$_POST['empname'];
    $desig=$_POST['desig'];

    // Check if button name "Submit" is active, do this 
    if(array_key_exists('Submit'$_POST))
    {
    for(
    $i=0;$i<count($count);$i++)
    {
         
    //protect form sql injection
        
    $a = (int) $_POST['empno'][$i]; 
        
    $b mysql_real_escape_string$_POST['empname'][$i] ); 
        
    $c mysql_real_escape_string$_POST['desig'][$i] ); 
     
    //read the query
     
    $sql="INSERT INTO '$tbl_name' (empno, empname, desig) VALUES('{$a}', '{$b}', '{$c}')";
     
    mysql_query($sql) or die(mysql_error());
    }
    }
    Last edited by SpacePhoenix; Aug 16, 2011 at 22:52. Reason: placed php tags around the php code

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You have an opening PHP tag where it shouldn't be, find the following code

    PHP Code:
    <?php
    while($rows=mysql_fetch_array($result))
    and replace it with

    PHP Code:
    while($rows=mysql_fetch_array($result)) 

  3. #3
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have modified the code like u said above but it is also not executing......
    below is the modified code.......
    Code:
    <?php
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    $db_name="test"; // Database name 
    $tbl_name="emp"; // Table name
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);
    
    // Count table rows 
    $count=mysql_num_rows($result);
    while($rows=mysql_fetch_array($result))
    {
    ?>
    <tr>
    <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="y" /></td>
    <td align="center"><input name="empno[]" type="text" id="name" value="<? echo $rows['empno'];?>"></td>
    <td align="center"><input name="empname[]" type="text" id="empname" value="<? echo $rows['empname'];?>"></td>
    <td align="center"><input name="desig[]" type="text" id="desig" value="<? echo $rows['desig'];?>"></td>
    </tr>
    
    <?php
    }
    ?>
    <input type="submit" name="Submit" value="Submit">
    
    <?php
    
    // Get values from form 
    $no=$_POST['empno'];
    $name=$_POST['empname'];
    $desig=$_POST['desig'];
    
    // Check if button name "Submit" is active, do this 
    if(array_key_exists('Submit', $_POST))
    {
    for($i=0;$i<count($count);$i++)
    {
         //protect form sql injection
        $a = (int) $_POST['empno'][$i]; 
        $b = mysql_real_escape_string( $_POST['empname'][$i] ); 
        $c = mysql_real_escape_string( $_POST['desig'][$i] ); 
     //read the query
     $sql="INSERT INTO '$tbl_name' (empno, empname, desig) VALUES('{$a}', '{$b}', '{$c}')";
     mysql_query($sql) or die(mysql_error());
    }
    }

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2010
    Location
    Netherlands
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ravi951 View Post
    i have modified the code like u said above but it is also not executing......
    The syntax seems OK now, what's the error that you're getting?

  5. #5
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my database name is "test".my table name is "emp".
    emp table contains 3 fields as shown below....

    empno empname desig

    1111 raju pilot
    1112 ram chef
    1113 ramu doctor
    1114 paul engineer
    1115 ajay player

    now i need to add values in to database using php.after entering values for 3 fields and click on the submit button it should be updated on the table....
    tell me how to do it...i dont need check box for inserting the values...
    below is the code.......
    what to modify in the below code for getting the above requirements.....

    Code:
    <?php
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    $db_name="test"; // Database name 
    $tbl_name="emp"; // Table name
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);
    
    // Count table rows 
    $count=mysql_num_rows($result);
    while($rows=mysql_fetch_array($result))
    {
    ?>
    <tr>
    <td align="center" bgcolor="#FFFFFF"></td>
    <td align="center"><input name="empno[]" type="text" id="name" value="<? echo $rows['empno'];?>"></td>
    <td align="center"><input name="empname[]" type="text" id="empname" value="<? echo $rows['empname'];?>"></td>
    <td align="center"><input name="desig[]" type="text" id="desig" value="<? echo $rows['desig'];?>"></td>
    </tr>
    
    <?php
    }
    ?>
    <input type="submit" name="Submit" value="Submit">
    
    <?php
    
    // Get values from form 
    $no=$_POST['empno'];
    $name=$_POST['empname'];
    $desig=$_POST['desig'];
    
    // Check if button name "Submit" is active, do this 
    if(array_key_exists('Submit', $_POST))
    {
    for($i=0;$i<count($count);$i++)
    {
         //protect form sql injection
        $a = (int) $_POST['empno'][$i]; 
        $b = mysql_real_escape_string( $_POST['empname'][$i] ); 
        $c = mysql_real_escape_string( $_POST['desig'][$i] ); 
     //read the query
     $sql="INSERT INTO '$tbl_name' (empno, empname, desig) VALUES('{$a}', '{$b}', '{$c}')";
     mysql_query($sql) or die(mysql_error());
    }
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •