数据不会在DB中更新

时间:2016-10-18 11:50:46

标签: php mysqli

我在将数据更新到DB中的表时遇到问题我想看看我是否有任何错误但是一切看起来都很好我以前的代码没有任何问题我已经使用使用$ _POST的旧代码测试了我知道不推荐这就是为什么我尝试这个新代码请帮助

<?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);
?> 

旧代码

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

1 个答案:

答案 0 :(得分:1)

您未在新代码中设置$email。试着这样做:

$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'";