Dear all, i am new to php and i am trying to update db table through php form ...

there is a form that takes inputs from php form and then update the db ...

Below is the update php page ...

<?php
$host="localhost"; // Host name 
$username="lpr"; // Mysql username 
$password="njeriop123"; // Mysql password 
$db_name="give_test"; // Database name 
$tbl_name="test_mysql"; // Table name 

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

// get value of id that sent from address bar
$id=$_GET['id'];

// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="ndrysho_akcioni.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center">
<input name="name" type="text" id="name" value="<? echo $rows['targa']; ?>">
</td>
<td align="center">
<input name="lastname" type="text" id="lastname" value="<? echo $rows['shteti']; ?>" size="15">
</td>
<td>
<input name="email" type="text" id="email" value="<? echo $rows['pershkrimi']; ?>" size="15">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?php
// close connection 
mysql_close();
?>

And here is update_action or in my lang ndrysho_akcioni.php ...

<?php
$host="localhost"; // Host name 
$username="lpr"; // Mysql username 
$password="njeriop123"; // Mysql password 
$db_name="give_test"; // Database name 
$tbl_name="test_mysql"; // Table name 

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

// update data in mysql database 
$sql="UPDATE $tbl_name SET targa='$targa', shteti='$shteti', pershkrimi='$pershkrimi' WHERE id='$id'";
$result=mysql_query($sql);

// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='listo.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

I dont know what the problem might be but it says succsesfully updated but it doesnt update anything ... If anybody could help me i would really appreciate it ... Thanks

Recommended Answers

All 3 Replies

Member Avatar for diafol

Try this:

$sql="UPDATE $tbl_name SET targa='$targa', shteti='$shteti', pershkrimi='$pershkrimi' WHERE id='$id'";
echo $sql;
$result=mysql_query($sql);
if(mysql_affected_rows()){
    echo "Successful";
    echo "<BR>";
    echo "<a href='listo.php'>View result</a>";
}else {
    echo "ERROR";
}

But just so that you know, you haven't shown where variables like $targa ar set. Have they been taken from something like:

$targa = mysql_real_escape_string($_POST['targa']);

?

first i dont see where you grab the data from your post form for the update.

ex: $name = $_POST['name']; // and so on
or $sql="UPDATE $tbl_name SET targa='$_POST['targa']' ...

i personnally prefer to set local variables
im not even sure you can put a post data in a query, cause of the ' even with \' never done it.

and second how do you pass the variables to listo.php so it can echo it ?

anyways my 2 cents

maybe some guru have a better answer

Dark

LOL

thats what happen when you let the writting screen open too long, someone else post before me :P

anyways, the answer is pretty much the same,

so have fun coding

Dark

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.