how to divide a web page in php? - php

This is my code:
<?php
function GetDays($sStartDate, $sEndDate){
$sStartDate = gmdate("Y-m-d", strtotime($sStartDate));
$sEndDate = gmdate("Y-m-d", strtotime($sEndDate));
$aDays[] = $sStartDate;
$sCurrentDate = $sStartDate;
while($sCurrentDate < $sEndDate){
$sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));
$aDays[] = $sCurrentDate;
}
return $aDays;
}
?>
<html>
<head>
<script language="JavaScript" src="calendar_us.js"></script>
<link rel="stylesheet" href="calendar.css">
</head>
<body background="mainbg.JPG">
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<?
//If we submitted the form
if(isset($_POST['submitMe']))
{
$sec=00;
$sc=00;
$name=$_POST['name'];
$mm=substr($_POST['testinput'],0,2);
$dd=substr($_POST['testinput'],3,2);
$yy=substr($_POST['testinput'],6,4);
$sdate = $yy."-".$mm."-".$dd;
$mn=substr($_POST['test'],0,2);
$dn=substr($_POST['test'],3,2);
$yn=substr($_POST['test'],6,4);
$edate = $yn."-".$mn."-".$dn;
$host="10.207.100.10";// Host name
$username="root";// Mysql username
$password="######"; // Mysql password
$db_name="wtems"; // Database name
$tbl_name="constellation_reserve"; // Table name // Connect to server and selectdatabse
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM constellation_reserve where CID=$name and (start_date between '$sdate' and '$edate' or end_date between '$sdate' and '$edate')";
$result=mysql_query($sql); // Define$color=1
if($result)
{
$color="1";
echo '<table border="0" cellpadding="8" align="center">';
echo'<tr>';
echo'</tr>';
echo'<tr>';
echo'</tr>';
echo '<tr><h3 align="center"<font color="white">RESERVATION </font></h3></tr>';
echo'<tr>';
echo'</tr>';
echo'<tr>';
echo '<table width="500" height="300" border="0" align="center" cellpadding="8" cellspacing="2">';
$fields_num = mysql_num_fields($result);
echo "<tr bgcolor='#0099FF'>";
echo "<td><FONT COLOR='FFFFFF'>ID</FONT> </td>";
echo "<td><FONT COLOR='FFFFFF'>START DATE</FONT></td>";
echo "<td> <FONT COLOR='FFFFFF'>RESERVED BY </FONT></td>";
echo "<td><FONT COLOR='FFFFFF'>END DATE</FONT></td>";
echo "</tr>\n";
while($rows=mysql_fetch_array($result))
{ // If $color==1 table row color = #FFC600
if($color==1)
{
echo "<tr bgcolor='#FFFFDD'>
<td>".$rows['CID']."</td><td>".$rows['start_date']."</td><td bgcolor='#99AAFF'><FONT COLOR='FFFFFF'>".$rows['owner']."</FONT></td><td>".$rows['end_date']."</td></tr>";
// Set$color==2, for switching to other color
$color="2";
} // When$color not equal 1, use this table row color
else
{
echo "<tr bgcolor='#FFFFEE'><td>".$rows['CID']."</td><td>".$rows['start_date']."</td><td bgcolor='#99AAFF'><FONT COLOR='FFFFFF'>".$rows['owner']."</FONT></td><td>".$rows['end_date']."</td></tr>";// Set $color back to 1
$color="1";
}
}
echo '</table>';
mysql_close();
echo '</tr>';
echo '</table>';
}
else
{
echo '<table border="0" cellpadding="8" align="center">';
echo '<tr><h3 align="center"<font color="white">NoDataFound</font></h3></tr>';
echo '</table>';
}
}
//If we haven't submitted the form
else
{
?>
<h3 align="center"<font color="white">REPORT DETAILS</font></h3>
<table border="1" cellpadding="8" align="center">
<br>
</br>
<tr><td align="left">Constellation ID</td><td>:</td><td> <input type="text" name="name" /></td></tr>
<tr><td align="left">Start Date</td><td>:</td><td> <input type="text" name="testinput" />
<script language="JavaScript">
new tcal ({
// form name
//'formname': 'testform'
// input name
'controlname': 'testinput'
});
</script>
</td></tr>
<tr><td align="left">End Date</td><td>:</td><td> <input type="text" name="test" />
<script language="JavaScript">
new tcal ({
// form name
//'formname': 'testform'
// input name
'controlname': 'test'
});
</script>
</td></tr>
</table>
<br>
</br>
<br>
</br>
<p align="center"><input type="submit" value="REPORT" name="submitMe"></p>
<?php
}
?>
</form>
</body>
</html>
when i execute the code is working fine but, the datas are not displaying in the same page.
when the data are displaying the page is refreshing so i cant view the data in same page.

You should never use a root username and password when you are developing an app, create an account and use, when you post remove it.
The issue has to be coming from your JavaScript if your page refreshes after loading. PHP is a server side language, meaning it can redirect before user output but not after. Post your JS or a link to the page here so we can help more.

Seconding #ProNeticas -- NEVER ever use your root password when you are developing an app.
Also your code is open to SQL injection -- please read about mysql_real_escape_string

#user.. i just format your code... please look and apply... then we'll discuss on your problem...
<?php
function GetDays($sStartDate, $sEndDate){
$sStartDate = gmdate("Y-m-d", strtotime($sStartDate));
$sEndDate = gmdate("Y-m-d", strtotime($sEndDate));
$aDays[] = $sStartDate;
$sCurrentDate = $sStartDate;
while($sCurrentDate < $sEndDate){
$sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));
$aDays[] = $sCurrentDate;
}
return $aDays;
}
?>
<html>
<head>
<script type="javascript" src="calendar_us.js"></script>
<link rel="stylesheet" href="calendar.css" >
</head>
<body background="mainbg.JPG">
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<?
//If we submitted the form
if(isset($_POST['submitMe']))
{
$sec=00;
$sc=00;
$name=$_POST['name'];
$mm=substr($_POST['testinput'],0,2);
$dd=substr($_POST['testinput'],3,2);
$yy=substr($_POST['testinput'],6,4);
$sdate = $yy."-".$mm."-".$dd;
$mn=substr($_POST['test'],0,2);
$dn=substr($_POST['test'],3,2);
$yn=substr($_POST['test'],6,4);
$edate = $yn."-".$mn."-".$dn;
$host="#######";// Host name
$username="#####";// Mysql username
$password="######"; // Mysql password
$db_name="####"; // Database name
$tbl_name="constellation_reserve"; // Table name // Connect to server and selectdatabse
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM constellation_reserve where CID=$name and (start_date between '$sdate' and '$edate' or end_date between '$sdate' and '$edate')";
$result=mysql_query($sql); // Define$color=1
$fields_num = mysql_num_fields($result);
if($result)
{ ?>
<table border="0" cellpadding="8" cellspacing="5" align="center">
<tr>
<td><h3 align="center"<font color="white">RESERVATION </font></h3></td>
</tr>
<tr>
<td>
<table width="500" height="300" border="0" align="center" cellpadding="8" cellspacing="2">
<tr style="color:#fff;background-color:#0099FF">
<td>ID</td>
<td>START DATE</td>
<td>RESERVED BY</td>
<td>END DATE</td>
</tr>
<?php
while($rows=mysql_fetch_array($result))
{ $color=1;
// If $color==1 table row color = #FFC600
if($color == 1){ ?>
<tr background-color='#FFFFDD'>
<td><?php echo $rows['CID']?></td>
<td><?php echo $rows['start_date'];?></td>
<td style="color:#FFF;background-color:#99AAFF'><?php echo $rows['owner'];?></td>
<td><?php echo $rows['end_date']?></td>
</tr>
<?php $color=2;
} else { ?>
<tr background-color='#FFFFEE'>
<td><?php echo $rows['CID'];?></td>
<td><?php echo $rows['start_date'];?></td>
<td style="color:#FFF;background-color:#99AAFF'><?php echo $rows['owner'];</td>
<td><?php echo $rows['end_date'];?></td>
</tr>
<?php $color=1;
}
} ?>
</table>
<?php mysql_close(); ?>
</td>
</tr>
</table>
<?php } else { ?>
<table border="0" cellpadding="8" align="center">
<tr><h3 align="center"style="color:#fff">NoDataFound</h3></tr>
</table>
<?php }
} //If we haven't submitted the form
else { ?>
<h3 align="center"<font color="white">REPORT DETAILS</font></h3>
<table border="1" cellpadding="8" align="center">
<tr>
<td align="left">Constellation ID</td>
<td>:</td>
<td> <input type="text" name="name" /></td>
</tr>
<tr>
<td align="left">Start Date</td>
<td>:</td>
<td> <input type="text" name="testinput" />
<script type="javascript">
new tcal ({
// form name
//'formname': 'testform'
// input name
'controlname': 'testinput'
});
</script>
</td>
</tr>
<tr>
<td align="left">End Date</td>
<td>:</td>
<td> <input type="text" name="test" />
<script typr"javascript">
new tcal ({
// form name
//'formname': 'testform'
// input name
'controlname': 'test'
});
</script>
</td>
</tr>
</table>
<p align="center"><input type="submit" value="REPORT" name="submitMe"></p>
<?php } ?>
</form>
</body>
</html>

Related

Can you update in a div tag

I have a little question about something. I have some forms, where I send the input to a MySQL database. I get the return from the database, out in some div tags. Here I have 2 buttons. Button number 1 can delete the row, and button number 2 should have a function, where I can update a specific row, if I want to change the content of the row. But can I update a div tag? I can see on the net, that a lot of people use tables to do that.
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/arrangeTables.css">
</head>
<body>
<?php
include 'connection.php';
if(isset($_POST['addto'])){
// Insert to database
$date = $_POST['date'];
$day = $_POST['day'];
$fromtime = $_POST['fromtime'];
$totime = $_POST['totime'];
$sql = "INSERT INTO addWorkTime(date, day, fromtime, totime) VALUES('$date', '$day', '$fromtime', '$totime')";
$result = mysql_query($sql, $dbhandle) or die(mysql_error($dbhandle));
// Update of the row
if(isset($_POST['update'])){
$hidden = mysql_real_escape_string($_POST['hidden']);
$UpdateQuery = "UPDATE addWorkTime
SET date='$_POST[date]',
day='$_POST[day]',
fromtime='$_POST[fromtime]',
totime='$_POST[totime]'
WHERE p_id='$_POST[hidden]'";
$update = mysql_query($UpdateQuery, $dbhandle) or die(mysql_error($dbhandle));
if($update) {
echo "Succes";
} else {
echo "Der er en fejl";
}
}; // brace for if(isset($_POST['update']))
if($result){
echo "Insert successful.";
}
}; // brace for if(isset($_POST['addto']))
if(isset($_POST['delete'])){
$hidden = mysql_real_escape_string($_POST['hidden']);
$DeleteQuery = "DELETE FROM addWorkTime WHERE p_id=$hidden";
$delete = mysql_query($DeleteQuery, $dbhandle) or die(mysql_error($dbhandle));
if($delete){
echo "Delete successful";
}
}
//Return records from database
$result = mysql_query("SELECT p_id, date, day, fromtime, totime FROM addWorkTime");
?>
<form method="post">
<h3>Add your worktime to database</h3><br>
Date:
<input type="date" name="date"><br><br>
Day
<select name="day">
<option value="Mandag">Mandag</option>
<option value="Tirsdag">Tirsdag</option>
<option value="Onsdag">Onsdag</option>
<option value="Torsdag">Torsdag</option>
<option value="Fredag">Fredag</option>
<option value="Lørdag">Lørdag</option>
<option value="Søndag">Søndag</option>
</select>
From time:
<input type="time" name="fromtime">
To time:
<input type="time" name="totime">
<input type="submit" name="addto" value="submit"><br><br>
<!-- Return from the database -->
<h3>Return from database:</h3><br>
<!-- headers -->
<table>
<tr>
<th class="column0">Primary Key</th>
<th class="column1">Date</th>
<th class="column2">Day</th>
<th class="column3">From</th>
<th class="column4">To</th>
</tr>
</table>
</form>
<!--loop through through the database -->
<?php while($row = mysql_fetch_array($result)): ?>
<form method="post">
<table>
<tr>
<td class="resultcolumn0"><?php echo $row{'p_id'};?></td>
<td class="resultcolumn1"><?php echo $row{'date'};?><br></td>
<td class="resultcolumn2"><?php echo $row{'day'};?></td>
<td class="resultcolumn3"><?php echo $row{'fromtime'};?></td>
<td class="resultcolumn4"><?php echo $row{'totime'};?></td>
<td><input type="hidden" name="hidden" value="<?php echo $row{'p_id'}?>"><?php echo $row{'p_id'}?></td>
<td><input type="submit" name="update" value="Update"></td>
<td><input type="submit" name="delete" value="Delete"></td>
</tr>
</table>
</form>
<?php endwhile; ?>
</body>
</html>
If you want to update your cell table like your recording, you can do a tricky way like this code snippet:
function change1() {
var inp1 = document.getElementById('myTable').rows[1].cells[0];
inp1.innerHTML = "<input type='text' name='inp1'>";
var inp2 = document.getElementById('myTable').rows[1].cells[1];
inp2.innerHTML = "<input type='text' name='inp2'>";
}
function change2() {
var inp1 = document.getElementById('myTable').rows[2].cells[0];
inp1.innerHTML = "<input type='text' name='inp1'>";
var inp2 = document.getElementById('myTable').rows[2].cells[1];
inp2.innerHTML = "<input type='text' name='inp2'>";
}
table, td {
border: 1px solid black;
}
<table id="myTable">
<tr>
<td>column 1</td>
<td>column 2</td>
<td>action</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
<td>
<button onclick="change1()">Update</button>
</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
<td>
<button onclick="change2()">Update</button>
</td>
</tr>
</table>
<button onclick="alert('whatever function to save the edit')">Save</button>
you can improve that to give <form> tag to your <table> tag and save that using another Save button. Or you can use if conditional to your update button for change to textfield and post it to your database.
<form method="post" action='function_to_update_or_delete.php'>
<table>
<tr>
<td class="resultcolumn0"><?php echo $row{'p_id'};?></td>
<td class="resultcolumn1"><?php echo $row{'date'};?><br></td>
<td class="resultcolumn2"><?php echo $row{'day'};?></td>
<td class="resultcolumn3"><?php echo $row{'fromtime'};?></td>
<td class="resultcolumn4"><?php echo $row{'totime'};?></td>
<td><input type="hidden" name="hidden" value="<?php echo $row{'p_id'}?>"><?php echo $row{'p_id'}?></td>
<td><button onclick="<?php echo change($some_information_to_your_cell)?>">Update</button></td>
<td><input type="submit" name="delete" value="Delete"></td>
</tr>
</table>
</form>
And, please try to make a function dynamically each row, don't hardcode it like function change1(), function change2(), function change3(), etc
Hope this will help you out. :)

Add a value to a table from text field

In a form there is a text field and a table. If we put a value in that text field the corresponding name needs to be displayed on the table:
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<center><table>
<tr><td>no</td><td><input type="text" name="no" autofocus></td></tr>
</table></center>
<br>
<br>
<center>
<table cellpadding="0" cellspacing="0" width="60%" border='1' bgcolor="#999999">
<thead>
<tr>
<th> Element_Number</th>
</tr>
</thead>
<?php
if(isset($_POST['no']))
{
$c=mysql_connect("localhost","root","");
mysql_select_db("test");
if(!$c)
{
echo "db prob".mysql_error();
}
$no=$_POST['no'];
$qry=mysql_query("select name from opt where no='$no'");
if(!$qry)
{
echo "ret".mysql_error();
}
if(mysql_num_rows($qry)!=0)
{
$row=mysql_fetch_array($qry);
$name=$row['name'];
}
else
{
echo "query Invalid";
}
echo "<td>" .$name."</td></tr>" ;
}
?>
Using this code the value enters in the table but I want to add the name frequently the value to the table as next row when the next value enter to the text field
do it with the help of ajax
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<center>
<table>
<tr><td>no</td><td><input type="text" name="no" autofocus></td></tr>
<tr><input type="button" name="submit" value="submit"></tr>
</table></center>
</form>
<br>
<br>
<center>
<table id="test_table" cellpadding="0" cellspacing="0" width="60%" border='1' bgcolor="#999999">
<thead>
<tr>
<th> Element_Number</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
$(document).ready(function(){
$('#submit').click(function(){
var no=$('[name="no"]');
$.post('your_current_page_name.php',{"no":no},function(data){
$("#test_table> tbody").append(data);
});
});
});
</script>
<?php
if(isset($_POST['no']))
{
$c=mysql_connect("localhost","root","");
mysql_select_db("test");
if(!$c)
{
echo "db prob".mysql_error();
}
$no=$_POST['no'];
$qry=mysql_query("select name from opt where no='$no'");
if(!$qry)
{
echo "ret".mysql_error();
}
if(mysql_num_rows($qry)!=0)
{
$row=mysql_fetch_array($qry);
$name=$row['name'];
}
else
{
echo "query Invalid";
}
echo "<tr><td>" .$name."</td></tr>" ;
}

Error when inserting data into table in sql using php form

i'm creating asset database system (just an offline system, not connected to internet) that will show all assets list. on the list, i can click on any asset to view the details. also i'm manage to update the details or delete the asset. but when it goes to asset record parts, it give an error on inserting record to asset using a form.
here is my record add form. and i'm also wanna make machine id visible under MACHINE ID field in the form, but i did't know yet how to put the data there.
for inserting record, its will capture machine id on rekod_add.php ( record add) url address from asset table to passing into rekod_tab table.
here is my record add page ( rekod_add.php )
<?php
//Start session
session_start();
//Check whether the session variable SESS_MEMBER_ID is present or not
if(!isset($_SESSION['username']) || (trim($_SESSION['password']) == '')) {
header("location: login.php");
exit();
}
?>
<html>
<head>
<title>EXA_mySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
body,td,th {
font-family: Tahoma, Geneva, sans-serif;
}
</style>
</head>
<body>
<script type="text/javascript">function checkinput() {
var id_mesin = document.getElementById('id_mesin').value;
if(!id_mesin.match(/\S/)) {
alert ('Please enter Machine ID');
return false;
} else {
return true;
}
}
</script>
<?php
$con=mysqli_connect("localhost","root","admin","exa");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id_mesin = $_POST['id_mesin'];
$query = "SELECT * FROM asset WHERE id_mesin ='".$id_mesin."'";
$result = mysqli_query($con,$query);
$rows = mysqli_fetch_array($result);
?>
<table width="733" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form_insert" method="post" action="rekod_add_ac.php">
<table width="709" border="0" align="center">
<tr>
<th width="23" scope="col">MACHINE ID</th>
<th colspan="2" scope="col">DATE</th>
<th width="68" scope="col">TIME</th>
<th width="175" scope="col">RECEIVE CALL BY</th>
<th width="97" scope="col">CURRENT METER</th>
<th width="90" scope="col">LAST METER</th>
<th width="136" scope="col">J.SHEET NO</th>
</tr>
<tr>
<td> </td>
<td colspan="2"><input name="tarikh_rekod" type="text" id="tarikh_rekod" size="15" /></td>
<td><input name="time" type="text" id="time" size="10" maxlength="9" /></td>
<td><input type="text" name="call_by" id="call_by" /></td>
<td><input name="meter_semasa" type="text" id="meter_semasa" size="15" /></td>
<td><input name="meter_last" type="text" id="meter_last" size="15" /></td>
<td><input name="rujukan" type="text" id="rujukan" size="10" /></td>
</tr>
<tr>
<td> </td>
<th width="81">PROBLEM</th>
<th width="5">:</th>
<td colspan="3"><textarea name="masalah" id="masalah" cols="55" rows="5"></textarea></td>
<th colspan="2" rowspan="2"><p>REMARK</p>
<p>
<textarea name="remark" cols="30" rows="6" id="remark"></textarea>
</p></th>
</tr>
<tr>
<td> </td>
<th>SOLUTION</th>
<th>:</th>
<td colspan="3"><textarea name="solution" id="solution" cols="55" rows="5"></textarea></td>
</tr>
<tr>
<td colspan="8" align="right"><?php echo "<input type='hidden' value='" . $rows['id_mesin'] . "' name='id_mesin'>"; echo "<input type='submit' value='Add Record'>";?></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php
mysqli_close($con);
?>
</body>
</html>
here is my rekod_add_ac.php
<?php
session_start();
if(!isset($_SESSION['username']) || (trim($_SESSION['password']) == '')) {
header("location: login.php");
exit();
}
?>
<html>
<head>
<title>EXA_mySQL</title>
<script type="text/javascript">
<!--
function CloseWindow() {
window.close();
window.opener.location.reload();
}
//-->
</script>
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors','on');
$con=mysqli_connect("localhost","root","admin","exa");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
print_r($_POST);
$id_mesin=$_POST['id_mesin'];
$tarikh_rekod=$_POST['tarikh_rekod'];
$time=$_POST['time'];
$call_by=$_POST['call_by'];
$meter_semasa=$_POST['meter_semasa'];
$meter_last=$_POST['meter_last'];
$rujukan=$_POST['rujukan'];
$masalah=$_POST['masalah'];
$solution=$_POST['solution'];
$remark=$_POST['remark'];
$rekod_in="INSERT INTO rekod_tab ( id_mesin, tarikh_rekod, time, call_by, meter_semasa, meter_last, rujukan, masalah, solution, remark) VALUES ( $'id_mesin', $'tarikh_rekod', $'time', $'call_by', $'meter_semasa', $'meter_last', $'rujukan', $'masalah', $'solution', $'remark')";
$result=mysqli_query($con, $rekod_in);
if($result){
echo "Successful";
echo "<BR>";
echo "<th><form>";
echo "<input type='button' onClick='CloseWindow()' value='Back to Exa_mySQL' align='middle'>";
echo "</form></th>";
}
else {
echo "Data error, please recheck before submit.";
echo "<BR>";
echo "Click back to add record.";
echo "<BR>";
echo "<form action='rekod_add.php?id=$id_mesin' method='post'>";
echo "<td><input type='hidden' value='$id_mesin' name='id_mesin'>";
echo "<input type='submit' value='Back'></td>";
echo "</form>";
echo "<th><form>";
}
mysqli_close($con);
?>
</body>
</html>
after user done inserting record details, the form will add the record on rekod_tab table including machine id ( id_mesin ) which automatically capture from url like i said before.
but the result is error. when inserting detail manual in sql, its work. can anyone help me?
here my error result.
sorry for my bad english.
Try INSERT query like this
$rekod_in="INSERT INTO rekod_tab
( id_mesin, tarikh_rekod, time, call_by, meter_semasa, meter_last,
rujukan, masalah, solution, remark)
VALUES ( '$id_mesin', '$tarikh_rekod', '$time', '$call_by', '$meter_semasa',
'$meter_last', '$rujukan', '$masalah', '$solution', '$remark')";

Search function for virtuemart

please could you advice me if there is some mistake in process of creating this page?
I would like to do search component for virtuemart and I use one php file, where I have written whole code. I want to add there new conditions to $compare2 variable using array variable send2[] and by adding new select options. Please do you have any suggestions? I am not an expert. Thanks
<?php
$con=mysqli_connect("","","","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['Submit'])) {
$compare1 = $_POST['send1'];
?>
?>
<table border="1" cellspacing="2" cellpadding="2">
<?php
for ($i=0; $i<sizeof($compare1);$i++) {
$result = mysqli_query($con,"SELECT * FROM auto_virtuemart_products WHERE '$compare1[$i]' LIKE product_sku");
?>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td>
<?php echo $row['product_sku'];?>
</td>
<td>
<?php echo $row['product_weight'];?>
</td>
</tr>
<?php
}
}
?>
</table>
<?php
echo "<input type='button' value='spat' onclick=location.href=''>";
}
else
{
?>
<table border="1" cellspacing="2" cellpadding="2">
<?php
$result = mysqli_query($con,"SELECT * FROM auto_virtuemart_products");
while($row = mysqli_fetch_array($result)) {
?>
<select>
<option><?php echo $row['product_sku']; ?></option>
</select>
<form method="post">
<input type="checkbox" name="send2" value="<?php echo $row['product_sku'] ?>" />
<?php
}
?>
</table>
<input type="Submit" name="Submit2" value="Vyber"/></form>
<table border="1" cellspacing="2" cellpadding="2">
<?php
if (isset($_POST['Submit2'])) {
$compare2 = $_POST['send2'];
$result = mysqli_query($con,"SELECT * FROM auto_virtuemart_products WHERE '$compare2' LIKE product_sku");
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $row['product_sku']; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $row['product_weight']; ?></font>
</td>
<td>
<form method="post">
<input type="checkbox" name="send1[]" value="<?php echo $row['product_sku'] ?>" />
</td>
</tr>
<?php
}
}
else
{
echo "Empty";
}
mysqli_close($con);
?>
</table>
<input type="Submit" name="Submit" value="Submit"/></form>
<?php
}
?>

Deleting Multiple records in php My sql

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="toybox"; // Database name
$tbl_name="Emp"; // 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);
// echo $count;
?>
<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['EmpId']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['EmpId']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['FirstName']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['LastName']; ?></td>
<td bgcolor="#FFFFFF"><?php 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 id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo " Record have been deleted";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
Rather than doing a query for each iteration of your delete-loop, I would build all of the indexes up into a string, and use something like the following:
DELETE FROM tableName
WHERE id IN (1,2,12,53)
Also, your submit button won't come through as $delete, but instead $_POST["delete"]. And with your connection:
mysql_connect("$host", "$username", "$password")
You really ought not use variables like strings (generally) - this should be written as:
mysql_connect($host, $username, $password)
Furthermore, you've got a few more problems in and around your delete-logic. For instance, I pointed out already that your <input type='submit' name='delete' /> button will be known as $_POST["delete"] once it is registered on the server. Likewise, your checkboxes, having an id value of checkbox[] will simply be known as $_POST["checkbox"] on the server.
Also, your $count variable, used in your delete-logic, is based on the earlier query that selected all of the records to show them. It does not reflect the number of checkboxes to be deleted, it reflects the number of records that were shown. As such, your for loop should not be based on it:
for ($i = 0; $i < count($_POST["checkbox"]); $i++)
{
// delete $_POST["checkbox"][$i];
}
And again, I would suggest you build a string of values and run a single query instead of multiple.
Working Code .. Consider Point 1 2 and 3
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("funconnect") or die(mysql_error());
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM members")
or die(mysql_error());
$count=mysql_num_rows($result);
echo "<form name='sendmail' method='post' action='memberList.php'><table border='1'>";
echo "<tr> <th>Select</th> <th>Name</th> </tr>";
// keeps getting the next row until there are no more to get
$countSn = 0;
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
$chkname = "checkbox".$countSn; #Point 1- Create defferent name for checkboxex like checkbox0, checkbox1
echo "<tr><td><input type='checkbox' name=".$chkname." value=".$row['m_id']." /></td>";
echo "</td><td>";
echo $row['m_name'];
echo "</td></tr>";
$countSn++;
}
echo '<tr><td colspan=2><input name="delete" type="submit" id="delete" value="Delete"></td></tr></table></form>';
$delete=$_POST['delete'];
$checkbox=$_POST['checkbox'];
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$cname = "checkbox".$i;# Point 2- Create check box name like checkbox0, checkbox1
$checkbox=$_POST[$cname]; #Point 3 - Retrieve data against name
echo $i."===".$checkbox."<br />";
//echo $del_id;
//$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();
?>

Categories