Update query in php correct layout - php

I am having a problem with a update query, I believe I have done this right but have not done it before and its not working so I have gone wrong somewhere but do not know where.
any help will be fantastic.
admin.php
$result = mysql_query("SELECT * FROM bands ");
echo "</br>";
echo "<table border = '1'>
<tr>
<th>Show No</th>
<th>Band Name</th>
<th>Venue</th>
<th>Category</th>
<th>Stock</th>
<th>Edit Show</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<td>" .$row['Band_id']. "</td>";
echo "<td>" .$row['Name']. "</td>";
echo "<td>" .$row['Venue']. "</td>";
echo "<td>" .$row['Category']. "</td>";
echo "<td>" .$row['Stock']. "</td>";
echo ("<td>Edit</td></tr>");
}
echo "</table>";
?>
editband.php
<?php
require 'core/init.php';
$result = mysql_query("SELECT * FROM bands where Band_id ='$Band_id'");
$row = mysql_fetch_array($result);
?>
<form method="post" action="ammenddetails.php">
<input type="hidden" name="id" value="<? echo "$row[Band_id]"?>">
<tr>
<td>Band Name</td>
<td>
<input type="text" name="Name"
size="20" value="<? echo "$row[Name]"?>">
</td>
</tr>
<tr>
<td>Venue</td>
<td>
<input type="text" name="Venue" size="20"
value="<? echo "$row[Venue]"?>">
</td>
</tr>
<tr>
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
?>
ammenddetails.php
<?php
require 'core/init.php';
$result = mysql_query("UPDATE bands
SET Name='$Name',
Venue='$Venue'
WHERE
Band_id='$Band_id'");
mysql_query($result);
header("location:admin.php");
?>

Your ammenddetails.php is missing the POST values
Should be as below-
require 'core/init.php';
$Name = mysql_real_escape_string($_POST["Name"]);
$Venue = mysql_real_escape_string($_POST["Venue"]);
$Band_id = (int)$_POST["id"];
$result = mysql_query("UPDATE bands
SET Name='$Name',
Venue='$Venue'
WHERE
Band_id='$Band_id'");
mysql_query($result);
header("location:admin.php");

require 'core/init.php';
$Name = mysql_real_escape_string($_POST["Name"]);
$Venue = mysql_real_escape_string($_POST["Venue"]);
$Band_id = (int)$_POST["id"];
$query= "UPDATE bands
SET Name='$Name',
Venue='$Venue'
WHERE
Band_id='$Band_id'";
mysql_query($query);
header("location:admin.php");
Try that? You should only need to run the mysql_query function once. Build the query string into a variable and run that variable on the mysql_query function

Related

Edit sql data in php page

I need to edit my database table using submitted data.
This is the form:
mysql_query("set names 'utf8'");
$query = "SELECT * FROM sec1octa";
$result = mysql_query($query);
?>
<div align="center">
<form method="get" action="edit_data.php">
<table width="104" border="1" class="center1">
<tr>
<th width="94">first</th>
<th width="94">second</th>
<th width="94">status</th>
</tr>
<tr>
<?php
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><input type="text" name="id" value="<?php echo $row ['stu_no']; ?> " size=10></td>
<td><input type="text" name="name" value="<?php echo $row ['stu_name']; ?> " size=10></td>
<td><?php
echo '<select name="status">'; {
echo '<option value="open">'.$row['stu_status'].'</option>';
echo '<option value="close">'.prevent.'</option>';
}
echo '</select>';
?></td>
</tr>
<?php
}
}
?>
</tr>
</table>
<input type="submit" name="submit" value="done" />
</form>
The problem is in the edit_data.php page.
I can't UPDATE.
I use this code but it's not working.
require_once('../Connections/config.php');
$id= $_GET['id'];
$status= $_GET['status'];
$query= mysql_query("UPDATE `goh`.`sec1octa` SET `stu_status` = '$status'
WHERE stu_no='".$id."'") or die (mysql_error ());
if($query){echo $status ."done ";}
The reason you are only getting the last values in your edit_data.php $_GET is because you are not setting the input/select names as arrays.
<input type="text" name="id" value="some_stu_no">
is happening over and over and over and every new one overwrites the previous.
Instead, you should use:
<input type="text" name="id[]" value="some_stu_no">
This will allow you to pass multiple id's in a single form submission.
Your form:
<form method="POST" action="edit_data.php">
....
echo "<tr>";
echo "<th>id</th>";
echo "<th>name</th>";
echo "<th>status</th>";
echo "</tr>";
if(mysql_num_rows($result)>0){
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td><input type=\"text\" name=\"id[]\" value=\"{$row['stu_no']}\" size=\"10\"></td>";
echo "<td>{$row['stu_name']}</td>";
echo "<td>";
echo "<select name=\"status[]\">"; // I don't like your option set up here, but I don't fully understand it either.
echo "<option value=\"open\">{$row['stu_status']}</option>";
echo "<option value=\"close\">.prevent.</option>";
echo "</select>";
echo "</td>";
echo "</tr>";
}
}
....
<input type="submit" value="Submit All">
</form>
edit_data.php
// create a mysqli connection called $db
if(isset($_POST['id'])){
$tally=0;
// build all queries for the batch
foreach($_POST['id'] as $index=>$id){
$queries[]="UPDATE `goh`.`sec1octa` SET `stu_status`='".mysqli_real_escape_string($db,$_POST['status'][$index])."' WHERE `stu_no`='".mysqli_real_escape_string($db,$id)."'";
}
// run all queries
if(mysqli_multi_query($db,implode(';',$queries)){
do{
$tally+=mysqli_affected_rows($db);
} while(mysqli_more_results($db) && mysqli_next_result($db));
}
// assess the outcome
if($error_mess=mysqli_error($db)){
echo "Syntax Error: $error_mess";
}else{
echo "$tally row",($tally!=1?"s":"")," updated";
}
mysqli_close($con);
}

PHP submit buttons/structure?

here is a bit of code im working on. Everything works expect that part where i enter ID, then it shows me information in that ID row and also 2 boxes pop where i can write new value of variable. There is also 2 buttons, one which comfirms the edit which has been written into boxes and second one to delete that row from database.
Im still at kinda basics at programming as you can see and im quessing there is something wrong with structure in this part of code?
Can anyone help me to make these 2 submit buttons work as they should.
Thanks you all. :)
if(isset($_POST['idnumber'])) {
$idnumber = protect($_POST['idnumber']);
global $idnumber;
if($idnumber == ""){
echo "Please supply all fields!";
}
else{
?>
<table cellpadding="2" cellspacing="2">
<br />
<tr>
<td width="50px"><b>ID</b></td>
<td width="150px"><b>Current name</b></td>
<td width="200px"><b>Current description</b></td>
<td width="100px"><b>Time added</b></td>
<td width="100px"><b>Current status</b></td>
</tr>
<?php
$get_tasks = mysqli_query($con, "SELECT id FROM task WHERE id='$idnumber'") or die(mysql_error());
while($row = mysqli_fetch_assoc($get_tasks)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
$get_taskname = mysqli_query($con, "SELECT taskname FROM task WHERE id='$idnumber'") or die(mysql_error());
$task_name = mysqli_fetch_assoc($get_taskname);
echo "<td>" . $task_name['taskname'] . "</td>";
$get_description = mysqli_query($con, "SELECT description FROM task WHERE id='$idnumber'") or die(mysql_error());
$description_text = mysqli_fetch_assoc($get_description);
echo "<td>" . $description_text['description'] . "</td>";
$get_dateadded = mysqli_query($con, "SELECT dateadded FROM task WHERE id='$idnumber'") or die(mysql_error());
$date_added = mysqli_fetch_assoc($get_dateadded);
echo "<td>" . $date_added['dateadded'] . "</td>";
$get_status = mysqli_query($con, "SELECT status FROM task WHERE id='$idnumber'") or die(mysql_error());
$cur_status = mysqli_fetch_assoc($get_status);
if($cur_status['status'] == 1) {
echo "<td>" . "Unfinished" . "</td>";
}
else{
echo "<td>" . "Finished" . "</td>";
}
echo "</tr>";
}
if(isset($_POST['edittask'])) {
$editname = protect($_POST['editname']);
mysqli_query($con, "UPDATE task SET taskname=$editname WHERE id='$idnumber'") or die(mysql_error());
}
elseif(isset($_POST['deletetask'])) {
mysqli_query($con, "DELETE FROM task WHERE id='$idnumber'") or die(mysql_error());
}
?>
<form action="test2.php" method="POST" >
<td width="50px"><b></b></td>
<td width="150px"><b>New name: <input type="text" name="editname"/></b></td>
<td width="200px"><b>New desc: <input type="text" name="editdesc"/></b></td>
<td width="100px"><b></b></td>
<td width="100px"><b>D</b></td>
<input type="submit" name="edittask" value="Edit task"/> <br />
<input type="submit" name="deletetask" value="Delete task"/> <br />
</form>
<?php
}
}
?>
</table>
<br /><br />
<form action="test2.php" method="POST" >
Insert ID of task to edit: <input type="text" name="idnumber"/>
<input type="submit" name="ids" value="Find task"/> <br />
</form>

PHP database delete function only works on first row of database?

<p><?php include 'header.php'; ?></p>
<div align="justify">
<td>Name:<input type="text" name="password" ></td> <!-- database -->
<td> Rank:<select>
<!--<option value="volvo">//Database</option>
<option value="saab">Saab</option>
-->
<?php
require ("dbfunction.php");
$con = getDbConnect();
<td> <input type="checkbox" name="vehicle" value="Bike">Group by Rank</td> <!-- database -->
<td> <input type="checkbox" name="vehicle" value="Bike">Include previous service terms</td> <!-- database -->
</div>
<p><table>
<tr>
<th>Name</th>
<th>Rank</th>
<th>Start Date</th>
<th>End Date</th>
<th>Watchkeeping</th>
<th>Active</th>
<th></th>
<th></th>
</tr> <!-- database -->
<tr> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
if (!mysqli_connect_errno($con)) {
$queryStr = "SELECT * " .
"FROM crewlist";
}
$result = mysqli_query($con, $queryStr);
while ($row = mysqli_fetch_array($result)) {
//echo "<div><a href=http://localhost/poshproject/crewlisting.php?crew_name={$row["crew_id"]}>";
echo "<tr>.<th>" . $row["crew_name"] . "<br></br>" . "</th>";
echo "<th>" . $row["crew_rank"] . "</th>";
echo "<th>" . $row["start_date"] . "</th>";
echo "<th>" . $row["end_date"] . "</th>";
echo "<th>" . $row["watchkeeping"] . "</th>";
echo "<th>" . $row["active"] . "</th>";
echo "<td>Edit";
//echo "<td><center><button type=\"submit\" name=\"Delete\" value="' . $row['crew_id'].'"/>Delete</button></center></td>";
echo "<td>Delete";
}
?>
<!--
<td><center><button type="submit" value="Edit">Edit</button></center></td>
<td><center><button type="submit" value="Delete">Delete</button></center></td>-->
</form></tr>
</tr>
</table>
---------------------delete.php---------------------
<?php
//print_r($_GET);
include 'dbfunction.php';
$con = getDbConnect();
if (!mysqli_connect_errno($con)) {
$queryStr = "SELECT * FROM crewlist";
}
$result = mysqli_query($con, $queryStr);
while ($row = mysqli_fetch_array($result)) {
if (!mysqli_connect_errno($con)) {
$sqliQueryStr = "DELETE FROM `posh`.`crewlist` WHERE crew_id = ". $row['crew_id'] . "";
}
mysqli_query($con, $sqliQueryStr);
header('Location: crewlisting.php');
mysqli_close($con);
//echo "user has been deleted";
}
?>
Delete function only works on first row of database. When I delete the rows that are not the first, it deletes the first row instead. Not sure where the error is when I've tried pretty much everything.
I think you are wrong on delete.php file. Put below code in your delete.php file.
---------------------delete.php---------------------
<?php
include 'dbfunction.php';
$con = getDbConnect();
if (!mysqli_connect_errno($con)) {
$sqliQueryStr = "DELETE FROM `posh`.`crewlist` WHERE crew_id = " . $_GET['id'];
mysqli_query($con, $sqliQueryStr);
}
header('Location: crewlisting.php');
mysqli_close($con);
Change your delete query as below
$sqliQueryStr = "DELETE FROM `posh`.`crewlist` WHERE crew_id = " . $_GET['id'] . "";

Selecting rows from mysql table and adding onto another table with checkbox and text inputs?

I have a page that lists all the products bought in a particular consingment. The user can then choose which products to re-order (by clicking on a checkbox) and input a quantity (text input). These are then passed onto another page (listbyconsg.php) that will add the details into a new table, but I can't seem to get this working and would appreciate direction or help. Can anyone help? Thanks.
$con=$_REQUEST['consignment'];
$sql = "SELECT * FROM products,supplier
WHERE products.consignment= $con
AND products.supplier = supplier.supplierID
ORDER BY products.supplier";
$result = mysql_query($sql, $link);
?>
<form action='listbyconsg.php' method='post'>
<?
echo "<table cellpadding='0' cellspacing='0' border='1'>";
echo "<tr> <th>Description</th> <th>Product Size</th> <th>Barcode</th> <th>Product Code</th>
<th>Image</th> <th> # </th> <th> Quantity </th></tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo $row['description'];
echo "</td><td>";
echo $row['productSize'];
echo "</td><td>";
echo $row['barcode'];
echo "</td><td>";
echo $row['productCode'];
echo "</td><td align='center'>";
echo '<img src="http://bargainsupplies4u.co.uk/productpics/' . $row['barcode'] . '.jpeg">';
echo "</td>"; ?>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?= $row['id'] ?>" ></td>
<td>Qty: <input name="quantity[]" type="text" ></td></tr>
<?
}
echo "</table>";
?>
<input type='submit' value='Submit' />
</form>
Then on the other page (listbyconsg.php), I have the following so far....
$get_id=$_POST['checkbox'];
$get_qty=$_POST['quantity'];
I am unsure then as to how to loop through both these arrays and add information into another table with the something thing like this:
$sql1 = "INSERT INTO newOrder (id, orderQuantity)
VALUES ('$get_id', '$get_qty')"
To loop through the arrays in the PHP, you can do something like this:
$count = count($_POST['quantity']);
for($i=0; $i<$count; $i++){
if(isset($_POST['checkbox'][$i])){
$checkbox = $_POST['checkbox'][$i];
$quantity = $_POST['quantity'][$i];
mysql_query("INSERT INTO newOrder (id, orderQuantity)
VALUES ($checkbox, $quantity)");
}
}

Link (on a while loop on a search query) inside a search form

echo "<center><form name = 'searching' method='POST' action='cpanel.php?manage=".$useraccounts."&rcad=".$viewcustomer."'><table border = 1>";
echo "<select name = 'filter'>";
echo "<option value ='username'>Username</option>";
echo "<option value ='username'>Username</option>";
echo "</select>";
echo " <input name='search' type='text' > ";
echo "<input type='submit' name='submit' value='Search'> ";
echo "<input type='submit' name='back' value='Back'><br><br>";
echo "<tr>";
echo "<td>User ID</td>";
echo "<td>Username</td>";
echo "<td>Last Name</td>";
echo "<td>First Name</td>";
echo "<td>Middle Initial</td>";
echo "<td>Address</td>";
echo "<td>Contact Number</td>";
echo "<td>Birthday</td>";
echo "<td>Date Registered</td>";
echo "<td> </td>";
echo "</tr>";
$submit = $_POST['submit'];
if(isset($submit)) {
$search = $_POST['search'];
$filter = $_POST['filter'];
include "dbconnect.php";
if ($search != NULL) {
$searchquery = mysql_query("SELECT * FROM register WHERE $filter LIKE '%$search%'");
while($fetchres = mysql_fetch_array($searchquery)) { //show search results
$userid = $fetchres['userid'];
$username = $fetchres['username'];
$lname = $fetchres['lname'];
$fname = $fetchres['fname'];
$mi = $fetchres['mi'];
$address = $fetchres['address'];
$contact = $fetchres['contact'];
$month = $fetchres['month'];
$day = $fetchres['day'];
$year = $fetchres['year'];
$dateregistered = $fetchres['date'];
$sendmessage = "<a href = 'cpanel.php?manage=".$useraccounts."&rcad=".$viewcustomer."&user=".$username."'>Send message</a>";
echo "<tr>";
echo "<td>$userid</td>";
echo "<td>$username</td>";
echo "<td>$lname</td>";
echo "<td>$fname</td>";
echo "<td>$mi</td>";
echo "<td>$address</td>";
echo "<td>$contact</td>";
echo "<td>$month $day, $year</td>";
echo "<td>$dateregistered</td>";
echo "<td>$sendmessage</td>";
echo "</tr>";
echo "$table";
if (isset($sendmessage)) {
$getuser = $_GET['user'];
if ($getuser == $username) {
//start send message
$touser = $username;
$fromuser = $adminsess;
$subject = $_POST['subject'];
$message = $_POST['message'];
$submit = $_POST['submit'];
$date = date("Y-m-d");
$rand = rand(98765432,23456789);
$table = '<center><script type="text/javascript" src="/js/sendmessage.js"></script>
<form action="cpanel.php?manage='.$useraccounts.'&rcad='.$viewcustomer.'&user='.$username.'&send='.$one.'" method="post" name="sendpm" onsubmit="return valid()">
<table>
<tr>
<td>
To:
</td>
<td>
'.$touser.'
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<input type="text" name="subject" id="subj1" />
</td>
</tr>
<tr>
<td>
Message:
</td>
<td>
<textarea name="message" cols="60" rows="10" id="mes1"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name = "submit" value="Submit" />
</td>
</tr>
</table>
</form></center>';
if (isset($submit))
{
$send = $_GET['send'];
if ($send == $one) {
include "maildbconnect.php";
$query = mysql_query("INSERT INTO mailtbl_admin VALUES ('', '$touser', '$fromuser', '$subject',
'$message', '0', '0', '1', '$date', '$rand')");
echo "Message successfully sent.";
}
}
}//end send message
}//end $getuser
}
}
The form for sending a message ($table) doesn't appear after clicking the link ($sendmessage) on a certain search result. After clicking the link, i'm prompted to an empty table (table not inside the while loop) but the $_GET function is working. Can anyone tell me how to fix this? thanks a lot
$submit wont be set when following the link. So any code after:
if(isset($submit)) {
Will not fire.
You've got a lot wrong with this form. First off, you forgot a <tr> and <td> after your <table> tag.
echo "<center><form name = 'searching' method='POST' action='cpanel.php?manage=".$useraccounts."&rcad=".$viewcustomer."'><table border = 1><tr><td colspan='10'>";
echo "<select name = 'filter'>";
echo "<option value ='username'>Username</option>";
echo "<option value ='username'>Username</option>";
echo "</select>";
echo " <input name='search' type='text' > ";
echo "<input type='submit' name='submit' value='Search'> ";
echo "<input type='submit' name='back' value='Back'><br><br>";
echo "</td></tr>";
Alternatively you could move the opening <table> tag below the <select> tag.
After you fix that, try moving the logic where you insert the values into mailtbl_admin outside of the loop. I'm guessing you're only going to want to send the message to one user, so it only makes sense to move it out of the loop.

Categories