mysql - Trying to add a delete button to PHP website -
i'm new php , trying add delete button remove object (job) list, want delete button appear beside each of individual objects (jobs) , once clicked job gets deleted database table. below code both edit_jobs.php (displays jobs particular user) , delete_job.php (suppose remove particular job table) can please tell me i'm doing wrong,
my edit_jobs page displays jobs in table particular user has posted.
<?php include_once "connect_to_mysql.php"; $id = $userid; $username = $_get['username']; $result = mysql_query("select * jobs user_id ='$id'") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { echo '<a href="job.php?id=' . $row['job_id'] . '"> ' . $row['job'] . '</a><br />'; echo 'category: ' . $row['category'] . '<br />'; echo 'description: ' . $row['description'] . '<br />'; echo '<a href="member.php?id=' . $row['userid'] . '">clients profile</a><br />'; echo '<br />';?> <a href="delete_job.php?job_id=<?php echo $row['job']; ?>" onclick="return confirm('are sure want delete book?');"> <img src="images/delete20.png" alt="delete book" /> </a> <?php } ?>
the delete_job page
<?php if ($_server['request_method'] == 'get') { if (!empty($_get['job_id'])) { $jobid = $_get['job_id']; require_once 'connect_to_mysql.php'; $sql = "delete jobs job_id = ?"; $params = array($jobid); $stmt = $link->prepare($sql); $status = $stmt->execute($params); if ($status == true) { header("location: edit_jobs.php"); } else { $error_info = $stmt->errorinfo(); $error_message = "failed delete job: {$error_info[2]} - error code {$error_info[0]}"; require 'error.php'; } } else { $error_message = "book id not specified"; require 'edit_jobs.php'; } } else { } ?>
i see lot of problems code. number 1 - agree above vulnerabilities. easy fix; validate job id integer , don't execute sql code if fails validation.
number 2 - think $param = array($jobid) incorrect. you're not passing job variable sql code @ all... it's providing null value execution statement. cleaned code, can't guarantee work can't see sql statements, better approach , should work right...
# include before else... require_once 'connect_to_mysql.php'; # ditch server validation check, waste of load time, store job id in variable off bat $jobid = $_get['job_id']; # validate if job is numeric , not empty if ((!empty($jobid)) || (is_numeric($jobid)) { # ditch $sql variable speed/memory, include in prepare statement # note limit statement - it's practice limit deletion queries 1 row # if deleting 1 row additional data doesn't accidentally deleted $stmt = $link->prepare("delete jobs job_id = ? limit 1"); # code prepares job id parameterized query , tells database parse int $stmt->bind_param('i', $jobid); # execute , validate $status = $stmt->execute($params); if ($status == true) header("location: edit_jobs.php"); else { $error_info = $stmt->errorinfo(); $error_message = "failed delete job: {$error_info[2]} - error code {$error_info[0]}"; require 'error.php'; } } else { $error_message = "booking id not valid"; require 'edit_jobs.php'; } # make sure close database connection when finished...
Comments
Post a Comment