1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

error in inserting record

Discussion in 'PHP' started by ravi951, Aug 17, 2011.

  1. ravi951

    ravi951 New Member

    Joined:
    Aug 9, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    web developer
    Location:
    hyderabad
    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:
    <?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 a moderator: Aug 17, 2011
  2. Webdeveloper

    Webdeveloper New Member

    Joined:
    Jun 22, 2011
    Messages:
    28
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    IT Professional
    Location:
    New Delhi
    Home Page:
    Hi,

    This is because of the syntax error in the SQL query being generated. The best way to identify the error is to echo the query generated on the screen and you will be able to see the error.

    Hope this helps.

    Cheers,

    ~Maneet
     
  3. pein87

    pein87 New Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    0
    Occupation:
    Web Dev
    Location:
    Limbo
    I think you need to get the basics of php before you even continue on any further with this project. Your doing so many things wrong its not even funny. Try http://www.w3schools.com/php/default.asp and once you understand the basics come back and do this basic project. Your programming foundation needs to be sturdy before you can attempt to create programs in any language.
     

Share This Page