Jump to content

error in inserting record

- - - - -

  • Please log in to reply
5 replies to this topic

#1
ravi951

ravi951

    Newbie

  • Members
  • PipPip
  • 18 posts
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
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
You appear to be opening a PHP tag (<?php) within a PHP script, and that is not a valid keyword.

You must close the tag before you open another one:
?>

<?php

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
ravi951

ravi951

    Newbie

  • Members
  • PipPip
  • 18 posts
here is the modified version. i remove <?php but also not executing.....

<?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
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Are there any errors in your error log?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
I don't see any <form> tag in your code...
The data won't be sended if it is not inside a form.

#6
yassinebelkaid

yassinebelkaid

    Newbie

  • Members
  • PipPip
  • 20 posts
I think it's better to make two pages, simple one to put your form in and give it action to e.g(insert.php) this page will treat your data and insert them into your database.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users