By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,944 Members | 1,280 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,944 IT Pros & Developers. It's quick & easy.

Deleting row in mysql database using php script

P: 2
Hi everybody,

I need your help.

I want to view my sql database and its work very well which is display in my web browser
but once I want to press button yes, its not working, I check the database the specified row was nit deleted

Here I put my script for both.
<?php #script view_users.php pg 279

include 'index1.php';

//This script retrieves all the records from the register table.

//This new version links to edit and delete pages.

//Page header.
echo '<h1 id="mainhead">Registered Projects</h1>';

require_once 'Connect/mysql_connect.php';

//Make the query.

$query = "SELECT serial, date, department, user_id, email, project, description FROM register";
//ORDER BY serial ASC";

//Run the query
$result = mysql_query ($query);
$num = mysql_num_rows($result);

if ($num > 0) { //If it ran OK, display the records.

echo "<p>There are currently $num registered users.</p>\n";

//Table header.

echo '<table align-"center" cellspacing="15" cellpadding-"5">

<td align="left"><b>Serial</b></td>
<td align="left"><b>Date</b></td>
<td align="left"><b>Department</b></td>
<td align="left"><b>User</b></td>
<td align="left"><b>Email</b></td>
<td align="left"><b>Project</b></td>
<td align="left"><b>Description</b></td>
<td align="left"><b>Edit</b></td>
<td align="left"><b>Delete</b></td>

//Fetch and print all the records.
$bg = '#eeeeee'; //set the background color.

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// echo '<tr>
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); //switch the background color.

echo '<tr bgcolor="' . $bg . '">

<td align="left">' . $row['serial'].'</td>
<td align="left">' . $row['date'].'</td>
<td align="left">' . $row['department'].'</td>
<td align="left">' . $row['user_id'].'</td>
<td align="left">' . $row['email'].'</td>
<td align="left">' . $row['project'].'</td>
<td align="left">' . $row['description'].'</td>
<td align="left"><a href="edit_user.php?serial_num=' . $row['serial'] . '">Edit</a></td>
<td align="left"><a href="delete_user.php?serial_num=' . $row['serial'] . '">Delete</a></td>


echo '</table>';

mysql_free_result ($result); //Free up the resources.

} else { //If did not run OK.

echo '<p class="error">There are currently no registered projects.</p>';

mysql_close(); //Close the database connection.


and below for deleting the project specified.

<?php #delete_user.php pg 283

//This page deletes a user.
//This page is accessed through view_users.php

//Check for a valid user serial number ID, through GET or POST.

if ( (isset($_GET['serial_num'])) && (is_numeric
($_GET['serial_num'])) ) { //acessed through view_users.php

$serial_num = $_GET['serial_num'];

} elseif ( (isset($_POST['serial_num']) ) &&

(is_numeric($_POST['serial_num'])) ) { //form has been submitted.

$serial_num = $_POST['serial_num'];

} else { //No valid ID, kill the script.

echo '<h1 id="mainhead">Page Error
<p class="error">This page has been
accessed in error.</p><p><br /><br />

require_once 'Connect/mysql_connect.php'; //connect to the database.

//check if the form has been submitted.
if (isset($_POST['submitted'])) {

if ($_POST['sure'] == 'Yes') { //Delete them.

//Make the query
$query = "DELETE FROM register WHERE serial=$serial_num";
$result = @mysql_query ($query); //Run the query

if (mysql_affected_rows() == 1) { //If it ran OK

//Print a message.
echo '<h1 id="mainhead">Delete a

<p>The project has been deleted.</p><p>
<br /><br /></p>';

} else { //If the query did not run OK.

echo '<h1 id="mainhead">System

<p class="error">The project could
not be deleted due to a system
error.</p>'; //public message
echo '<p>' . mysql_error() .
'<br /><br />Query: '. $query .
'</p>'; //debugging message.

} else { //wasn't not sure about deleting the project.

echo '<h1 id="mainhead">Delete a
<p>The project has NOT been deleted.
</p><p><br /></p>';


} else { // Show the form

//Retrieve the user's information.

$query = "SELECT project FROM register WHERE serial=$serial_num";

$result = @mysql_query($query); //Run the query.

if (mysql_num_rows($result) == 1) { //valid serial number, show the form.

//Get the serial number information.

$row = mysql_fetch_array ($result, MYSQL_NUM);

//Create the form.

echo '<h2>Delete the project</h2>

<form action="delete_user.php" method="post">
<h3>Project: ' . $row[0] . '</h3>
<p> Are you sure you want to delete this project?<br />
<input type="radio" name="sure" value="Yes" /> Yes
<input type="radio" name="sure" value="No" checked="checked" /> No</p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="serial_num" value="' . $serial_num . '" />

} else { //Not a valid serial number.

echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';


} //End of the main Submit conditional.

mysql_close(); //Close the database connection.

Dec 8 '06 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 100+
P: 2,330
wrong forum
moved to php
also added code tags
Dec 8 '06 #2

Expert 2.5K+
P: 4,258
Your form posts value "Submit" name "submit". [php]<input type="submit" name="submit" value="Submit" /></p>[/php]
Your form handler checks "submitted".[php]if (isset($_POST['submitted'])) {

Ronald :cool:
Dec 8 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.