Can not connect to my database server using PHP - php

I'm trying to build a profile page system for users bare in mind I'm real new to php. In the code below, I get the error message I specified it to echo, when i cannot connect to my database ie :
"could not connect to server"
I dont get any other error messages, only this. I cant seem to find the problem, any help would be appreciated.
Here is my code :
<?php
include_once 'Header.php';
?>
<?php
if (isset($_GET['user_uid']))
$user_uid = $_GET['user_uid'];
mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
mysqli_select_db($conn, "users") or die ("could not connect to server");
$userquery = mysqli_query("SELECT * FROM users where user_uid='$user_uid'")
or die ("The Query could not be completed, contact an administrator");
if (mysql_num_rows($userquery) != 1) {
die ("that username could not be found");
}
while ($row = mysqli_fetch_array($userquery, MYSQL_ASSOC)) {
$first = $row['first'];
$last = $row['last'];
$city = $row['city'];
$country = $row['country'];
}
?>
<table width="398" border="0" align="center" cellpadding="0">
<tr>
<td height="26" colspan="2">Your Profile Information </td>
<td><div align="right">logout</div></td>
</tr>
<?php echo $first; ?>
<tr>
<td width="129" rowspan="5"><img src="<?php echo $picture ?>" width="129"
height="129" alt="no image found"/></td>
<td width="82" valign="top"><div align="left">FirstName:</div></td>
<td width="165" valign="top"><?php echo $first ?></td>
</tr>
<tr>
<td valign="top"><div align="left">LastName:</div></td>
<td valign="top"><?php echo $last ?></td>
</tr>
<tr>
<tr>
<td valign="top"><div align="left">City:</div></td>
<td valign="top"><?php echo $city ?></td>
</tr>
<tr>
<td valign="top"><div align="left">Country:</div></td>
<td valign="top"><?php echo $country ?></td>
</tr>
</table>
<p align="center"></p>
<?php
include_once 'Footer.php';
?>

It is specifically dying because of $conn - that doesn't appear to be set anywhere.
However in saying that, as has been pointed out, you don't need to use mysqli_select_db in mysqli as it's all done in the mysqli_connect function.
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
That is enough - you only need the mysqli_db to change the current DB, but even then it needs a valid link identifier (the result returned by mysqli_connect).
So, that would be:
mysqli_select_db($conn, "users") or die ("could not connect to server");
...as long as $conn is valid.

Sorry guys, the whole layout I was using was wrong, for making a live profile page, this new layout fixed the problems :)...
<?php
include_once 'RoadieHeader.php';
?>
<?php
//PULLING OUT USERNAME//
$result = mysqli_query($conn, "select user_uid from users where user_uid
='".$_SESSION['u_uid']."'");
$row = mysqli_fetch_array($result);
$uid = ucfirst($row['user_uid']);
//PULLING OUT EVERYTHING!//
$result_f = mysqli_query($conn, "select user_first from users where
user_first ='".$_SESSION['u_first']."'");
$row_f = mysqli_fetch_array($result_f);
$first = ucfirst($row_f['user_first']);
$last = ucfirst($_SESSION['u_last']);
$city = ucfirst($_SESSION['u_city']);
$country = ucfirst($_SESSION['u_country']);
$about = ucfirst($_SESSION['u_about']);
?>
<table width="398" border="0" align="center" cellpadding="0">
<tr>
<td height="26" colspan="2"><?php echo $uid ?></td>
<td><div align="right">logout</div></td>
</tr>
<tr>
<td width="129" rowspan="5"><img src="<?php echo $picture ?>"
width="129" height="129" alt="no image found"/></td>
<td width="82" valign="top"><div align="left">FirstName:</div></td>
<td width="165" valign="top"><?php echo $first ?></td>
</tr>
<tr>
<td valign="top"><div align="left">LastName:</div></td>
<td valign="top"><?php echo $last ?></td>
</tr>
<tr>
<tr>
<td valign="top"><div align="left">City:</div></td>
<td valign="top"><?php echo $city ?></td>
</tr>
<tr>
<td valign="top"><div align="left">Country:</div></td>
<td valign="top"><?php echo $country ?></td>
</tr>
<tr>
<td valign="top"><div align="left">About me: </div></td>
<td valign="top">
<?php echo $about ?>
</td>
</tr>
</table>
<p align="center"></p>
<?php
include_once 'RoadieFooter.php';
?>
I forgot the current user's ID's are stored in $_SESSION when they log in, and is carried page to page on session_start()

Related

i am unable to view the mysql Data in my table made on php .mysql table primary key intake. 2. I have created 2 php file.first delete.php and delq.php

This is the delete page where i want to view the mysql stored data in table but I am unable view. I have created 7 columns for table name registration but I can't fetch the data from database.
<div id="main">
<div id="formainsupport"></div>
<div id="forreg">
<?Php
$tbl_name="registration"; // Table name
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("examonline.", $con);
// select record from mysql
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<table width="900" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td colspan="8" bgcolor="#FFFFFF"><strong>Delete data in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>intakeid</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>firstname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>password</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>confirmpassword</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>homeaddress</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>email</strong></td>
<td align="center" bgcolor="#FFFFFF"> </td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['intakeid']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['firstname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['password']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['confirmpassword']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['homeaddress']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
<td bgcolor="#FFFFFF">delete</td>
</tr>
<?php
// close while loop
}
?>
</table>
<?php
// close connection;
mysql_close();
?>
Here is code for the to delete the data from database. My sql delete query is here but I think it's not working although it's a fine code. I am unable to find the problem.
<h1>delq.php</h1>
<?php
$tbl_name="registration"; // Table name
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("examonline.", $con);
// get value of id that sent from address bar
$intakeid=$_GET['intakeid'];
// Delete data in mysql from row that has this id
$sql="delete from $tbl_name where intakeid='$intakeid'";
$result=mysql_query($sql);
// if successfully deleted
if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='Delete.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysql_close();
?>

Rows are not being deleted

Here is my php code to select and delete rows form table:
<h1>Deleting Multiple Records using PHP & MySQL</h1>
<p> </p>
<?php
These are my db connection:
$host = "localhost"; // Host name
$username = "root"; // Mysql username
$password = ""; // Mysql password
$db_name = "project"; // Changed database name
$tbl_name = "users";
mysql_connect($host, $username, $password) or die("cannot connect");
mysql_select_db($db_name) or die("cannot select DB");
$sql = "SELECT * FROM $tbl_name";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
?>
Here is my html code to retrieve data from database:
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr><td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"><tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td> </tr>
<tr><td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>First Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Gender</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Phone Number</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Experiance</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>User Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Password</strong></td>
</tr>
<?php while ($rows = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['fname']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['lname']; ?> </td>
<td bgcolor="#FFFFFF"><?php echo $rows['gend']; ?> </td>
<td bgcolor="#FFFFFF"><?php echo $rows['phone']; ?> </td>
<td bgcolor="#FFFFFF"><?php echo $rows['exp']; ?> </td>
<td bgcolor="#FFFFFF"><?php echo $rows['uname']; ?> </td>
<td bgcolor="#FFFFFF"><?php echo $rows['pwd']; ?> </td>
</tr>
<?php } ?>
<tr><td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete" onclick="javascript:confirm('Are you sure to delete')"></td></tr>
<?php
if(isset($_POST['delete'])){
$checkbox = $_POST['checkbox'];
for($i=0;$i<count($_POST['checkbox']);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
print $sql;
$result = mysql_query($sql);}
if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";}}
mysql_close();
?>
</table></form></td></tr></table>
<p>Record count: <?php echo number_format($count) ?></p>*/
I tried many times but it not deleting a single row from table.
just now i changed the code of executing query....please check it and inform me...
What is this query
$sql = "DEFRE FROM users WHERE id in (" . implode(",", $deleteIds) . ") ";
??
For deleting you should do something like this
$sql = "DELETE FROM users WHERE id IN (" . implode(",", $deleteIds) . ") ";
In the code
if (isset($_POST['checkbox']) && count($_POST['checkbox']) > 0)
{
$deleteIds = $_POST['checkbox']; // it will be an array
$sql = "DELETE FROM users WHERE id in (" . implode(",", $deleteIds) . ") ";
// run the query
}
We need to see the actual code trying to "delete". The usual mysql_query($sql) won't work. You have to use mysql_exec() instead of mysql_query().
Update: Sorry, I was confusing another form of query. mysql_query("delete from table where (whatever)") should work, though it returns true on success or false on failure, not a rowset.
I deleted one line ID from my program and i got the answer and it's working fine.
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" id="checkbox[]" value='<?php echo $row['id']; ?>'></td>
<td bgcolor="#FFFFFF"><?php echo $row['fname']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['lname']; ?> </td>
<td bgcolor="#FFFFFF"><?php echo $row['gend']; ?> </td>
<td bgcolor="#FFFFFF"><?php echo $row['phone']; ?> </td>
<td bgcolor="#FFFFFF"><?php echo $row['exp']; ?> </td>
<td bgcolor="#FFFFFF"><?php echo $row['uname']; ?> </td>
<td bgcolor="#FFFFFF"><?php echo $row['pwd']; ?> </td>
</tr>
<?php } ?>
<tr><td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete" onclick="javascript:confirm('Are you sure to delete')"></td></tr>
<?php
if(isset($_POST['delete'])){
$checkbox = $_POST['checkbox'];
for($i=0;$i<count($_POST['checkbox']);$i++){
$del_id = $checkbox[$i];
//print '$del_id';
$sql = "DELETE FROM users WHERE id='$del_id'";
//print $sql;
$result = mysql_query($sql);}
}
if($result){
echo"successfully deleted";
}
mysql_close();
?>
</table></form></td></tr></table>
<p>Record count: <?php echo number_format($count) ?></p>

Displaying MSQL with PHP. Want to move selected rows to another sql table and delete from existing table

I am designing a mobile helpdesk system for my department. I currently have an HTML form that submits data to a MySQL table. I then display that table on a separate page with checkboxes beside each line. Currently the complete button is coded to simply delete any record that is checked. However, pressing this button appears to do nothing. I am wondering what I am doing wrong.
Here is the code:
<?php
$host="localhost";
$username="root";
$password="";
$db_name="opentix";
$tbl_name="opentix";
//Connect and Select
mysql_connect("$host","$username","$password") or die("Cannot Connect");
mysql_select_db("$db_name")or die("Cannot Select Database");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><form name="OpenTickets" method="post" action"">
<table width="400" border="0" cellspacing="3" cellpadding="0" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"></td>
<td align="center" colspan="15" bgcolor="#FFFFFF"><strong>Open Tickets</strong>
</td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><strong></stong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Ticket Number</stong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Time Created</stong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Room</stong></td>
<td align="center" bgcolor="#FFFFFF"><strong>If Other</stong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Problem Type</stong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Machine Name</stong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Operating System</stong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Problem Description</stong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Troubleshooting</stong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Request</stong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</stong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['tixnum']; ?>".</td>
<td bgcolor="#FFFFFF"><? echo $rows['tixnum']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['timecreated']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Room']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Other']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Type']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['System']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Problem']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Troubleshooting']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Request']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="15" align="center" bgcolor="#FFFFFF"><input name="complete" type ="submit" id="complete" value="Complete"></td>
</tr>
<?php
if($complete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM opentix WHERE id='$del_id'";
$result = mysql_querry($sql);
}
if($result){
echo"<meta http-equiv=\"refresh\"content=\"0;URL=pcsehelpdesk\completeopen.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
Any input into what I am doing wrong would be great.
Looks like you need to tell the form to post to itself. Add this to your tag:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
More reading here: http://tycoontalk.freelancer.com/php-forum/51903-php-self-submitting-form.html

Undefined variable: delete in C:\xampp\htdocs\xampp\Test\HRMS\try\search1.php on line 61

I am trying very hard for the last 2 hours but am not able to understand. Why am I getting undefined variable in DELETE. Please please look in to my code and help me out.
My main intention is to delete multiple rows at once using checkboxes.
When I am trying to run the below codes it gives me following errors:
Undefined variable: delete in C:\xampp\htdocs\xampp\Test\HRMS\try\search1.php on line 61
But I already have used it in following way:
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
Thanks in advance.
my php codes :
<?php
$host="localhost"; // Host name
$username="a"; // Mysql username
$password="a"; // Mysql password
$db_name="b"; // Database name
$tbl_name="emp_info"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Password</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Address</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Source</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>salary</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Zip</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Mobile</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['eid']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['eid']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['ename']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['password']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['address']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['source']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['salary']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['zip']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['mobile']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<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($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE eid='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to search1.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=search1.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
Register globals is off (and this is good) so the line
if($delete){
should be
if (isset($_POST['delete'])) { ...
Moreover you need to restructure your code as it's absolutely unnecessery to make a select query to database when you have a post request to delete records.
Here is a complete code you need. Just replace yours with this one.
<?php
error_reporting(E_ALL);
$host = "localhost"; // Host name
$username = "a"; // Mysql username
$password = "a"; // Mysql password
$db_name = "b"; // Database name
$tbl_name = "emp_info"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Delete records
if (isset($_POST['delete']))
{
// Concatenate ids in a comma-separated string
$ids = implode(',', $_POST['checkbox']);
if (!empty($ids))
{
$sql = "DELETE FROM $tbl_name WHERE eid IN ($ids)";
$result = mysql_query($sql) or die(mysql_error());
}
// Redirect back
header('Location: search1.php');
die();
}
// Select records
$sql = "SELECT * FROM $tbl_name";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong></td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
</tr>
<?php while ($rows = mysql_fetch_array($result)) { ?>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" id="checkbox[]"
value="<?php echo $rows['eid']; ?>">
</td>
<td bgcolor="#FFFFFF"><?php echo $rows['eid']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['ename']; ?></td>
</tr>
<?php } ?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
Before looping results from MySQL result variable, you should ALWAYS have to make sure that it contain any values:
if($total > 0)
{
while ($rows = mysql_fetch_array($result))
{
//your code goes here
}
}
Another issue you have here, is same as in your previous question, variable $delete is not defined, based on your code, it is not clear where it is coming from.
So, if $delete is actually a $_POST variable, your if statement should look like this:
if(isset($_POST['delete'])){}
Another issue, $checkbox it is not defined to. I would recommend you to read more about filter_input method as it can be very handy on sanitizing user submitted data.
Also you are using incorrect html tags instead of:
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
use correct TD tag which aligns text in center and makes it bold, also bgcolor should be defined in CSS file.
<th bgcolor="#FFFFFF">Id</th>
Your final code should look like this:
<?php
$host = "localhost"; // Host name
$username = "a"; // Mysql username
$password = "a"; // Mysql password
$db_name = "b"; // Database name
$tbl_name = "emp_info"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Make sure that delete action was called using
//$delete = isset($_POST['delete']);
//OR
//$delete = array_key_exists('delete', $_POST);
//OR
$delete = filter_input(INPUT_POST, 'delete');
$selected = filter_input(INPUT_POST, 'checkbox', FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY);
if ($delete && !empty($selected))
{
foreach ($selected as $id)
{
$sql = "DELETE FROM $tbl_name WHERE eid='$id'";
$result = mysql_query($sql);
}
// if successful redirect to search1.php
echo "<meta http-equiv=\"refresh\" content=\"0;URL=search1.php\">";
}
$sql = "SELECT * FROM $tbl_name";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
?>
<form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<th bgcolor="#FFFFFF"> </th>
<th colspan="9" bgcolor="#FFFFFF">Delete multiple rows in mysql</th>
</tr>
<tr>
<th bgcolor="#FFFFFF">#</th>
<th bgcolor="#FFFFFF">Id</th>
<th bgcolor="#FFFFFF">Name</th>
<th bgcolor="#FFFFFF">Password</th>
<th bgcolor="#FFFFFF">Address</th>
<th bgcolor="#FFFFFF">Source</th>
<th bgcolor="#FFFFFF">salary</th>
<th bgcolor="#FFFFFF">Zip</th>
<th bgcolor="#FFFFFF">Mobile</th>
<th bgcolor="#FFFFFF">Email</th>
</tr>
<?php
if ($count)
{
while ($rows = mysql_fetch_array($result))
{
?>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" id="checkbox" value="<?php echo $rows['eid']; ?>">
</td>
<td bgcolor="#FFFFFF"><?php echo $rows['eid']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['ename']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['password']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['address']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['source']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['salary']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['zip']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['mobile']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['email']; ?></td>
</tr>
<?php
}
}
mysql_close();
?>
<tr>
<td colspan="10" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete">
</td>
</tr>
</table>
</form>
Please don't just copy paste it, read it and try to understand it.

Delete multiple rows with checkbox form DB table

Here is my code -
<?php
$host = "localhost"; // Host name
$username = ""; // Mysql username
$password = ""; // Mysql password
$db_name = "test"; // Database name
$tbl_name = "test_mysql"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name") or die("cannot select DB");
$sql = "SELECT * FROM $tbl_name";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong></td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php
while ($rows = mysql_fetch_array($result)) {
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox"
id="checkbox[]" value="<? echo $rows['id']; ?>">
</td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete"
value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if ($delete) {
for ($i = 0; $i < $count; $i++) {
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if ($result) {
echo "<meta http-equiv="refresh" content="0;URL = delete_multiple . php">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
What I want is that data rows of the table, whose checkboxes are checked,
to be Deleted from the Database on click of Delete button.
I have tried these but it doesn't work well.
Please suggest me regarding the above code how to delete the selected rows
from DB, onclick of Delete button.
Theoretically it should work, but I have not tested
<?php
$host = 'localhost'; // Host name
$username = ''; // Mysql username
$password = ''; // Mysql password
$db_name = 'test'; // Database name
$tbl_name = 'test_mysql'; // Table name
// Connect to server and select databse.
mysql_connect($host, $username, $password) or die('cannot connect');
mysql_select_db($db_name) or die('cannot select DB');
$sql = 'SELECT * FROM `'.$tbl_name.'`';
$result = mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php while ($rows = mysql_fetch_array($result)): ?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="need_delete[<? echo $rows['id']; ?>]" type="checkbox" id="checkbox[<? echo $rows['id']; ?>]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['name']); ?></td>
<td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['lastname']); ?></td>
<td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['email']); ?></td>
</tr>
<?php endwhile; ?>
<tr>
<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 ( ! empty($_POST['delete'])) {
foreach ($_POST['need_delete'] as $id => $value) {
$sql = 'DELETE FROM `'.$tbl_name.'` WHERE `id`='.(int)$id;
mysql_query($sql);
}
header('Location: delete_multiple.php'); exit();
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
Tested version:
Change lines 45-55 to
if($_POST['delete']){
$i = 0;
while(list($key, $val) = each($_POST['checkbox'])) {
$sql = "DELETE FROM $tbl_name WHERE id='$val'";
mysql_query($sql);
$i += mysql_affected_rows();
}
// if successful redirect to delete_multiple.php
if($i > 0){
echo '<meta http-equiv="refresh" content="0;URL=delete_multiple.php">';
}
}
The file should start with "<?php" and it is considered bad style to mix "<?" and "<?php" in the same file (lines 31-35 and line 43).
EDIT: forgot the $_POST[''] from $delete on line 45. If you have the register_globals ini-directive on, it does not matter (It's still dangerous and bad style though).

Categories