删除不删除所选帖子,但删除最近(newels)添加的帖子

时间:2015-11-01 12:38:09

标签: php html twitter-bootstrap

我的Update.php文件:

<?php ob_start();
 session_start();


//declare the basic variables
$servername = "localhost";
$username = "bilal";
$password = "super";
$dbname = "coursework";

 //create connection
 $link =  mysqli_connect($servername, $username, $password, $dbname);


//check connection
if($link->connect_error){
die ("connection failed: " . $link->connect_error);
}

//Security purpose, handiling escape string
  // $crested_by = $_POST ['created_by'];
  $title = $_POST['title'];
  $catagory = $_POST['catagory'];
  $contact = $_POST['contact'];
  $comment = $_POST ['description'];
   $ses = $_SESSION['email'];
  $date = date('Y-m-d');
  $availability = $_POST ['availability'];
  $price = $_POST ['price'];
  $id = $_POST['wow'];
 // $created_by_id = $_SESSION['created_by_id'];

 // $username = $_SESSION['firstname'];
 if (isset($_POST['del'])){
  $deletequery=mysqli_query($link,"DELETE FROM new_post WHERE id='$id'");
  header ("Location: added_posts.php");
   }

    else if(isset($_POST['update'])){

   $sql = "UPDATE new_post SET title='$title', contact='$contact',        availability='$availability', price='$price', comment='$comment' WHERE  id='$id'";
 if (mysqli_query($link, $sql)){
 echo "awesoke";
  header("Location:added_posts.php");
  }else{
   echo "Error: Sign up Unsuccessfull";
}  
}

$link->close();
 ?>

这是我的update.php文件,我将id内部显示为textbox,将id = the text field value显示在Traceback (most recent call last): File "C:\...\some_code.py", line 74, in <module> table_headers, table_data = fetch_relations() File "C:\...\some_code.py", line 27, in fetch_relations cur.execute(sql); File "C:\Python34\lib\site-packages\pypyodbc.py", line 1605, in execute self.execdirect(query_string) File "C:\Python34\lib\site-packages\pypyodbc.py", line 1631, in execdirect check_success(self, ret) File "C:\Python34\lib\site-packages\pypyodbc.py", line 986, in check_success ctrl_err(SQL_HANDLE_STMT, ODBC_obj.stmt_h, ret, ODBC_obj.ansi) File "C:\Python34\lib\site-packages\pypyodbc.py", line 966, in ctrl_err raise DatabaseError(state,err_text) pypyodbc.DatabaseError: ('07002', '[07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.') 。它正在删除,但最近添加的项目删除,如果我有3个帖子ID像(4,5,6)id删除6先然后5然后4.我在bootstrap卡显示数据。但是发布后的每个删除按钮都会删除最近添加的最近删除按钮。

1 个答案:

答案 0 :(得分:2)

您是否正在尝试删除数据库中的最新记录?

您的问题是 sql语句 您有

"DELETE FROM new_post WHERE id='$id'"

这是删除new_post中的记录,其中id等于从表单发布的ID。

您必须考虑表中的最新条目最大 ID 即可。

所以使用这样的东西:

DELETE FROM new_post ORDER BY id DESC LIMIT 1

尝试2:

您想根据ID?

删除特定行

我建议你使用 prepared statements 来提高安全性

首先尝试一下:

$stmt = $link->prepare("DELETE FROM new_post WHERE id= ?");
$stmt->bind_param('i', $_POST['wow']); // can also use $id 
$stmt->execute(); 
$stmt->close();