Sorry, but i ve got another problem... with connection, I guess.
I try to delete record, but my database is empty. And look like this:
And this is the code. I know, this probably trivial matter but I'm amateur in PHP.
<?php
$con=mysqli_connect("localhost","root","","kluby ranking");
// select record from mysql
$sql="SELECT * FROM europa";
$result=mysqli_query($con,$sql);
?>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td colspan="5" bgcolor="#FFFFFF"><strong>Delete data in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Nacja</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>LiczbaPkt</strong></td>
<td align="center" bgcolor="#FFFFFF"> </td>
</tr>
<?php
while($rows=mysqli_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['ID']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Nacja']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['LiczbaPkt']; ?></td>
<td bgcolor="#FFFFFF">delete</td>
</tr>
<?php
// close while loop
}
?>
</table>
<?php
// close connection;
mysqli_close($con);
?>
<?php
$con=mysqli_connect("localhost","root","","kluby ranking");
// get value of id that sent from address bar
$id=$_GET['ID'];
// Delete data in mysql from row that has this id
$sql="DELETE FROM europa WHERE ID='ID'";
$result=mysqli_query($con,$sql);
// if successfully deleted
if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='usuw.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysqli_close($con);
?>
Related
I am using a while loop to parse through my database, and I know that the information is there. It is a forum style setup, and after I add a topic and return to the main page, the new line is added, but still blank. I will include my code, I'm not quite sure what I am missing that would cause it to not bring the data from the DB. Also, I am not using PHPMyAdmin, and I believe this tutorial code was originally written for use with it, if this helps address the difference in syntax I might need to solve the problem. Thanks in advance.
mysql_connect("$host","$username","$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC;";
//order result by descending
$result=mysql_query($sql);
?>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>
<?php
//start looping table row
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id'];?></td>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id'];?>"><?echo $rows['topic']; ?>
</a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>
<?php
// Exit looping and close connection
}
mysql_close();
?>
Test this : replace <? by <?php
So <? echo $rows['view']; ?> by <?php echo $rows['id']; ?> idem view, reply, datetime
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();
?>
I am making a forum from one I borrowed online. I would like the main_forum page to show the most recently posted in topic first, and then the second most recently, etc. My code for the main_forum.php is this:
<?php
require_once 'includes/overall/header.php';
$sql="SELECT * FROM `forum_question` ORDER BY id DESC";
// OREDER BY id DESC is order result by descending
$result=mysql_query($sql);
?>
<h1>Forum</h1>
<table width=700 class="outer">
<tr>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>
<?php
// Start looping table row
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><?php echo $rows['topic']; ?><BR></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['reply']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['datetime']; ?></td>
</tr>
<?php
// Exit looping and close connection
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#E6E6E6"><strong>Create New Topic</strong> </td>
</tr>
</table>
<?php
require_once 'includes/overall/footer.php';
ob_end_flush();
?>
Change your query to retrieve your posts in the order you want them. Taking a guess based on your post I would say your query should look more like:
$sql="SELECT * FROM `forum_question` ORDER BY datetime DESC";
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
I am trying to implement a UI which the user would be able to select entries by selecting checkboxes to delete them from the database. However, while everything looks fine and able to display, there is an error saying Notice: Undefined variable: delete in /opt/uiForm.php on line 154 whereas $delete is not readable. I am following the example given here. Am I missing anything?
<body>
<form name="frm" method="get" action="doSubmit();">
<?php
// Connect to server and select database.
mysql_connect("127.0.0.1:3306", "root", "")or die("cannot connect");
mysql_select_db("PushApplication")or die("cannot select DB");
$sql="SELECT * FROM Device";
$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" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>DeviceID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>DeviceType</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Description</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>OS_Version</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Status</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['DeviceID']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['DeviceType']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Description']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['OS_Version']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Status']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="7" 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 Device 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>
</form>
</body>
</html>
You really should be using
if (!empty($_POST['delete'])) {
instead of
if ($delete) {
to avoid the notice you are getting. Also your code is prone to the so called SQL injection vulnerability which you read upon if you are considering to put it somewhere on the web.
Also you should be using method="post" instead of GET for this type of operations.
I suspect the other example was not displaying all errors.
When checking for the existence of a variable, you need to use if(!empty($delete)), because if returns an error when the variable it's checking does not exist. empty() does not.
Your code supposes that server's PHP has option register_globals = On; Which is not true by default for the late versions of PHP setup. Read this What are register_globals in PHP?