/    Sign up×
Community /Pin to ProfileBookmark

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
[B]Parse error: syntax error, unexpected ‘<‘ in C:xampphtdocsinsert1.php on line 18
[/B]

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

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@ryanlundAug 17.2011 — The problem is the fact that you have opening/closing php tags inside your php code. (before and after where you are starting your while loop).
Copy linkTweet thisAlerts:
@ravi951authorAug 17.2011 — actually 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

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());
}
}
[/CODE]
×

Success!

Help @ravi951 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...