PHP: Two buttons to submit - php

I've been trying to submit two buttons, one to delete and one to edit/add. But I can't seem to get it right.
HTML:
<!-- Edit/Delete Account Form -->
<form method="get" action="edit.php" onsubmit="setTimeout('location.reload()');">
<div id="edit-form" class="modal hide fade">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4><?php _e('Edit/Delete Account'); ?></h4>
</div>
<div class="modal-body">
<div id="message"></div>
<div class="control">
<div id="edit-account-form">
<table id="add-account" class="table">
<tbody>
<tr>
<td valign="middle">
Account Number
</td>
<td>
<input type="hidden" id="editid" name="editid" value=""/>
<input type="text" id="number" name="number" value="Enter Some text here "/>
</td>
</tr>
<tr>
<td>
Account Description
</td>
<td>
<input type="text" id="description" name="description" value="Enter Some text here "/>
</td>
</tr>
<tr>
<td>
Level 01
</td>
<td>
<input type="text" id="level01" name="level01" value="Enter Some text here "/>
</td>
</tr>
<tr>
<td>
Level 02
</td>
<td>
<input type="text" id="level02" name="level02" value="Enter Some text here "/>
</td>
</tr>
<tr>
<td>
Level 03
</td>
<td>
<input type="text" id="level03" name="level03" value="Enter Some text here "/>
</td>
</tr>
<tr>
<td>
Level 04
</td>
<td>
<input type="text" id="level04" name="level04" value="Enter Some text here "/>
</td>
</tr>
<tr>
<td>
Tax
</td>
<td>
<input type="text" name="account" value="Enter Some text here "/>
</td>
</tr>
<tr>
<td>
Allow General Journals
</td>
<td>
<select>
<option selected disabled>Select</option>
<option>Yes</option>
<option>No</option>
</select>
</td>
</tr>
<tr>
<td>
Allow Payments
</td>
<td>
<select>Select</select>
</td>
</tr>
<tr>
<td>
Allow Expense Claims
</td>
<td>
<select>Select</select>
</td>
</tr>
<tr>
<td>
Show On Dashboard
</td>
<td>
<select>Select</select>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" data-complete-text="<?php _e('Done'); ?>" name="add" class="btn btn-primary pull-right"><?php _e('Submit'); ?></button>
<p class="pull-left"><button type="submit" name="delete" class="btn btn-primary pull-right"><?php _e('Delete'); ?></button></p>
</div>
</div>
</form>
PHP:
<?php
//Account Details
$number = $_GET['number'];
$description = $_GET['description'];
$level01 = $_GET['level01'];
$level02 = $_GET['level02'];
$level03 = $_GET['level03'];
$level04 = $_GET['level04'];
$id = $_GET['editid'];
$dbc = mysqli_connect('localhost', 'root', 'root', 'accounting') or die('Connection error!');
if(isset($_POST['delete'])) {
$check = "DELETE FROM accountSlave WHERE `id` = '$id' ";
mysqli_query($dbc, $check) or die('Database error, delete account!');
}
if(isset($_POST['add'])) {
//$check = "DELETE FROM accountSlave WHERE `id` = '$id' ";
$check = "UPDATE accountSlave SET `accountNumber` = '$number', `accountDescription` = '$description', `accountLevel1` = '$level01', `accountLevel2` = '$level02', `accountLevel3` = '$level03', `accountLevel4` = '$level04' WHERE `id` = '$id'";
mysqli_query($dbc, $check) or die('Database error, add account!');
}
header('location:master-accounts.php');
?>
It works when I have one query. I've tried one query to add and to delete and it worked. But I need to submit a delete if delete button clicked and vice versa.
I followed this post to try get it right multiple buttons on a form
Any help or suggestions will be appreciated!

You need to use $_GET instead of $_POST. It should be like
if(isset($_GET['delete'])) {
and
if(isset($_GET['add'])) {
Because your form submit method is GET not the POST

You have to check which button is pressed and according to that do your operation.
Also you have to use $_GET not $_POST because your form method is GET.
if(isset($_GET['add'])) {
// code for add
} elseif(isset($_GET['delete'])) {
// code for delete
}

Related

Table Showing even in modal but the paragraph doesnt show

This is my code for my modal. im having a problem on my table because it is still showing. but when its only the paragraph tag, the words inside there are not showing. i already tried putting the table inside the p tag but still showing . what could i possibly do?
<!-- Modal for CONFIRM-->
<div class="modal fade" id="ModalConfirm<?php echo $id;?>" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-body">
<p>Are you sure you want to confirm request?<?php echo $id?> </p>
<?php
$modalquery="select * from tbl_informant,tbl_missing_person,tbl_contact_person
where concat(reported_id=mp_id && mp_id=cp_id) && informant_id=".$id;
$modalresult=mysql_query($modalquery);
while ($row=mysql_fetch_array($modalresult)) {
?>
<form method="POST" action="">
<table border=1 width="120%" style="margin-left: 60px;">
<tr >
<td colspan="3" height="40px">
<center>
<b>INFORMANTS INFORMATION</b>
</center>
</td>
</tr>
<tr>
<td>
<b>Informants Name</b>
</td>
<td>
<?php echo $row['informant_fname'].' '.$row['informant_lname'];?>
</td>
</tr>
<tr>
<td>
<b>Address:</b>
</td>
<td colspan="2">
<?php echo $row['informants_address'];?>
</td>
</tr>
<tr>
<td>
<b>Contact</b>
</td>
<td >
<input type="number" name="informant_contact" placeholder="Enter Your Number"></input>
</td>
<td>
<input type="email" name="informant_email" placeholder="Enter Your Email"></input>
</td>
</tr>
</table><br><br>
<table border=1 width="120%" style="margin-left: 60px;">
<tr>
<td colspan="3" height="40px" >
<center>
<b>SIGHTING INFORMATION</b>
</center>
</td>
</tr>
<tr>
<td>
<b>Upper Apparel</b>
</td>
<td>
<?php echo$row['si_upper'];?>
</td>
<td>
<b>Lower Apparel</b>
</td>
<td>
<?php echo$row['si_lower'];?>
</td>
</tr>
<tr>
<td >
<b>Last Location Seen:</b>
</td>
<td colspan="2">
<center>
<textarea cols="40" rows="3" placeholder="Enter the Last Location You've seen him/her" name="si_location"></textarea>
</center>
</td>
</tr>
<tr>
<td>
<b>
Childs Last Action/ Doing
</b>
</td>
<td colspan="2">
<center>
<textarea cols="40" rows="3" placeholder="Enter The Childs Last Activity" name="si_activity"></textarea>
</center>
</td>
</tr>
<tr>
<td colspan="3" height="50px">
<center>
<input type="submit" name="btnSubmit" value="SUBMIT"></input>
</center>
</td>
</tr>
</table><br><br><br>
</form>
<?php
}
?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" >Yes</button>
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
</div>
</div>
this is all of the code that my confirm button should have but when it has tables the yes or no button also shows outside the modal.

submit button to store value in database without using form action php

I would like click on submit and the value input in the field to be stored in database.
However, I do not want to use a form action. Is it possible to do it without creating form action with PHP?
<tr>
<form method="post">
<tr>
<td>
<label for="Item name"><b>Finish Product:</b></label>
</td>
<td>
<input id="finish_product" type="text" maxlength="100" style="width:100px"name="finish_product" required>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Save" id="submit" />
</td>
</tr>
<?php
if(isset($_POST['submit']))
{
var_dump($_POST); exit;
$SQL = "INSERT INTO bom (finish_product) VALUES ('$finish_product')";
$result = mysql_query($SQL);
}?>
</tr>
However, I do not want to use a form action. Is it possible to do it
without creating form action with PHP?
No, that's not possible.
Just do it like this:
<tr>
<form method="post" action="mypage.php">
<tr>
<td>
<label for="Item name"><b>Finish Product:</b></label>
</td>
<td>
<input id="finish_product" type="text" maxlength="100" style="width:100px"name="finish_product" required>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Save" id="submit" />
</td>
</tr>
</form>
<?php
if(isset($_POST['finish_product']))
{
var_dump($_POST); exit;
$SQL = "INSERT INTO bom (finish_product) VALUES ('$finish_product')";
$result = mysql_query($SQL);
}?>
</tr>

sent dynamic table to mysql

I am trying to send my dynamic table to mysql database but I am having difficulties. I've tried using a for to get it to send but I'm not getting very far. At the moment I am just displaying it but I really want to send it to mysql. I want all the data to be in one row with the rest of the data like test case name, test case number and so on. But the dynamic table must be put into the same row where the rest of the data goes.
Below you will see what I have done and maybe you can see what my intentions are...
<div class="col-md-6">
<label>Test Case Number:</label>
<?php
$sql = "SELECT test_case_number FROM qa_testing_application ORDER BY id desc LIMIT 1";
$result = mysqli_query($database, $sql) or trigger_error("SQL", E_USER_ERROR);
while ($row = mysqli_fetch_assoc($result)) {
?>
<input class="form-control" style="display: none" type="text" name="test_case_number" readonly="readonly" value="<?php echo $row['test_case_number']+3?>">
<?php } ?>
<label>Company Name:</label>
<input class="form-control" type="text" name="test_case_company_name"/>
<label>Tester:</label>
<input class="form-control" style="display: none" type="text" name="user_username" value="<?php echo $user_username ?>" readonly/>
<label>System:</label>
<input class="form-control" type="text" name="test_case_system"/>
<label>URL:</label>
<input class="form-control" type="text" name="test_case_url"/>
</div>
<div class="col-md-12" style="border: 1px solid #28415b; padding-bottom: 12px; padding-top: 12px;margin-top: 20px; margin-bottom: 15px">
<p>
<INPUT class="btn btn-primary ladda-button" type="button" value="Add row" onclick="addRow('dataTable')" />
<INPUT class="btn btn-primary ladda-button" type="button" value="Delete row" onclick="deleteRow('dataTable')" />
</p>
<div class="clear"></div>
<p>'All fields below are compatible to use markdowns for editing' </p>
<table class="table table-hover">
<thead>
<tr>
<th style="width: 15px">Chk</th>
<th style="width: 335px">Action:</th>
<th style="width: 326px;">Expected System Response:</th>
<th style="width: 151px;">Pass/ Fail</th>
<th>Comment</th>
</tr>
</thead>
</table>
<table id="dataTable" class="table table-hover">
<tbody>
<tr>
<td style="width:20px;"><INPUT type="checkbox" name="chk[]" id="chk"/></td>
<td><INPUT class="form-control" type="text" name="step[]" autocomplete="on" placeholder="Action" required/></td>
<td><INPUT class="form-control" type="text" name="url[]" autocomplete="on" placeholder="Expected Outcome" required/></td>
<td>
<select name="passfail[]" class="form-control" style="width:120px;">
<OPTION value="Pass">....</OPTION>
<OPTION value="Pass">Pass</OPTION>
<OPTION value="Fail">Fail</OPTION>
</select>
</td>
<td>
<TEXTAREA class="form-control" type="text" name="comment[]" rows="2" cols="15" placeholder="Comment" required></TEXTAREA>
</td>
</tr>
</tbody>
</table>
Thats my table that i am dynamically making...
In my PHP file i display the dynamic table as follows:
<table class="table table-bordered">
<thead>
<tr>
<td>Step</td>
<td>process</td>
<td>Expected System Response</td>
<td>
<center>Pass/ Fail</center>
</td>
<td>Comment</td>
</tr>
</thead>
<?php
if (isset($_POST)) {
$step = $_REQUEST['step'];
$url = $_REQUEST['url'];
$pass_fail = $_REQUEST['passfail'];
$comment = $_REQUEST['comment'];
$countPass = 0;
$countFail = 0;
foreach ($step as $key => $row) {
?>
<tbody>
<tr>
<td><?php echo $key + 1; ?></td>
<td><?php echo $step[$key]; ?></td>
<td><?php echo $url[$key]; ?></td>
<td style="color:<?php if ($pass_fail[$key] == 'Fail') {
echo 'color: red';
} else {
echo 'limegreen';
} ?>"><b>
<center><?php echo $pass_fail[$key]; ?></center>
</b></td>
<td><?php echo $comment[$key]; ?>
</td>
</tr>
</tbody>
</table>
Now how do i insert it into mysql???
So basically what you need to do to insert it into your database is doing a sql query in PHP.
Let me show you an code example:
//establish db connection
$con=mysqli_connect("dbhost","username","dbpassword","dbname");
$sql = "INSERT INTO tablename (name_of_row1,
name_of_row2,
name_of_row3)
VALUES ('".$value1."',
'".$value2."',
'".$value3."')";
mysqli_query($con, $sql);
mysqli_close($con);
So you have to open a sql connection and then insert your data to the table you want. I hope this it's clear enough. Now if the PHP file gets called the SQL query is beeing made.

avoiding duplicate entries and caching entered form values

i have some code that controls duplicate entries in particular the USER ID. it checks in the database at submit and if that USER ID exists already it gives that notification. now the problem is when i submit and if that USER ID entered already exists in the database, all the other entries on the form are cleared, prompting me to re_enter all the other details again. i find this annoying and retrogressive. i want some help on how better i can do it such that only the USER ID text box returns empty, keeping other details safe/unchanged or indeed alternatively keeping/buffering/caching all details previously entered so that i can also review the duplicate USER ID before changing it.
new_user.php
<h1 align="center">Create New User</h1>
<p align="center" style="color:#F00"><?php if(isset($_GET['dup'])){ echo "That User ID Already Exists!"; } ?> </p>
<form id="form1" method="post" action="add_user.php">
<table width="100%">
<tr>
<td width="204"><div align="right">User ID:</div></td>
<td width="286">
<input type="text" name="user_id" id="user_id" />
</td>
</tr>
<tr>
<td><div align="right">Full Names:</div></td>
<td>
<input type="text" name="fname" id="fname" />
</td>
</tr>
<tr>
<td><div align="right">Gender:</div></td>
<td><select id="sex" name="sex">
<option selected="selected" value="male">Male</option>
<option name="female">Female</option>
</select></td>
</tr>
<tr>
<td><div align="right">NRC Number:</div></td>
<td>
<input type="number" name="nrcno" id="nrcno" min="1" />
</td>
</tr>
<tr>
<td><div align="right">Phone Number:</div></td>
<td>
<input type="number" name="cellno" id="cellno" />
</td>
</tr>
<tr>
<td><div align="right">Email Address:</div></td>
<td>
<input type="email" name="emailad" id="emailad" />
</td>
</tr>
<tr>
<td><div align="right">Position Held:</div></td>
<td>
<input type="text" name="posh" id="posh" />
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="create" id="create" value="Add User" /></td>
</tr>
</table>
</form>
add_user.php
<?php
$user_id=$_POST['user_id'];
$fname = $_POST['fname'];
$sex= $_POST['sex'];
$name= $_POST['name'];
$nrcno = $_POST['nrcno'];
$cellno= $_POST['cellno'];
$emailad = $_POST['emailad'];
$posh = $_POST['posh'];
require("get_func.php");
checkID($id);
include("connect.php");
mysql_select_db("ceec", $con);
$query = "INSERT INTO user VALUES ('$user_id', '$fname', '$sex','$name', '$nrcno', '$cellno', '$emailad', '$posh')";
if (mysql_query($query)){
header("Location: success.php");
}
else {echo "Nada" . mysql_error(); }
mysql_close($con);
?>
get_func.php
<?php
function checkID($id){
include_once("connect.php");
mysql_select_db("ceec",$con);
$query = "SELECT * FROM user WHERE user_id = '$id'";
$result= mysql_query($query);
if($row = mysql_fetch_array($result))
{
header("Location: new_user.php?dup=true");
break;
}
else {}
}
?>
<input type="text" name="user_id" id="user_id"
<?php if(isset($_POST['user_id'])){echo htmlentities($_POST['user_id'];} ?>/>

Cannot update SQL database

So I created an edit profile page and everything went fine. But later on I wanted to use similar code to update another thing inside my database. Its a very odd bugg because when I add to many values that need to be update it refuse to work.
<?php
session_start();
if(isset($_POST['submit'])){
$Creator = $_SESSION['username'];
$dbName = $_POST['Name'];
$dbBase_damage = $_POST['Base_damage'];
$dbDamage = $_POST['Damage'];
$dbPellets = $_POST['Pellets'];
$dbAttackspeed = $_POST['Attackspeed'];
$dbRS = $_POST['RS(first)'];
$dbRS2 = $_POST['RS(consecutive)'];
$dbLoaded = $_POST['Loaded'];
$dbReserve = $_POST['Reserve'];
$dbCritical = $_POST['Critical'];
$dbAbility = $_POST['Ability'];
$dbPiece_set = $_POST['Piece_set'];
$dbBonus_set = $_POST['Bonus_set'];
$dbDescription = $_POST['Description'];
if ($dbName && $dbBase_damage && $dbDamage && $dbAttackspeed && $dbLoaded && $dbCritical) {
mysql_connect ("localhost", "root", "") or die ("Could not connect");
mysql_select_db("weapons") or die ("Could not connect to the database");
$wepquery = mysql_query("SELECT * FROM weapon WHERE Creator='$Creator' AND Name='$dbName'") or die ("The query could not be completed, please try again later!");
if (mysql_num_rows($wepquery) != 0) {
mysql_query ("
UPDATE weapon SET Base_Damage='$dbBase_damage', Damage='$dbDamage',
Pellets='$dbPellets', Attackspeed='$dbAttackspeed', RS(first)='$dbRS', RS(consecutive)='$dbRS2',
Loaded='$dbLoaded', Reserve='$dbReserve', Critical='$dbCritical', Ability='$dbAbility', Piece_set='$dbPiece_set',
Bonus_set='$dbBonus_set' Description='$dbDescription' WHERE Creator='$Creator' AND Name='$dbName'
") or die ("Could not update!");
echo "It just werks!";
}else echo ("That username could not be found!");
}else echo ("Something went wrong!");
}
?>
When I use this code I get an error that 'Base_Damage' is undefined index. But when I remove 'Base_Damage', it can not update at all. I have double checked the database and created 'weapon ideas' with it without problems. But for some reason when I reduce the amount of values that need to be update it works just fine. If you wonder about the "$Creator = $_SESSION['username'];", I get the Creator username depending on who is logged in from "nav.php"
My html code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1.0" />
<title>TF2 NUT</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<div id="Container">
<div id="header">
<h1>Welcome to TF2 NUT!</h1>
</div>
<div id="nav">
<?php
include_once "nav.php";
?>
</div>
<div id="Content">
<h2>Update your submissions</h2>
<form action="edit_sub.php" method="POST">
<table>
<tr>
<td>
Choose weapon to edit:
</td>
<td>
<input type="text" name="Name" id="Name" />
</td>
</tr>
<tr>
<td>
Base damage:
</td>
<td>
<input type="text" name="Base_damage" id="Base_damage" />
</td>
</tr>
<tr>
<td>
Damage:
</td>
<td>
<input type="text" name="Damage" id="Damage" />
</td>
</tr>
<tr>
<td>
Pellets:
</td>
<td>
<input type="text" name="Pellets" id="Pellets" />
</td>
</tr>
<tr>
<td>
Attackspeed:
</td>
<td>
<input type="text" name="Attackspeed" id="Attackspeed" />
</td>
</tr>
<tr>
<td>
Reload speed:
</td>
<td>
<input type="text" name="RS(first)" id="RS(first)" />
</td>
</tr>
<tr>
<td>
Reload(consecutive):
</td>
<td>
<input type="text" name="RS(consecutive)" id="RS(consecutive)" />
</td>
</tr>
<tr>
<td>
Loaded:
</td>
<td>
<input type="text" name="Loaded" id="Loaded" />
</td>
</tr>
<tr>
<td>
Reserve:
</td>
<td>
<input type="text" name="Reserve" id="Reserve" />
</td>
</tr>
<tr>
<td>
Critical:
</td>
<td>
<input type="text" name="Critical" id="Critical" />
</td>
</tr>
<tr>
<td>
Ability:
</td>
<td>
<input type="text" name="Ability" id="Ability" />
</td>
</tr>
<tr>
<td>
Piece set:
</td>
<td>
<input type="text" name="Piece_set" id="Piece_set" />
</td>
</tr>
<tr>
<td>
Bonus set:
</td>
<td>
<input type="text" name="Bonus_set" id="Bonus_set" />
</td>
</tr>
</table>
Description:
<br />
<textarea style="resize: none;" rows="8" cols="40" id="Descripton" name="Description"></textarea>
<br />
<input type='submit' name="submit" value='Update' />
</form>
</div>
<div id="aside">
</div>
<div id="footer">
<h6>Responsible for the stuff on this site is Anton Magnusson</h6>
<h6>If I have done something wrong(I hope not ;_;) just contact me: anton_peace#hotmail.com</h6>
</div>
</div>
Anyone can help? :P
i guess your if statment is not readable and thats why you couldnt update
first your name name="Base_damage" ia not same as your variable index $dbBase_Damage = $_POST['Base_Damage'];
it should be
$dbBase_Damage = $_POST['Base_damage']; // with small d
try replave this also
if ($dbName && $dbBase_Damage && $dbDamage && $dbAttackspeed && $dbLoaded && $dbCritical)
edit:
in your query replace this
RS(first)='$dbRS', RS(consecutive)='$dbRS2'
by
`RS(first)`='$dbRS', `RS(consecutive)`='$dbRS2'
EDIT2:
please replace
or die ("Could not update!");
with
or die(mysql_error()); // like that you can see what error or what goes wrong.
EDIT3.
Bonus_set='$dbBonus_set' , Description=
^^//you missed coma here

Categories