-1

I know mysql is oudated like yahoo. but, I was asked to fix on a friends website. so first of all, heres the code:

<?php
$name = $_POST['name'];
$kcid = $_POST['kcid'];

$host="localhost"; // Host name 
$username="username"; // Mysql username 
$password="password"; // Mysql password 
$db_name="dbname"; // Database name 
$tbl_name="tablename"; // 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 lastname='$name' WHERE KcID='$kcid'";
$result=mysql_query($sql);

// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
}

else {
echo "ERROR";
}

?>

When i try to run it, it says sucessful but it doesnt edit anything at all talbe:

KcID | Kc | Lastname

I hope somone has a solution.

please correct my code if you see something that causes it not to work

Php Gecko
  • 29
  • 4

3 Answers3

0

try to type like this:

$sql="UPDATE {$tbl_name} SET lastname='{$name}' WHERE KcID='{$kcid}'";

without the brackets, it cant understand that '$name' is a php variable.

Yair.R
  • 795
  • 4
  • 11
0

Some updates I can suggest to make your code a lot better and safer:

<?php
    $name = $_POST['name'];
    $kcid = $_POST['kcid'];

    $host="localhost"; // Host name 
    $username="username"; // Mysql username 
    $password="password"; // Mysql password 
    $db_name="dbname"; // Database name 
    $tbl_name="tablename"; // Table name 

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

    // update data in mysql database 
    $sql=sprintf("UPDATE %s SET lastname='%s' WHERE KcID='%s'", $tbl_name, mysql_real_escape_string($name), mysql_real_escape_string($kcid));
    mysql_query($sql, $connection);

?>

I am not sure, but I think the problem lies with your assigning the query in a result rather than actually querying. Try this solution and let me know if this workds.

0

Try This Here Code contains Mysqli Query rather than MySQL which is the improved extension of MYSQL

    <?php
$name = $_POST['name'];
$kcid = $_POST['kcid'];

$host="localhost"; // Host name 
$username="username"; // Mysql username 
$password="password"; // Mysql password 
$db_name="dbname"; // Database name 
$tbl_name="tablename"; // Table name 

// Connect to server and select database.
$con = mysqli_connect("$host", "$username", "$password","$db_name")or die("cannot connect"); 


// update data in mysql database 
$sql="UPDATE $tbl_name SET lastname='$name' WHERE KcID='$kcid'";
$result=mysqli_query($con,$sql);

// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
}
else {
echo "ERROR";
}

?>