我无法将输入的数据更新为db

时间:2013-05-13 06:32:46

标签: php mysqli

这是我的代码

  <?php
  include('admin/class.php');

这是数据库连接

  $link = mysqli_connect("localhost", "root", "", "timesheet1234");

这里正在对我的保存按钮进行操作

if(isset($_POST['save']))
{  
  $sel=@$_POST['selpro'];
  $mon=@$_POST['mon'];
  $tue=@$_POST['tue'];
  $wed=@$_POST['wed'];
  $thu=@$_POST['thu'];
  $fri=@$_POST['fri'];
  $sat=@$_POST['sat'];
  $sun=@$_POST['sun'];

这是问题发生的地方

if(isset($_SESSION['user']))
{
    echo "session user";

不接受mysqli

$stmt = mysqli_prepare($link,"UPDATE empdaytimesheet SET 
`projectcode`='$sel',`mon`='$mon',`tue`='$tue',`wed`='$wed',
`thu`='$thu',`fri`='$fri',`sat`='$sat',`sun`='$sun' where
`username`='".$_SESSION['user']."'");
mysqli_stmt_bind_param($stmt,"siiiiiii", $sel,$mon,$tue,$wed,$thu,$fri,$sat,$sun);

$res= mysqli_stmt_execute($stmt);

没有达到这个条件

if($res){
  echo "<script type='text/javascript'>";
  echo "alert('TimeSheet Saved..!')";
  echo "</script>";
  echo "<script type='text/javascript'>";
  echo "window.location='my_timesheet.php'";
  echo "</script>";
}

相反,它会执行此操作:

else {
  echo "<script type='text/javascript'>";
  echo "alert('Some Error Occured ! Retry..!')";
  echo "</script>";
  echo "<script type='text/javascript'>";
  echo "window.location='my_timesheet.php'";
  echo "</script>";
}
}
  }
  ?>

3 个答案:

答案 0 :(得分:0)

尝试(使用占位符(?)而不是直接变量)

$stmt = "UPDATE empdaytimesheet SET `projectcode`=?,
                                    `mon`=?,
                                    `tue`=?,
                                    `wed`=?,
                                    `thu`=?,
                                    `fri`=?,
                                    `sat`=?,
                                    `sun`=? 
        WHERE username = ?";

$stmt = mysqli_prepare($link, $stmt);
// s : corresponding variable has type string
mysqli_stmt_bind_param($stmt, 'sssssssss', $sel, $mon, $tue, $wed,$thu,$fri,$sat,$sun,$username);

$sel = 'whatevr';
$mon = 'whatever';
//...
//...so on ...

$stmt->execute();

Reference

答案 1 :(得分:0)

您使用预准备语句的方式是错误的。

您必须使用占位符而不是变量:

此外,您没有检查错误,因此您无法从数据库中获取提示

$sql  = "UPDATE empdaytimesheet SET `projectcode`=?',`mon`=? ..."; // and so on
$stmt = mysqli_prepare($link, $sql) or trigger_error(mysqli_error($link));

注意第二行。它会引发PHP错误,告诉你出现了什么问题

答案 2 :(得分:-1)

通常绑定语句意味着您将变量放在占位符?的位置 在准备语句时,您永远不会放置实际变量。

$stmt = "UPDATE empdaytimesheet SET `projectcode`=?,
                                `mon`=?,
                                `tue`=?,
                                `wed`=?,
                                `thu`=?,
                                `fri`=?,
                                `sat`=?,
                                `sun`=? 
    WHERE username = ?";

当你编写上面的语句时,你实际上是向mysql表明你要执行这个表单的查询......

并使用

绑定查询时
 mysqli_stmt_bind_param($stmt, 'ssssssss', $sel, $mon, $tue,    $wed,$thu,$fri,$sat,$sun,$username);

您要在?

上提供要替换的实际值

您收到错误的原因是因为您的陈述未正确准备,因此mysqli_prepare正在返回false

尝试使用mysqli_prepare() or die(mysqli_error())

捕获错误

查看更多here