I've the following code :
$query = "SELECT * FROM items WHERE SUBSTRING(item_no, 1, ".$length.") BETWEEN
'".$from_new."' AND '".$to_new."' ORDER BY item_no Desc";
$result = mysql_query($query);
$dd=array();
$ii=array();
$qq=array();
$aa=array();
if(mysql_num_rows($result)>0){
$num = mysql_num_rows($result);
?>
<form method="post" action="final_group_items.php">
<?php
echo "<table>";
for($i=0;$i<$num;$i++){
$row = mysql_fetch_array($result);
echo "<tr><td align=center>"; ?>
<input disabled maxlength="2" type="text"
name="ii[]" value="<?php echo strtoupper($row['item_no']); ?>"><?php echo
"</td><td align=center>";?>
<input disabled maxlength="2" type="text"
name="qq[]" value="<?php echo $row['qty'];?>">
<?php echo "</td><td align=center>"; ?>
<input disabled maxlength="2" type="text"
name="aa[]" value="<?php echo $row['actual_price'];?>">
<?php echo "</td><td align=center>";?>
<input required maxlength="2" type="text" name="dd[]" value="<?php echo
$row['discount_price']; ?>">
<?php
echo "</td><tr>";
}
echo "</table>";
?>
<input type="submit" value="Change Values">
</form>
Now when i click Submit it will open the final_group_items.php which has the follow testing code to make sure if all array (ii,qq,dd,aa) are not empty:
if(empty($_POST['qq']))
{
echo "No value inside";
return false;
}
foreach($_POST['qq'] as $test)
{
echo $test;
}
return true;
so by testing all array's, the only one works is $_POST['dd']...Others outputs "no value inside" which I really don't know how or why?
What should I do when I have multiple of fields having uniqe arrays and values.
Thank You
This:
<input disabled maxlength="2" type="text" name="qq[]" value="<?php echo $row['qty'];?>">
Will prevent the browser from even posting the field at form submission, because disabled gets an implicit value of "disabled".
The attribute should be removed:
<input maxlength="2" type="text" name="qq[]" value="<?php echo $row['qty'];?>">
Related
I am trying to use PHP to update an SQL table using HTML forms.
I want the user to be able to search for a VCR name and display it details in a while loop and then an update form will appear for the user to change its details in the database.
However every time i press the update button on the update form, the variables that hold the new details empty and become undefined.
<?php require('connect.php'); ?>
<?php require('headerPrivate.php'); ?>
<?php require('session.php');?>
<?php
//SEARCH PHP CODE
//THIS WORKS FINE AND ALL THE DETAILS APPEAR
if(isset($_POST["search"]))
{
//CREATE VARIABLES
$username=$_SESSION['username'];
echo "username: ".$username;
echo '<br>';
$vcrName=$_POST['name'];
echo "VCR Name: ".$vcrName;
echo '<br>';
echo '<br>';
//SELECT * FROM PRODUCT
$sql="SELECT *
FROM product
INNER JOIN user
ON product.owner_ID=user.user_ID
WHERE username='$username' AND name='$vcrName'";
echo "SQL SELECT 1: ".$sql;
echo '<br>';
echo '<br>';
//$vcrName=$_POST['name'];
$result = mysqli_query($con,$sql);
echo '<div class="row">';
echo '<div class="col-xs-6 col-md-4">';
while ($row_all = mysqli_fetch_assoc($result))
{
echo '<form method="post">';
echo "<u>Title: ".$row_all["name"].'</u>';
echo '<br>';
echo '<small>';
echo " Price: ".$row_all["price"];
echo '</small>';
echo '<br>';
echo "<p><u>Short Description:</u> ".$row_all["short_descripton"]."</p>";
echo '<br>';
echo "<p><u>Long Description:</u> ".$row_all["long_description"]."</p>";
echo '<br>';
echo '<hr>';
echo '</form>';
echo '<div>';
}
echo '</div>';
}
?>
<content>
<!--SEARCH FOR VCR NAME-->
<form class="form" method="post">
<label for="name" class="sr-only">VCR Name</label>
<input type="text" name="name" class="form-control" placeholder="VCR Name" required="" autofocus="" autocomplete="off">
<button name="search" type="search" class="btn btn-success btn-block">Search</button>
</form>
<?php
//This is where i run into issues. The old name in the variable $vcrName is empty and i need it for the update SQL statement.
//UPDATE PHP
if(isset($_POST["alter"]))
{
//CREATE A SESSION VARIABLE FOR THE CUSTOMER ID
$customer_ID=$_SESSION['customer_ID'];
echo "Customer ID: ".$customer_ID;
echo '<br>';
//CREATE VARIABLES
$changeTitle=$_POST["titleChange"];
$changesDescChange=$_POST["sDescChange"];
$changelDescChange=$_POST["lDescChange"];
$changepriceChange=$_POST["priceChange"];
$vcrName=$_POST['name'];
//UPDATE SQL
$sql_update="UPDATE product
SET
name='$changeTitle',
short_descripton='$changesDescChange',
long_description='$changelDescChange',
price='$changepriceChange'
WHERE
owner_ID='$customer_ID' AND name='$vcrName'";
echo "SQL Update 0: ".$sql_update;
echo '<br>';
echo '<br>';
echo "Updated Name: ".$changeTitle;
echo '<br>';
echo '<br>';
echo "SQL Update 1: ".$sql_update;
return $sql_update;
echo '<br>';
echo '<br>';
$result_update = mysqli_query($con,$sql_update);
if($result_update){
echo "Update Successful!";
}
else {
echo "Update Unsuccessful";
}
}
?>
<!--UPDATE FORM-->
<form class="form" method="post">
<label for="titleChange" class="sr-only">VCR Name</label>
<input type="text" name="titleChange" class="form-control" placeholder="VCR Name" required="" autofocus="" autocomplete="off">
<label for="sDescChange" class="sr-only">Short Description</label>
<input type="text" name="sDescChange" class="form-control" placeholder="Short Description" required="" autofocus="" autocomplete="off">
<label for="lDescChange" class="sr-only">Long Description</label>
<input type="text" name="lDescChange" class="form-control" placeholder="Long Description" required="" autofocus="" autocomplete="off">
<label for="priceChange" class="sr-only">Price</label>
<input type="text" name="priceChange" class="form-control" placeholder="Price" required="" autofocus="" autocomplete="off">
<button name="alter" type="submit">Change</button>
</form>
</content>
</body>
</html>
Your PHP code refers to a POST variable that doesn't exist:
$vcrName=$_POST['name'];
In your update form, you need to pass it as a hidden value:
<input type="hidden" name="name" value="<?=htmlspecialchars($vcrName)?>"/>
It's not clear if these are two separate PHP scripts (or if so, why they are) so you may need to put in a database call to get that value. From a user interface point of view, one is typically given the existing values when updating a record anyway. This would mean getting the data and giving each form element a value attribute.
I was making a form inside a PHP script and when i tried to access a field
of form in the same script it was giving me error Undefined index
This is my code:
<?php
$query="SELECT * FROM `vendor_list`";
$query_run=mysql_query($query);
if(mysql_num_rows($query_run)>0)
{
echo '<form method="POST">';
echo 'Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>';
echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';
echo '<select name="vendor_email">';
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
echo '</select><br>';
echo '<input type="submit" value="submit">';
echo '</form>';
}
else
{
echo 'No vendor to display';
}
echo $_POST['book_name'];
USE THIS:-
<?php
$query="SELECT * FROM `vendor_list`";
$query_run=mysql_query($query);
if(mysql_num_rows($query_run)>0)
{
echo '<form method="POST">';
echo 'Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>';
echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';
echo '<select name="vendor_email">';
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
echo '</select><br>';
echo '<input type="submit" value="submit">';
echo '</form>';
}
else
{
echo 'No vendor to display';
}
if($_POST){
echo $_POST['book_name'];
}
?>
Try this
<?php
$query="SELECT * FROM `vendor_list`";
$query_run=mysql_query($query);
if(mysql_num_rows($query_run)>0)
{
?>
<form method="POST" action="">
Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>
Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>
<select name="vendor_email">
<?php
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
?>
</select><br>
<input type="submit" name="submit" value="submit">
</form>
<?php
}
else
{
echo 'No vendor to display';
}
if(isset($_POST["submit"]))){
echo $_POST['book_name'];
}
?>
The Clean Way
All the breaks you have in that code are a hot mess...
Tip When you are echoing too much HTML just wrap it in an if..else.. like this...
<?php
$query = "SELECT * FROM `vendor_list`";
$query_run = mysql_query($query);
?>
<?php if(mysql_num_rows($query_run)>0): ?>
<form method="POST">
<label for="book_name">Book name:</label>
<input type="text" name="book_name" id="book_name" size="40" maxlength="40">
<label for="book_name"Number Of Copies:</label>
<input type="text" size="5" name="num_copies" maxlength="2">
<select name="vendor_email">
<?php while($query_row=mysql_fetch_assoc($query_run)): ?>
<option value="<?=$query_row['email']?>"><?=$query_row['vendor_name']?></option>
<?php endwhile; ?>
</select>
<input type="submit" value="submit">
</form>
<?php else: ?>
<p>No vendor to display</p>
<?php endif; ?>
<?php if (isset($_POST['book_name'])) echo $_POST['book_name']; ?>
Using the below code this will also check if you have a form submission or not, unless this code is a cut/copy paste.. It looks like it will process sections when it should not.. eg the echo post..
Personally i myself will set the action="" to PHP_SELF as many years ago had issues with it, soon this will change once i start using HTML5 when its cross browser enough / older users update their browsers.
Example of processing or showing a post but not both.
echo $_POST['book_name'];
echo "POST Dump: " . print_r($_POST, true);
} else {
// No Form Submission, So we generate the form instead
// QUERY - data
$query = "SELECT * FROM 'vendor_list'";
$query_run = mysql_query($query);
if(mysql_num_rows($query_run)>0) {
// FORM - Name/#Copies
echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
echo 'Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>';
echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';
// FORM - Select Vendor
echo '<select name="vendor_email">';
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
echo '</select><br>';
// FORM - Submit / Close Form
echo '<input type="submit" value="submit">';
echo '</form>';
} else {
echo 'No vendor to display';
}
}
?>
Or using a code that shows the form and processing any $_POST
<?php
// QUERY - data
$query = "SELECT * FROM 'vendor_list'";
$query_run = mysql_query($query);
if(mysql_num_rows($query_run)>0) {
// FORM - Name/#Copies
echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
echo 'Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>';
echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';
// FORM - Select Vendor
echo '<select name="vendor_email">';
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
echo '</select><br>';
// FORM - Submit / Close Form
echo '<input type="submit" value="submit">';
echo '</form>';
} else {
echo 'No vendor to display';
}
if (isset($_POST['book_name'])) {
echo "<b>Displaying post data</b> <br />";
echo "Example book: " . $_POST['book_name'] . "<br />";
echo "POST Dump: <br /><pre>" . print_r($_POST, true) . "</pre>";
} else {
echo "Page has not been subitted yet, please select an item";
}
?>
I'm making a site where the owner has to be able to update their events, but my update code isnt working even though im 99% sure I havent made any errors.
First the form where you press update:
<?php
$sql = "SELECT * FROM events ORDER BY id ASC";
$res = $objCon->query($sql) or die('fejl i query:'.mysqli_error($objCon));
while($row=$res->fetch_array()) {
$id = $row['id'];
echo "<div class='eventpost'>";
echo "<div class='dato'>";
echo $row['id'];
echo "</div>";
echo "<p class='overskrift'>";
echo "<a href='update.php?id=$id'>RET </a>";
echo "<a href='code_delete.php?id=$id'>SLET</a>";
echo $row['overskrift'];
echo "</p>";
echo "</div>";
}
?>
then the update form:
<form action="code_update.php" method="POST">
<label>Dato:<br>
<input type="text" name="dag" value="<?php echo $data['dag']; ?>"></label>
<label>MÃ¥nede:<br>
<input type="text" name="month" value="<?php echo $data['month']; ?>"></label>
<label>Overskrift:<br>
<input type="text" name="overskrift" value="<?php echo $data['overskrift']; ?>"></label>
<label>Tekst:<br>
<input type="text" name="tekst" value="<?php echo $data['tekst']; ?>"></label>
<input type="hidden" name="id" value="<? echo $id; ?>">
<input type="submit" value="Opret">
</form>
and finally the update code
<?php
session_start();
if($_SESSION['auth'] == 2){
include('incl_db.php');
$id = $_POST['id'];
$overskrift = $_POST['overskrift'];
$dag = $_POST['dag'];
$month = $_POST['month'];
$tekst = $_POST['tekst'];
$sql = "UPDATE events SET overskrift='$overskrift', dag='$dag', month='$month', tekst='$tekst' WHERE id='$id'";
$res = $objCon->query($sql);
header('location:events.php');
}else{
header('location:index.php');
}
?>
Probably shorthand tags are disabled in your php version.So try changing this
<input type="hidden" name="id" value="<? echo $id; ?>">
to this
<input type="hidden" name="id" value="<?php echo $id; ?>">
You can check this answer for more.
Hi there I'm quite new to PHP
I have this problem:
I would like to POST a multiple choice + a hidden field from a form:
<?php
if (isset($_SESSION['nickname']))
{
$result = mysql_query("SELECT * FROM users");
$teamsCount = ceil(mysql_num_rows($result)/2);
for ($i=1; $i<=$teamsCount; $i++)
{
// TEST: echo $i . " TeamsCount er: " . $teamsCount. "<br>";
?>
Team <? echo $i; ?>
<form name="addTeam" action="buildTeams.php" method="POST">
<input type="hidden" name="hiddenField" value="<?php $i; ?>" />
<select name="teams[]" multiple="multiple" size="<?php echo mysql_num_rows($result); ?>">
<?php
$query = mysql_query("SELECT * FROM users");
while ($row=mysql_fetch_array($query))
{
$id=$row["ID"];
$nick=$row["Nick"];
?>
<option value="<?php echo $id; ?>"><?php echo ucfirst($nick); ?></option>
<?php
}
?>
</select>
<input type="submit" value="Make them teams!!" />
</form>
<?php
}
}
?>
I think you have an error in this line:
<input type="hidden" name="hiddenField" value="<?php $i ?>" />
It should be
<input type="hidden" name="hiddenField" value="<?php echo $i ?>" />
Edit:
Put the team id in the select name. Example:
<select name="teams[<?=$i?>][]">
And in PHP do:
foreach ($_POST['teams'] as $team_id => $choices)
I think you should check $_POST['hiddenField'] to obtain hidden value
I am trying to create a quiz with PHP/Mysql...
I have created a form with radio buttons for answers which displays data pulled from the database as values for the radio buttons. I tried to submit the form but the result page does not show anything.
My quiz code goes as follows:
<form method="post" action="insertscore.php" name="cssCheckbox" id = "cssCheckbox">
<?php $query = "SELECT * FROM questions WHERE (`topics` = '.NET' OR `topics` = 'PHP') ORDER BY Rand() LIMIT 5"; $result = mysql_query($query);
if ($result && mysql_num_rows($result)) {
$numrows = mysql_num_rows($result);
$count =1;
while ($row = mysql_fetch_array($result))
{
?>
<div class="group">
<input type="hidden" name="<?php echo $row['key_id']; ?>"><?php $row['key_id']; ?></input>
<span class="test_question"><strong><?php echo $count;?>) <?php echo $row['question']; ?>
</strong><br />
<?php if($row['answer1'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer1']; ?>" id="chkLimit_1" ></input>
<label for="chkLimit_1" ><?php echo $row['answer1']; echo "<br />"; } else {} ?></label>
<?php if($row['answer2'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer2']; ?>" id="chkLimit_2" ></input>
<label for="chkLimit_2" ><?php echo $row['answer2']; echo "<br />"; } else {} ?></label>
<?php if($row['answer3'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer3']; ?>" id="chkLimit_3" ></input>
<label for="chkLimit_3" ><?php echo $row['answer3']; echo "<br />"; } else {} ?></label>
<?php if($row['answer4'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer4']; ?>" id="chkLimit_4" ></input>
<label for="chkLimit_4" ><?php echo $row['answer4']; echo "<br />"; } else {} ?></label>
<?php if($row['answer5'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer5']; ?>" id="chkLimit_5" ></input>
<label for="chkLimit_5" ><?php echo $row['answer5']; echo "<br />"; } else {} ? ></label>
<?php if($row['answer6'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer6']; ?>" id="chkLimit_6" ></input>
<label for="chkLimit_6" ><?php echo $row['answer6']; echo "<br />"; } else {} ?></label>
<?php if($row['answer7'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer7']; ?>" id="chkLimit_7" ></input>
<label for="chkLimit_7" ><?php echo $row['answer7']; echo "<br />"; } else {} ?></label>
<?php if($row['answer8'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer8']; ?>" id="chkLimit_8" ></input>
<label for="chkLimit_8" ><?php echo $row['answer8']; echo "<br />"; } else {} ?></label>
<input type="hidden" name="<?php echo $row['right_answer']; ?>"><?php $row['right_answer']; ?></input>
</div>
<input name="Submit" type="submit" value="Submit Your Answers" class="submit">
</form>
Code on submitted Page looks like:
<?php
if(isset($_POST['Submit'])){
$key_id=$_POST['key_id']; echo $key_id;
$question=$_POST['question']; echo $question;
$answers=$_POST['answers']; echo $answers;
$correctanswer=$_POST['correctanswer']; echo $correctanswer;
}
foreach($_POST as $key => $val)
{
echo "$key --> $val<br />";
}
//var_dump($_POST);
?>
Please let me know if anything is not clear or if I am missing anything....
Thanks,
Shank
I would:
remove comments to //var_dump($_POST); and move this line at the top of the code on submitted Page.
if you still don't see anything, I think the code on submitted page is not in a file called insertscore.php or such file is not in same folder of your form page.