Results 1 to 8 of 8

Thread: error in inserting record

  1. #1
    Join Date
    Jul 2011
    Location
    hyderabad,India
    Posts
    58
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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
    $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());
    }
    }

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Fixed. You put in a <?php in the middle of your PHP execution, throwing an error.

    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);

    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());
    }
    }
    - Josh

  3. #3
    Join Date
    Jul 2011
    Location
    hyderabad,India
    Posts
    58
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default reply

    even now also it is displaying error.
    unable to insert values in textbox......

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by ravi951 View Post
    even now also it is displaying error.
    unable to insert values in textbox......
    your statement is unclear. Are you still receiving the same error? same error, different location? different error?

    ( saying "it doesn't work", without elaborating, is very, very un-useful. do your absolute best to figure out what's going on, and contribute to the solution, before throwing it back as "still not working." )

  5. #5
    Join Date
    Jul 2011
    Location
    hyderabad,India
    Posts
    58
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default reply

    it is already filled with some values in textbox even before we are about to enter like <? echo ?>
    i think error is still in the program........

  6. #6
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    The syntax of your program looks correct to me. What error are you getting? Copy+paste it.
    - Josh

  7. #7
    Join Date
    Jul 2011
    Location
    hyderabad,India
    Posts
    58
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default reply

    thank you i got it.

  8. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    please share your solution, so others can benefit from this as well

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
  •