Not deleting values from Database using checkboxes - php

I have a quick question, I'm trying to use a function to delete a certain movie from a database using checkboxes. I can add stuff to the database just fine, but can't seem to delete using the checkboxes. Thanks in advance for your help!
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
</head>
<body>
<?
$db = new PDO("mysql:host=localhost;dbname=armstrongpz", "armstrongpz", "12345");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
<?
function deleteFromDatabase($db)
{
$deleteQuery = $db->prepare("SELECT * FROM movie");
$deleteQuery->execute();
foreach($deleteQuery->fetchAll() as $row)
{
if(array_key_exists($row['id'], $_POST))
{
$deleteQuery = $db->prepare('DELETE FROM movie WHERE movie.id='. $row['id']);
$deleteQuery->excute();
}
}
}
?>
<h2 style='text-align: center';>
Movie Collection Database
</h2>
<?
if($_SERVER['REQUEST_METHOD'] === "POST")
{
deleteFromDatabase($db);
if(isset($_POST['add']))
{
$TitleOfMovie = $_POST['TitleMovie'];
$StudioOfMovie = $_POST['StudioMovie'];
$RatingOfMovie = $_POST['mRating'];
$PubYearOfMovie = $_POST['PubYear'];
$IMDBRating = floatval($_POST['imdbRating']);
$RunTimeOfMovie = $_POST['RunTime'];
$addQuery = "INSERT INTO movie (id,title,studio,rating,pub_year,imdb_rating,run_time) VALUES(".'NULL'.", '$TitleOfMovie','$StudioOfMovie','$RatingOfMovie','$PubYearOfMovie', '$IMDBRating', '$RunTimeOfMovie')";
$statement = $db->prepare($addQuery);
$statement->execute();
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
<table align='center' border='0' cellpadding='5'>
<tbody>
<tr>
<td style='border-style: none'>
<input name='TitleMovie' size='50' type='text' width='35' value= 'Title'/>
</td>
<td style='border-style: none'>
<input name='StudioMovie' size='25' type='text' width='35' value= 'Studio Name'/>
</td>
<td>
<?
$rating = array(1=>'G', 'PG', 'PG-13', 'R', 'NC-17', 'Not Rated');
echo "<select size='1' selected='movieRating' name='mRating'>";
foreach($rating as $key=>$value)
{
echo "<option value=\"$key\">$value</option>\n";
}
echo "</select>\n";
?>
</td>
<td style='border-style: none'>
<input name='PubYear' size='10' type='text' width='35' value='Pub Year'/>
</td>
<td style='border-style: none'>
<input name='imdbRating' size='10' type='text' width='35' value='IMDB rating'/>
</td>
<td>
<input name='RunTime' size='10' type='text' width='35' value='Run time'/>
</td>
<td>
<input type="checkbox" name="add" value="Add" align="top"/>Add
</td>
</tr>
<div style="text-align:center">
<input type='submit' name='submit' value='Update Database'>
</div>
</tbody>
</table>
</form>
<table border ='1' align='center'>
<?
$arrayOfFieldNames = array("Title", "Studio", "Rating", "Pub. Year", "IMDB Rating", "Run time(min)");
echo "<tr>";
echo "<td>". $arrayOfFieldNames[0] . "</td>";
echo "<td>". $arrayOfFieldNames[1] . "</td>";
echo "<td>". $arrayOfFieldNames[2] . "</td>";
echo "<td>". $arrayOfFieldNames[3] . "</td>";
echo "<td>". $arrayOfFieldNames[4] . "</td>";
echo "<td>". $arrayOfFieldNames[5] . "</td>";
echo "<td>Delete</td>";
echo "</tr>";
?>
<?
$query = "SELECT * FROM movie";
$stmt = $db->prepare($query);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
echo "<td>" .$row['title']. "</td>";
echo "<td>" .$row['studio']. "</td>";
echo "<td>" .$row['rating']. "</td>";
echo "<td>" .$row['pub_year']. "</td>";
echo "<td>" .$row['imdb_rating']. "</td>";
echo "<td>" .$row['run_time']. "</td>";
echo "<td><input type='checkbox' name='". $row['id'] . "' value='" . $row['id'] . "'>Delete</td>";
echo "</tr>";
}
?>
</table>
<?
if(isset($db))
{
$db= null;
}
?>
</body>

Related

can't figure out how to list all rows from MYSQL in php

So I have finished my page and want to list all the data in my database for the current customer id selected and I can only seem to get the first row in the DB to list, I can't get all of them.
I have tried a while loop but I just can't seem to get the logic. Any help would be appreciated
Here is the entire code for the page. I want to use the last table on the page to show all the customer data.
I have labeled it with a comment if you are looking for the section im referring to.
// Display all data for customer from db. -- cant seem to figure this part out [while loop?]
<?php
// Check existence of id parameter before processing further
if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
// Include config file
require_once "../includes/config.php";
// Prepare a select statement
$sql = "SELECT * FROM customers, orders WHERE customers.id = orders.customerId AND customers.id = ?";
if($stmt = mysqli_prepare($con, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "i", $param_id);
// Set parameters
$param_id = trim($_GET["id"]);
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
// Retrieve individual field value
$customerId = $row['customerId'];
$name = $row['name'];
$address = $row['address'];
$phone = $row['phone'];
$email = $row['email'];
$other1 = $row['other1'];
$other2 = $row['other2'];
$notStartedCheckbox = $row['notStartedCheckbox'];
$dateToWork = $row['dateToWork'];
$finishedCheckbox = $row['finishedCheckbox'];
$dateTimeFinished = $row['dateTimeFinished'];
$paidCheckbox = $row['paidCheckbox'];
$dateTimePaid = $row['dateTimePaid'];
$paidWith = $row['paidWith'];
$notes = $row['notes'];
} else{
// URL doesn't contain valid id parameter. Redirect to error page
header("location: error.php");
exit();
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>View Record</title>
<!-- stylesheets -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- scripts -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js#1.16.1/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="https://kit.fontawesome.com/16d3fe3a77.js" crossorigin="anonymous"></script>
<style>
.wrapper{
width: 80%;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div>
<h3 class="mt-5 mb-3"><?php echo $row['name'] . ' | ' . $row['address'] . ' | ' . $row['phone'] . ' | ' . $row['email'] . ' ' . $row['other1'] . ' ' . $row['other2'] ; ?></h3>
<!-- Submit a form that adds a new job for a customer -->
<form action="newWorkOrder.php" method="post">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Customer ID (Auto)</th>
<th>Started</th>
<th>Work Date</th>
<th>Service</th>
<th>Finished</th>
<th>Finished Date/Time</th>
<th>Paid</th>
<th>Paid Date/Time</th>
<th>Paid With</th>
<th>Notes</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="radio" checked name="customerId" id="customerId" value="<?php echo $row['customerId'] ?>"></td>
<td><input type="checkbox" name="notStartedCheckbox" id="notStartedCheckbox" value="0"></td>
<td><input type="date" name="dateToWork" id="dateToWork"></td>
<td>
<select name="service" id="service">
<option value="mowing">Mowing</option>
</select>
</td>
<td><input type="checkbox" name="finishedCheckbox" id="finishedCheckbox" value="0"></td>
<td><input type="datetime-local" name="dateTimeFinished" id="dateTimeFinished"></td>
<td><input type="checkbox" name="paidCheckbox" id="paidCheckbox" value="0"></td>
<td><input type="datetime-local" name="dateTimePaid" id="dateTimePaid"></td>
<td><input type="text" name="paidWith" id="paidWith"></td>
<td><input type="text" name="notes" id="notes"></td>
<td><input type="submit" value="submit"></td>
</tr>
</table>
</form>
<hr>
<!-- Display the table -->
<?php
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>Started</th>";
echo "<th>Work Date</th>";
echo "<th>Service</th>";
echo "<th>Finished</th>";
echo "<th>Finished Date/Time</th>";
echo "<th>Paid</th>";
echo "<th>Paid Date/Time</th>";
echo "<th>Paid With</th>";
echo "<th>Notes</th>";
echo "</tr>";
echo "</thead>";
// Display all data for customer from db. -- cant seem to figure this part out [while loop?]
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['notStartedCheckbox'] . "</td>";
echo "<td>" . $row['dateToWork'] . "</td>";
echo "<td>" . $row['service'] . "</td>";
echo "<td>" . $row['finishedCheckbox'] . "</td>";
echo "<td>" . $row['dateTimeFinished'] . "</td>";
echo "<td>" . $row['paidCheckbox'] . "</td>";
echo "<td>" . $row['dateTimePaid'] . "</td>";
echo "<td>" . $row['paidWith'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
echo "</table>";
?>
<br>
<p>Back</p>
</div>
</div>
</div>
</div>
</body>
</html>
Here is the section I can't get to work properly, it will display all rows after I submit a new form, but won't show any pre-existing data from my database.
while($row = mysqli_fetch_array($result) ){
echo "<tr>";
if($row['notStartedCheckbox'] == true) {
echo "<td>" . 'Started' . "</td>";
} else {
echo "<td style='color: red'>Not Started</td>";
}
echo "<td>" . $row['dateToWork'] . "</td>";
echo "<td>" . $row['service'] . "</td>";
if($row['finishedCheckbox'] == true) {
echo "<td>" . 'Finished' . "</td>";
} else {
echo "<td style='color: red'>Not Finished</td>";
}
echo "<td>" . $row['dateTimeFinished'] . "</td>";
if($row['paidCheckbox'] == true) {
echo "<td>" . 'Paid' . "</td>";
} else {
echo "<td style='color: red'>Not Paid</td>";
}
echo "<td>" . $row['dateTimePaid'] . "</td>";
echo "<td>" . $row['paidWith'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}

PHP select option values when use with echo

I am trying to show details in a table and show list of values in drop down list. the values are shown in table but drop down list is not showing the values although the values exists and when i save it in session and print it, it appears at the top . Also I have problem in using name of select. when i click on button and wants to read the post value of the option, it gives me error as shown in figure
here is the code:
for($i=0;$i<count($dr_ide);$i=$i+5)
{
echo "<tr>";
echo "<td> Dr. " . $dr_namee[$i] . "</td>";
echo "<td>" . $sub_namee[$i] . "</td>";
echo "<td>" . $sub_namee[$i+1] . "</td>";
echo "<td>" . $sub_namee[$i+2] . "</td>";
echo "<td>" . $sub_namee[$i+3] . "</td>";
echo "<td>" . $sub_namee[$i+4] . "</td>";
echo "<td> <select name='perr' >
<option name='perr' selected=selected>Choose one</option>";
foreach($Names as $name) {
echo"<option name='perr' value=";
$name ;
echo "> ";
$_SESSION["tt"]=$name;
$name;
echo"</option>";
}
echo "</select></td>";
//echo "<td>" . $dayse[$i] . "</td>";
//echo "<td>" . $timing[$i] . "</td>";
echo "</tr>";
}
}
echo $_SESSION["tt"];
if(isset($_POST['confirm'])){
echo $_POST['perr'];
}
Here is my full page code.
<html>
<head>
<title> Expert System </title>
<link rel="stylesheet" type="text/css" href="style.css">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:700,600' rel='stylesheet' type='text/css'>
</head>
<body>
<div >
<table id="cells2" border="0" cellpadding="15" cellspacing="5" font size="6">
<tr>
<th><img id="header2" src="images/Kuwait_University_Logo.jpg" > </th>
<th>KUWAIT UNIVERSITY</th>
</tr>
</table>
<div class='back1'>
<table border='1' align='center' id='customers' >
<?php
require "init.php";
session_start();
global $con,$users,$dr_id,$sub_id,$dr_name,$sub_name,$days,$fav,$timing;
global $dr_ide,$dr_namee,$sub_namee,$dayse,$fave,$timinge;
$query= "SELECT * FROM subjects_current where subjects_current.sub_ID NOT IN (SELECT test.subject_id from test)";
$result=mysqli_query($con,$query);
if ( $result->num_rows == 0 ) // User doesn't exist
echo "Subjects doesn't exist!";
else {
while($row = mysqli_fetch_array($result))
{
$IDs[]=$row['sub_ID'];
$Names[]=$row['Name'];
//echo $row['Name'];
}
}
$query= "SELECT * FROM test order by dr_id";
$result=mysqli_query($con,$query);
if ( $result->num_rows == 0 ) // User doesn't exist
echo "Subjects doesn't exist!";
else { echo "
<tr>
<th>Professor Name</th>
<th>First Choice</th>
<th>Second Choice</th>
<th>Third Choice</th>
<th>Fourth Choice</th>
<th>Fifth Choice</th>
<th>Update Subject</th>
</tr>";
$r=0;
$f=0;
while($row = mysqli_fetch_array($result))
{
$dr_ide[$f]=$row['dr_id'];
$dr_namee[$f]=$row['dr_name'];
$sub_namee[$f]=$row['sub_name'];
$dayse[$f]=$row['days'];
$timinge[$f]=$row['timing'];
$fave[$f]=$row['fav'];
//echo "<tr>";
//echo "<td> Dr. " . $dr_namee[$f] . "</td>";
//echo "<td>" . $sub_namee[$f] . "</td>";
//echo "<td>" . $fave[$f] . "</td>";
//echo "<td>" . $dayse[$f] . "</td>";
//echo "</tr>";
//$r++;
$f++;
}
//for($i=0;$i<count($Names);$i=$i+5)
//{
for($i=0;$i<count($dr_ide);$i=$i+5)
{
echo "<tr>";
echo "<td> Dr. " . $dr_namee[$i] . "</td>";
echo "<td>" . $sub_namee[$i] . "</td>";
echo "<td>" . $sub_namee[$i+1] . "</td>";
echo "<td>" . $sub_namee[$i+2] . "</td>";
echo "<td>" . $sub_namee[$i+3] . "</td>";
echo "<td>" . $sub_namee[$i+4] . "</td>";
echo "<td> <select name='perr' >
<option name='perr' selected=selected>Choose one</option>";
foreach($Names as $name) {
echo"<option name='perr' value='$name'>$name</option>";
}
echo "</select></td>";
//echo "<td>" . $dayse[$i] . "</td>";
//echo "<td>" . $timing[$i] . "</td>";
echo "</tr>";
}
}
echo $_SESSION["tt"];
if(isset($_POST['confirm'])){
echo $_POST['perr'];
}
?>
</table>
<form method='post' action='edit_subjects.php'>
<input ID="btn2" name="confirm" type="submit" value="Home">
</form>
</div>
</body>
</html>
Remove name='perr' from <options> and change foreach() like below:-
foreach($Names as $name) {
echo"<option value='$name'>$name</option>";
}
Do change in your code like this:-
echo "<form method='post' action='edit_subjects.php'>"; //add before while
while($row = mysqli_fetch_array($result))
{
$dr_ide[$f]=$row['dr_id'];
$dr_namee[$f]=$row['dr_name'];
$sub_namee[$f]=$row['sub_name'];
$dayse[$f]=$row['days'];
$timinge[$f]=$row['timing'];
$fave[$f]=$row['fav'];
$f++;
}
for($i=0;$i<count($dr_ide);$i=$i+5)
{
$dr_nam = $dr_namee[$i];
echo "<tr>";
echo "<td> Dr. " . $dr_namee[$i] . "</td>";
echo "<td>" . $sub_namee[$i] . "</td>";
echo "<td>" . $sub_namee[$i+1] . "</td>";
echo "<td>" . $sub_namee[$i+2] . "</td>";
echo "<td>" . $sub_namee[$i+3] . "</td>";
echo "<td>" . $sub_namee[$i+4] . "</td>";
echo "<td><select name='perr[$dr_nam]' >
<option name='perr' selected=selected>Choose one</option>";
foreach($Names as $name) {
echo"<option name='perr' value='$name'>$name</option>";
}
echo "</select></td>";
echo "</tr>";
}
echo"<input ID='btn2' name='confirm' type='submit' value='Home'></form>"; //add just after while code ended
echo "<td> <select name='perr' >
<option name='perr' selected='selected'> Choose one </option>";
foreach($Names as $name) {
echo"<option name='perr' value='". $name ."'> ". $name ." </option>";
}
echo "</select></td>";
You have to echo the names like this:
foreach($Names as $name) {
echo "<option value=";
echo $name ;
echo "> ";
$_SESSION["tt"]=$name;
echo $name;
echo"</option>";
}
And put the table inside the form:
<html>
<head>
<title> Expert System </title>
<link rel="stylesheet" type="text/css" href="style.css">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:700,600' rel='stylesheet' type='text/css'>
</head>
<body>
<div >
<table id="cells2" border="0" cellpadding="15" cellspacing="5" font size="6">
<tr>
<th><img id="header2" src="images/Kuwait_University_Logo.jpg" > </th>
<th>KUWAIT UNIVERSITY</th>
</tr>
</table>
<div class='back1'>
<form method='post' action='edit_subjects.php'>
<table border='1' align='center' id='customers' >
<?php
require "init.php";
session_start();
global $con,$users,$dr_id,$sub_id,$dr_name,$sub_name,$days,$fav,$timing;
global $dr_ide,$dr_namee,$sub_namee,$dayse,$fave,$timinge;
$query= "SELECT * FROM subjects_current where subjects_current.sub_ID NOT IN (SELECT test.subject_id from test)";
$result=mysqli_query($con,$query);
if ( $result->num_rows == 0 ) // User doesn't exist
echo "Subjects doesn't exist!";
else {
while($row = mysqli_fetch_array($result))
{
$IDs[]=$row['sub_ID'];
$Names[]=$row['Name'];
//echo $row['Name'];
}
}
$query= "SELECT * FROM test order by dr_id";
$result=mysqli_query($con,$query);
if ( $result->num_rows == 0 ) // User doesn't exist
echo "Subjects doesn't exist!";
else { echo "
<tr>
<th>Professor Name</th>
<th>First Choice</th>
<th>Second Choice</th>
<th>Third Choice</th>
<th>Fourth Choice</th>
<th>Fifth Choice</th>
<th>Update Subject</th>
</tr>";
$r=0;
$f=0;
while($row = mysqli_fetch_array($result))
{
$dr_ide[$f]=$row['dr_id'];
$dr_namee[$f]=$row['dr_name'];
$sub_namee[$f]=$row['sub_name'];
$dayse[$f]=$row['days'];
$timinge[$f]=$row['timing'];
$fave[$f]=$row['fav'];
//echo "<tr>";
//echo "<td> Dr. " . $dr_namee[$f] . "</td>";
//echo "<td>" . $sub_namee[$f] . "</td>";
//echo "<td>" . $fave[$f] . "</td>";
//echo "<td>" . $dayse[$f] . "</td>";
//echo "</tr>";
//$r++;
$f++;
}
//for($i=0;$i<count($Names);$i=$i+5)
//{
for($i=0;$i<count($dr_ide);$i=$i+5)
{
echo "<tr>";
echo "<td> Dr. " . $dr_namee[$i] . "</td>";
echo "<td>" . $sub_namee[$i] . "</td>";
echo "<td>" . $sub_namee[$i+1] . "</td>";
echo "<td>" . $sub_namee[$i+2] . "</td>";
echo "<td>" . $sub_namee[$i+3] . "</td>";
echo "<td>" . $sub_namee[$i+4] . "</td>";
echo "<td> <select name='perr' >
<option selected=selected>Choose one</option>";
foreach($Names as $name) {
echo"<option value='$name'>$name</option>";
}
echo "</select></td>";
//echo "<td>" . $dayse[$i] . "</td>";
//echo "<td>" . $timing[$i] . "</td>";
echo "</tr>";
}
}
echo $_SESSION["tt"];
if(isset($_POST['confirm'])){
echo $_POST['perr'];
}
?>
</table>
<input ID="btn2" name="confirm" type="submit" value="Home">
</form>
</div>
</body>
</html>

Having issue showing image returning from database with $.post() jquery and php

I'm retrieving a table from database as result of a search action, but when I try to display the result I see the table and the data, but the image returned in each row is not render, I think my problem is in the $('#search').html(data), I'm not sure please someone knows what is the problem?
this is the result
http://s9.postimg.org/mro5qn46n/search_result.jpg
****This is the search page, where result table is displayed****
<table align="center">
<tr>
<td>
<label for="criteria">Select Criteria</label>
</td>
<td>
<select name="select" id="criteria">
<option selected="true" style="display:none;"></option>
<option value="value1">name</option>
<option value="value2">apartment</option>
</select>
</td>
<td>
<input type="text" name="value" size="40" maxlength="60" id="value"\>
</td>
</tr>
<tr>
<td>
<input name="name-submit" type="button" id="submit" value="Search"\>
</td>
</tr>
<tr>
<td >
<div id="search"></div>
</td>
</tr>
</table>
<script type="text/javascript" src="../js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$('#submit').click(function(){
var criteria = $("#criteria option:selected").text();
var value = $("#value").val();
$.post("search_r.php",{criteria:criteria,value:value},function(data){
$('#search').html(data);
});
});
</script>
****This is the Page that calls $.post() in the main seach page ****
<?php
$criteria = $_POST['criteria'];
$value = $_POST['value'];
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$query = "SELECT * FROM residents WHERE $criteria = '$value'";
$result = mysqli_query($con,$query);
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Last Name</th>
<th>Apartment</th>
<th>Parking</th>
<th>Phone1</th>
<th>Phone2</th>
<th>image</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['0'] . "</td>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo "<td>" . $row['5'] . "</td>";
echo "<td>" . $row['6'] . "</td>";
echo "<td><img src=get_image.php?id=".$row['0']." width=160 height=120/></td>";
echo "</tr>";
}
echo "</table>";
?>
***Here the get_image.php****
<?php
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$id = $_GET['id'];
$query = "SELECT * FROM residents WHERE id='$id'";
$result = mysqli_query($con,$query);
if($result)
$picture = mysqli_fetch_array($result);
header('Content-Type: image/jpg');
echo $picture['11'];
?>
You can chage the get_image.php file as this. Then this work will work.
<?php
function get_image($id){
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$query = "SELECT * FROM residents WHERE id='$id'";
$result = mysqli_query($con,$query);
if($result)
$picture = mysqli_fetch_array($result);
return $picture['11'];
}
?>
Then use require_once(); function and in image src like this.echo "<td><img src='".get_image($row['0'])."' width=160 height=120/></td>";. In your code check how the src path will print and it will be print like as echo string, not as a execute the file.
echo
"<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Last Name</th>
<th>Apartment</th>
<th>Parking</th>
<th>Phone1</th>
<th>Phone2</th>
<th>image</th>
</tr>";
require_once('search_r.php');
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['0'] . "</td>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo "<td>" . $row['5'] . "</td>";
echo "<td>" . $row['6'] . "</td>";
echo "<td><img src='".get_image($row['0'])."' width=160 height=120/></td>";
echo "</tr>";
}
echo "</table>";

Deleting rows from table on web retrieved from database

I am having issue with deleting rows from a database that I echoed onto my website, I have used tick check boxes and when multiples are selected they should be deleted. But it's just NOT HAPPENING! Nothing is getting deleted from the database! please help!
<form method="" action="tester.php">
<?php
include 'connect_to_mysql.php';
$count=0;
$count=mysql_num_rows($result);
$result = mysql_query("SELECT * FROM booking ORDER BY ID ASC");
echo "<table border='1'>
<tr>
<th>DEL</th>
<th>Name</th>
<th>Email ID</th>
<th>Phone Number</th>
<th>Collection Address</th>
<th>Collection Date</th>
<th>Collection Time</th>
<th>Make & Model</th>
<th>Message</th>
</tr>";
while($row = mysql_fetch_array($result))
{
?>
<td align="center" bgcolor="#FFFFFF"><input name="delete_these[]" type="checkbox" id="checkbox[]" value="<?php echo $row['ID']; ?>"></td>
<?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['collectionaddress'] . "</td>";
echo "<td>" . $row['collectiondate'] . "</td>";
echo "<td>" . $row['collectiontime'] . "</td>";
echo "<td>" . $row['makemodel'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
?> <br>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if(isset($_GET['delete'])) {
for($i=0;$i<$count;$i++){
$id =(int)$_POST['delete_these'][$i];
$sql = "DELETE FROM booking WHERE ID='$id'";
print_r($_GET['delete_these[]']);
$sql = "DELETE FROM booking WHERE id IN($ids)";
echo "<br />SQL: $sql<br />";
$result = mysql_query($sql);
}
if($result){
}
}
mysql_close();
?>
</form>
First off you can just implode() all the gathered ids from the form and from there build the query.
Sample code:
<form method="POST" action="index.php">
<table>
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
<td><input type="checkbox" name="delete_these[]" value="<?php echo $row['id']; ?>" /></td>
<td><?php echo $row['name']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<input type="submit" name="delete" value="Delete Selected" />
</form>
<?php
$selected_values = array();
if(isset($_POST['delete'])) {
$selected_values = $_POST['delete_these'];
$ids = implode(',', $selected_values);
$query = mysql_query("DELETE FROM booking WHERE id IN($ids)");
// this becomes -> delete from booking where id in (1, 2, 3, ...)
}
?>
and while you still can, use mysqli or PDO, its free anyway

post value's text field OrderForm

I have multiple books that people can order. All those books are stored in a MySQL database.
People can enter value's (INT) into a textfield. Example: Book One value = [5].
But when I enter submit it will only show the last entered value of that textfield.
How can I arrange, that if people only enter value's in some textfields and then hit submit they see what product they ordered and the value with it. Thanks :)
My code
<table width="990">
<form name="form" action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<tr>
<td width="93" class="main">Bestelnummer</td>
<td width="550" class="main">Titel</td>
<td width="100" class="main">Categorie</td>
<td width="150" class="main">Type Onderwijs</td>
<td width="80" class="main">Groep</td>
<td width="50" class="main">Prijs</td>
<td width="40" class="main">Aantal</td>
</tr>
<?php
// Laat Resultaten zien
$s = "SELECT * FROM producten ORDER BY id ASC";
$sql = (mysql_query($s))or die ("FOUT: " . mysql_error());
while($row = mysql_fetch_array($sql))
{
$id = $row['id'];
echo "<tr>";
echo "<td width='93'>" .$row['bestelnummer']. "</td>";
echo "<td width='550'><a href='".$row['link']."' target='_blank' title='".$row['titel']."' >" .$row['titel']. "</a></td>";
echo "<td width='100'>" .$row['categorie']. "</td>";
if ($row['onderwijs'] == "BO") { echo "<td width='150'>Basis Onderwijs</td>"; } elseif ($row['onderwijs'] == "VO") { echo "<td width='150'>Voortgezet Onderwijs</td>"; } else { }
echo "<td width='80'>" . $row['groep'] . "</td>";
if ($row['prijs'] == 0) { echo "<td width='50'><i>gratis</i></td>"; } else { echo "<td width='50'>€ " .$row['prijs']. "</td>"; }
echo "<td width='40'><input type='text' name='nummer".$id."' title='nummer".$id."' class='aantal' maxlength='4' /></td>";
echo "</tr>";
}
?>
<tr>
<td><input type="submit" name="submit" value="Plaats bestelling" class="verzend"/></td>
</tr>
</form>
</table>
<?php if (isset($_POST['submit']))
{
if (empty($_POST['aantal']))
{
echo "L33g";
}
else
{
$_POST['nummer".$id."'];
}
}?>
You would be better off using an array so use
<input type='text' name='nummer[".$id."]' title='nummer".$id."' class='aantal' maxlength='4' />
Then replace
$_POST['nummer".$id."'];
with
foreach($_POST['nummer'] as $value) {
echo $value;
}

Categories