How to make value in form POST to same page? - php

I have to make everything happened on the same page. I have used action="<?PHP echo $_SERVER['PHP_SELF']; ?>" here but it is not working. I have insert the PHP query below the form. Basically, my question is how do I make sure the form is posting the values on the same page. If it is updated, a pop up will come up.
$user_id=$_SESSION['user_id'];
$date = date("l jS \of F Y h:i:s A");
$query1 ="SELECT daily_limit FROM user WHERE user_id='$user_id'";
$result1 = mysqli_query($link, $query1) or die(mysqli_error($link));
while ($row1 = mysqli_fetch_array($result1)) {
$dailylimit=$row1['daily_limit'];
}
$query2 = "SELECT SUM(debit) AS debited_today FROM transaction WHERE user_id = '$user_id' AND date = CURRENT_DATE" ;
$result2 = mysqli_query($link, $query2) or die (msqli_error($link));
while ($row2 = mysqli_fetch_array($result2)){
$debited_today = $row2['debited_today'];
}
// form
<form method="POST" action="<?PHP echo $_SERVER['PHP_SELF']; ?>" >
<table id="table">
<tr>
<td class="alt">Existing Daily Limit</td>
<td>S$ <?php echo $dailylimit; ?> </td>
<input type="hidden" name="dailylimit" value="<?php echo $dailylimit ?> "/>
</tr>
<tr>
<td class="alt"><label for="newdailylimit">New Daily Limit</label></td>
<td>$ <select name="newdailylimit">
<option value="100.00">100.00</option>
<option value="500.00">500.00</option>
<option value="1000.00">1000.00</option>
<option value=5000.00">5000.00</option>
</select></td>
</tr>
<tr>
<td class="alt">Amount Debited Today</td>
<td>S$ <?php echo $debited_today; ?></td>
</tr>
<tr>
<td class="alt">Amount Debited Left</td>
<td>S$ <?php echo ($dailylimit - $debited_today); ?> </td>
</tr>
</table>
<br/>
<input type="submit" name="submit "value="Submit"></input>
</form>
// Values I need to POST
$dailylimit = $_POST['dailylimit'];
$newdailylimit = $_POST['newdailylimit'];
if ($dailylimit != $newdailylimit){
$query = "UPDATE user SET daily_limit='$newdailylimit' WHERE user_id='$user_id'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
echo "<script>alert('You have successfully updated your daily limit');</script>";
}
else if ($dailylimit == $newdailylimit){
echo "<script>alert('You have selected the same daily limit as your previous one. Please choose a different one. ');</script>";
}
else{
}

Ahh I may have found out what the problem is:
You have a space after the word submit and no space before value.
This will prevent your form from submitting.
<input type="submit" name="submit "value="Submit"></input>
---^ ^
Change this to:
<input type="submit" name="submit" value="Submit">
The double quote might mess things up.
Other things that are wrong but won't fix your problem
Also
<option value=5000.00">5000.00</option>
should be
<option value="5000.00">5000.00</option>
Also
<input type="hidden" name="dailylimit" value="<?php echo $dailylimit ?> "/>
// You have an extra space here ^
Which will change your $dailylimit, and append it with a space.

make your action=''. it will post to itself.
then on top of your page check if the request is post ex. if($_POST){//add your code}else{//yourform}

Related

how do fetching and then inserting from multi check boxes in php

I am going to fetching table values in a html table along checkbox in each row and then inserting values in another database table from multi check boxes in php.
Only the values of checked boxes should be submitted to that table.
db name "laboratory":
test: fetching values.
package: inserting table.
view
Status
Active
Inactive
<?php
$conn=mysqli_connect("localhost","root","","laboratory") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$query="SELECT * FROM test";
$result=mysqli_query($conn,$query);
if ($result) {
while ($record=mysqli_fetch_array($result)) {
Please try to follow this code and implement in your program . Hope that this will cooperate you much
if(isset($_POST['name'])){
$name = $_POST['name'];
$status = $_POST['status'];
if(empty($name) || empty($status)){
echo "Field Must Not be empty";
} else{
$conn=new mysqli("localhost","root","","test");
if($conn){
$query = "SELECT * FROM userdata limit 5";
$stmt = $conn->query($query);
$val = '<form action="" method=""> ';
$val .= '<table> ';
if ($stmt) { ?>
<form action="" method="post">
<table>
<?php while ($result=$stmt->fetch_assoc()) { ?>
<tr>
<td><?php echo $result['post']; ?></td>
<td><input value="<?php echo $result['post']; ?>" type="checkbox" name="check[]" /></td>
</tr>
<?php } ?>
<tr>
<td>Actual Price </td>
<td>Discount</td>
<td>Final Price</td>
</tr>
<tr>
<td><input type="text" name="actual"/></td>
<td><input type="text" name="discount"/></td>
<td><input type="text" name="final"/></td>
</tr>
<tr>
<td>Description</td>
<td><textarea name="description" id="" cols="30" rows="10"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="Cancel" /></td>
</tr>
</table>
</form>
<?php }} }}?>
<?php
if(isset($_POST)){
echo "<pre>";
print_r($_POST);
echo "<pre>";
}
?>`enter code here`
First of all you have to decide that what are you using either mysqli or mysql, if you are using mysqli then you have to improve your code
$query="SELECT * FROM test";
$result=mysqli_query($conn,$query);
if ($result) {
while ($record=mysqli_fetch_array($result)) {
and when you want to insert the checked data will be inserted in package table. If package table in another database then you have to give us the full detail i mean tell us the database name of package table.

Updating a field from an sql table from php

I have a table with more columns. On one column I have 3 buttons for each row with different queries associated. When I click one of the buttons, field in the table from my database should update.The problem is that when I click on a button on any row in the table, it updates only the field from the first row.For example, I have a table with 10 rows.If I click one of the buttons on the 10th row IT will update my first row, not the 10th one as I want. Is there any possibility to solve this?
This is the code..I'm sorry it is too long:
<?php
$query = "SELECT * FROM masculin ORDER BY id_concurent";
$result = mysqli_query($link ,$query);
$row = mysqli_fetch_assoc($result);
if(isset($_POST['locul1']))
{
$sql = "UPDATE masculin SET Premiu=1 WHERE CNP='".$row['CNP']."'";
mysqli_query($link,$sql);
header("Location:administrare.php");
}
else if(isset($_POST['locul2']))
{
$sql = "UPDATE masculin SET Premiu=2 WHERE CNP='".$row['CNP']."'";
mysqli_query($link,$sql);
header("Location:administrare.php");
}
else if(isset($_POST['locul3']))
{
$sql = "UPDATE masculin SET Premiu=3 WHERE CNP='".$row['CNP']."'";
mysqli_query($link,$sql);
header("Location:administrare.php");
} ?>
<u><i><h1 align="center">Administrare concurenti</h1></i></u>
<u><i><h2>MASCULIN</h2></i></u>
<?php
$query = "SELECT * FROM masculin ORDER BY id_concurent";
$result = mysqli_query($link ,$query);
if (mysqli_num_rows($result) == 0) {
echo 'Inca nu s-a inscris niciun concurent.';
} else {
?>
<table width="100%">
<tr>
<th>Nr.<br />concurs</th>
<th>Nume</th>
<th>Prenume</th>
<th>CNP</th>
<th>Categoria</th>
<th>Varsta</th>
<th>Premiu</th>
<th>Modifica<br />rezultat</th>
<th>Descalifica</th>
</tr>
<?php while($row = mysqli_fetch_assoc($result)){ ?>
<tr>
<?php
$query1="SELECT id_concurent FROM concurenti WHERE CNP='".$row['CNP']."'";
$result1=mysqli_query($link,$query1);
$nr_conc=mysqli_fetch_assoc($result1);
?>
<td> <?php echo $nr_conc['id_concurent'] ?> </td>
<td> <?php echo $row['Nume'] ?> </td>
<td> <?php echo $row['Prenume'] ?> </td>
<td> <?php echo $row['CNP'] ?> </td>
<td> <?php echo $row['Categorie'] ?> </td>
<td> <?php echo $row['Varsta'] ?> </td>
<td> <?php echo $row['Premiu'] ?> </td>
<td>
<form action="administrare.php" method="post">
<input type="submit" name="locul1" value="Premiul 1">
<input type="submit" name="locul2" value="Premiul 2">
<input type="submit" name="locul3" value="Premiul 3">
</form>
</td>
</tr>
<?php } ?>
</table> <?php } ?>
for updated the da tbale row when you submit the values related to a html table row you should add and hidden input with the value that let you reach the correct row eg:
<form action="administrare.php" method="post">
<input type="submit" name="locul1" value="Premiul 1">
<input type="submit" name="locul2" value="Premiul 2">
<input type="submit" name="locul3" value="Premiul 3">
<input type="hidden" name="CNP" value="<?php echo $row['CNP'] ?>">
</form>
and in your sql updated query add the POST value related to the row
$sql = "UPDATE masculin SET Premiu=1 WHERE CNP='".$_POST['CNP']. "'";

Blank screen when updating data in php table

I have been working on a project and i am at the final stages of the project. My problem is whenever i try to update data in my database table into returns a blank screen with no error messages. Please find the php script and html form (the form responsible for updating the database table) below, i have divided it into about four sections:
Thanks in advance
Update Form:
<a name="inventoryEditForm" id="inventoryEditForm"></a>
<h3>↓Add New Question Form↓</h3>
<form action="inventory_edit.php" enctype="multipart/from-data" name="myForm" id="myForm" method="post">
<table width="80%" border="0" cellspacing="3" cellpadding="7">
<tr>
<td width="20%"> </td>
<td width="80%"> </td>
</tr>
<tr>
<td>Question</td>
<td><textarea rows="" name="question" cols=""><?php echo $question; ?></textarea></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Venue</td>
<td><input type="text" name="venue" maxlength="50" value="<?php echo $venue; ?>"></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Date</td>
<td><input type="date" name="questiondate" value="<?php echo $date; ?>"></td>
</tr>
</table>
<br>
<input name="thisID" type="hidden" value="<?php echo $targetID; ?>"/>
<input type="submit" name="submit" value="Update Question">
<input type="reset" name="clear" value="Clear Form">
</form>
PHP Script:
<?php
//Error reporting due to long script
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
error_reporting(E_PARSE);
//Update question table
If (isset($_POST['question'])) {
$id = mysqli_real_escape_string($link, $_POST['thisID']);
$question = mysqli_real_escape_string($link, $_POST['question']);
$venue = mysqli_real_escape_string($link, $_POST['venue']);
$date = mysqli_real_escape_string($link, $_POST['questiondate']);
//Update question in the table
$sql = mysqli_query($link, "UPDATE DebateQuestion SET question='$question',venue='$venue',date='$date' WHERE qQuestionNo='$id'LIMIT 1") or die(mysql_error());
header("location: inventory.php");
exit();
}
?>
<?php
error_reporting(E_PARSE);
//Gather this questions full information and insert automatically into the edit form
if (isset($_GET['qid'])) {
$targetID = $_GET['qid'];
$sql = mysqli_query($link, "SELECT * FROM DebateQuestion WHERE qQuestionNo='$targetID'LIMIT 1") or die(mysql_error());
$questionCount = mysqli_num_rows($sql); // count the output amount
if ($questionCount > 0) {
while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)) {
$id = $row["qQuestionNo"];
$question = $row["qQuestion"];
$venue = $row["qDebateVenue"];
$date = strftime("%b %d, %Y", strtotime($row["qDate"]));
}
} else {
echo "Oops, no questions like that exists. Check <a href='inventory.php'>inventory</a>again";
exit();
}
}
?>
In your update query you have the data column without using ` back ticks , date is also a mysql's function try to wrap up your column names with back ticks if you are not sure whether they conflict with mysql's reserved keywords
$sql = mysqli_query($link,"UPDATE DebateQuestion SET
`question`='$question',`venue`='$venue',`date`='$date'
WHERE qQuestionNo='$id'LIMIT 1")
"SELECT * FROM DebateQuestion WHERE qQuestionNo='$targetID'LIMIT 1"
Here is qQuestionNo column a string type?if not remove quotes around $targetID.
Note : I have not tested the code - just read it on screen.
I've never seen an IF statement capitalized before :
If (isset($_POST['question'])) {
I'd guess this makes a difference however.
There's lots of other weird things going on in your files, but none that should give you white screen. Try lowercase 'I' in your if statement first.
ALSO - re: the UPDATE statement, you are missing a space between the $id and the LIMIT :
**qQuestionNo='$id'LIMIT 1**

PHP to see if password matches and if so, submit its data

im new to php and learning quite fast. i have a form which allows the user to update his/her security question and also requires the user to enter his/her reference number in order to go ahead with the change.
my code for the form is as follows:
<form action="securityupdated.php" method="post">
<table width="80%" border="0">
<tr>
<td><label for="secret_question">Secret Question</label></td>
<td><span id="spryselect1">
<select name="secret_question" id="secret_question">
<option selected="selected">Please Select Your Secret Question</option>
<option id="secret_question" value="What Is Your Mothers Maiden Name?">What Is Your Mothers Maiden Name</option>
<option id="secret_question" value="What Was The Name Of Your First Pet?">What Was The Name Of Your First Pet</option>
<option id="secret_question" value="What Was Your First Car?">What Was Your First Car</option>
<option id="secret_question" value="What Is Your Favourite Colour?">What Is Your Favourite Colour</option>
</select>
<span class="selectRequiredMsg">*</span></span></td>
</tr>
<tr>
<td><br /><label for="secret_anwser">Your Anwser</label></td>
<td><br /><span id="sprytextfield1">
<input type="text" name="secret_anwser" id="secret_anwser" />
<span class="textfieldRequiredMsg">*</span></span></td>
</tr>
<tr>
<td><br /><label for="password">Your Reference</label>
</td>
<td><br />
<span id="sprytextfield2">
<input type="text" name="ref" id="ref" />
<span class="textfieldRequiredMsg">*</span></span></td>
</tr>
<tr>
<td> </td>
<td><br /><input name="" type="submit" value="Update" /></td>
</tr>
</table>
</form>
my php script is as follows:
<?php
$secret_question = mysql_real_escape_string($_REQUEST['secret_question']);
$secret_anwser = mysql_real_escape_string($_REQUEST['secret_anwser']);
$sql= "UPDATE public SET secret_question = '$secret_question', secret_anwser = '$secret_anwser' WHERE active = 'activated' AND ni = '". $_SESSION['ni']."'";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
else
{
echo '<hr /><h3 align="center">Security Question Has Been Updated</h3><hr />';
}
?>
what i dont know is how to code it so that it can check if the ref entered by the user matches with my ref field in my database and if so then it should go ahead with the update and if not, it should ask the user to enter his password again?
I'm not a believer in "security questions", but i suppose adding password check would be reasonable, since if someone does get to this point, he might block the real user from requesting password restoration, if thats what you are using it for. So yep, use mysql_fetch_row to get password (I hope it is sha1 hash?) and compare it to whatever user enters (add new password field)
HTML:
<tr>
<td><label for="password_check">Your password</label></td>
<td><input type="text" name="password_check" id="password_check" /></td>
</tr>
And php:
$result = mysql_query("SELECT `password` FROM public WHERE ni = '". $_SESSION['ni']."'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
if(strlen($row[0])>0 && $row[0]==sha1($_POST['password_check'])){
.. your existing update code goes here
}
<?php
if(isset($_POST['submit'])){
$ref_number = $_POST['reference_number'];
$query = "SELECT * FROM table_name WHERE reference_number = 'ref_number'";
$result = mysql_query($query);
$output = mysql_num_rows($result);
if($output > 0){
//Proceed in registration or whatever you want to do here
}else{
echo "<script type='text/javascript'>alert('Please put your whatever again')</script>";
}
}
?>
Part of your form
<input type='text' name='reference_number'><br />
<input type='submit' name='submit' value='Submit'>;
Do you have one ref number for everyone or just a single ref?
If every user has its own ref:
$Result = mysql_query("SELECT * FROM table WHERE user_id = 'user_id' AND ref_number = '$Ref_Number'");
Or if there is only one ref number:
$Result = mysql_query("SELECT * FROM table WHERE ref_number = '$Ref_Number'");
If the ref number provided equals the one in the database, then you'll get a row back.
if (mysql_num_rows($Result) != 0)){
// your update code
} else {
// present password form
}
If you have only one ref-number for everyone it might be more interesting to just store that value in a variable.
$Query = mysql_query("SELECT * FROM table");
$R = mysql_fetch_array($Result);
$RealRef_number = $R['ref_number'];
if ($_POST['Ref_number'] == $RealRef_number){
// proceed to updating
} else {
// they were not equal..
}

Updating a table with PHP and MYSQL

<?php require("inc_connect.php"); ?>
<h1 align="center">Farris Website</h1>
<hr width="1000">
<p align="center">
<table align="center" width="1000" border="3" bordercolor="#0066FF" >
<tr>
<td align="left" valign="top">
<form name="update" method="post" action="ex_update.php?id=<?php echo urlencode($_POST['id']); ?>">
<p><strong>Enter Name:</strong>
<input type="text" name="name">
<br />
ID:
<label for="select"></label>
<select name="id">
<?php
$query = "SELECT * FROM test";
$run = mysql_query($query);
while($output = mysql_fetch_array($run)){
echo "<option value=\"{$output['id']}\">{$output['id']}</option>";}
?>
</select>
</p>
<p>
<input type="submit" name="submit" value="Update!">
</p>
</form></td>
<td width="300" align="left" valign="top"><?php include("inc_output.php"); ?></td>
</tr>
</table>
</p>
The above is the index page ...
<?php
$connect = mysql_connect("localhost","root","");
$sel_database = mysql_select_db("test");
$name = mysql_real_escape_string( $_POST["name"] );
$id = (int) $_GET['id'];
$query = "UPDATE test SET name='{$name}'";
if($run = mysql_query($query)){
header("location: index.php");
exit;
}else{mysql_error();}
?>
And this is the page that processes the form.
The problem is that the record won't update if i set the id={$_GET['id']}
and if I remove that part it updates all the rows.
So updating according to id ...
Thanks in Advance
FarrisFahad
Try changing your form action to
<form name="update" method="post" action="ex_update.php?id=<?php echo urlencode($_GET['id']); ?>">
Also, doing an echo of $query might help debug your problem.
First, just be aware of SQL Injection - your code is wide open to it. See http://bobby-tables.com/
PHP Code
<?php
$connect = mysql_connect("localhost","root","");
$sel_database = mysql_select_db("test");
$name = mysql_real_escape_string( $_POST["name"] );
$id = (int) $_GET['id'];
$query = "UPDATE test SET name='{$name}' WHERE id = {$id}";
if($run = mysql_query($query)){
header("location: index.php");
exit;
}else{
# In production, don't show raw errors to users - log them to a file and
# present the user with a generic "There was a problem with the database"
# error. Or people can start sniffing for vulnerabilities in your site.
echo mysql_error();
}
?>
Page
<?php
require("inc_connect.php");
?>
<h1 align="center">Farris Website</h1>
<hr width="1000">
<p align="center">
<table align="center" width="1000" border="3" bordercolor="#0066FF" >
<tr>
<td><form name="update" method="post" action="ex_update.php?id=<?php echo urlencode($_GET['id']); ?>">
<p><strong>Enter Name:</strong>
<input type="text" name="name"><br />
<label for="select">ID:</label>
<select name="id" id="select">
<?php
$query = "SELECT * FROM test";
$run = mysql_query($query);
while( $r = mysql_fetch_array($run) ){
# I always use short, single character, variables when in loops.
# Saves alot of characters and potential confusion.
echo " <option value='{$r['id']}'>{$r['id']}</option>\n";
}
?>
</select>
</p>
<p>
<input type="submit" name="submit" value="Update!">
</p>
</form></td>
<td><?php include("inc_output.php"); ?></td>
</tr>
</table>
</p>
As you want to update that record which is selected from your dropdown. Moreover u have set your form method to POST. So you should try following:
<form name="update" method="post" action="ex_update.php?id=<?php echo urlencode($_POST['id']); ?>">

Categories