ghost baby ghost baby - 1 month ago 12
PHP Question

data wont update in DB

i am having trouble updating data to table in DB i looked to see if i had any errors but everything looks fine nothing is wrong with my previous code i already tested it using old code that uses $_POST i know that is not recommended that's why im trying this new code please help



<?php
$con=mysqli_connect("localhost", "", "Password", "");

// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security

$xskids = mysqli_real_escape_string($con, $_POST['xskids']);
$skids = mysqli_real_escape_string($con, $_POST['skids']);

$sql="UPDATE shirt1_table SET xskids = '$xskids', skids = '$xskids' WHERE email = '$email'";

if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "record added";

mysqli_close($con);
?>





OLD CODE



<?php
require_once("configur.php");

$mysqli = new mysqli("localhost", "", "");

$query='UPDATE shirt1_table SET xskids="'.$_POST[xskids].'",skids="'.$_POST[skids].'"
WHERE email= "'.$_SESSION['email'].'"';

if ($mysqli->query($query) === TRUE) {


echo "success";

} else {
echo "Error updating record: " . $conn->error;
}

$mysqli->close();

?>




Answer

You did not set $email in your new code. Try to do it like this:

$xskids = mysqli_real_escape_string($con, $_POST['xskids']);
$skids = mysqli_real_escape_string($con, $_POST['skids']);
$email = mysqli_real_escape_string($con, $_SESSION['email']);

$sql="UPDATE shirt1_table SET xskids = '$xskids', skids = '$xskids' WHERE email = '$email'";
Comments