So basically what I am trying to do is using a loop to display multiple image and create a corresponding button to each image. If I pressed on the corresponding button, it will store the image into another database.
The first part display quite well. However, the second part I can hardly figure out how to determine the corresponding button for each image.
<?php while($row=mysqli_fetch_array($result)) { ?>
<div id="item">
<?php echo '<img height="200" width="200" src="data:image;base64,'.$row[2]. '">';?>
</br>
<?php echo $row[ "name"];?>
</br>
<?php echo $row[ "price"];?>
</br>
<?php echo $row[ "description"];?>
</br>
<form>Quantity:
<input type="text" value="" name="quantity" />
</br>
<input type="submit" value="add to cart" name="cart" />
</form>
<?php
if (isset($_POST[ "cart"])){
$addtoname=$_SESSION[ 'username'];
$addtoprice=$row[ 'price'];
$addtodiscount=$row[ 'discount'];
$addtoid=$row[ 'id'];
$addtoimage=$row[ 'image'];
$addtoquantity=$_POST[ 'quantity'];
$hostname="localhost" ;
$username="root";
$password="" ;
$database="myproject" ; $con=mysqli_connect($hostname,$username,$password,$database) or die(mysqli_error());
$select=mysqli_select_db($con, "myproject")or die( "cannnot select db"); mysqli_query($con,"INSERT INTO cart(username,quantity,price,image,id,discount) VALUES('$addtoname','$addtoquantity','$addtoprice','$addtoimage','$addtoid','$addtodiscount')");
echo "success";
}
else{
echo "fail";
}?>
</div>
<?php }//end of while ?>
It seems like it always go to fail AND never run into isset($_POST[ "cart"]))
Any tips will be much appreciated.
Thank you
Create 2 files:
One with the actual form populated by the DB:
<?php while($row=mysqli_fetch_array($result)) { ?>
<div id="item">
<form method="post" action="cartReceiver.php">Quantity:
<?php echo '<img height="200" width="200" src="data:image;base64,'.$row[2]. '">';?>
</br>
<?php echo $row[ "name"];?>
</br>
<?php echo $row[ "price"];?>
</br>
<?php echo $row[ "description"];?>
</br>
<input type="text" value="" name="quantity" />
</br>
<input type="submit" value="add to cart" name="cart" />
</form>
</div>
<?php }//end of while ?>
And then the receiver of the action declared in form(cartReceiver.php):
<?php
if (isset($_POST[ "cart"])){
$addtoname=$_SESSION[ 'username'];
$addtoprice=$row[ 'price'];
$addtodiscount=$row[ 'discount'];
$addtoid=$row[ 'id'];
$addtoimage=$row[ 'image'];
$addtoquantity=$_POST[ 'quantity'];
$hostname="localhost" ;
$username="root";
$password="" ;
$database="myproject" ; $con=mysqli_connect($hostname,$username,$password,$database) or die(mysqli_error());
$select=mysqli_select_db($con, "myproject")or die( "cannnot select db"); mysqli_query($con,"INSERT INTO cart(username,quantity,price,image,id,discount) VALUES('$addtoname','$addtoquantity','$addtoprice','$addtoimage','$addtoid','$addtodiscount')");
echo "success";
}
else{
echo "fail";
}
Note that I changed the tag order of the form. If you wish to receive the fetched parameters from POST without querying the DB after transition, then you can change the first file like this:
<?php while($row=mysqli_fetch_array($result)) { ?>
<div id="item">
<form method="post" action="cartReceiver.php">Quantity:
<?php echo '<img height="200" name="image" width="200" src="data:image;base64,'.$row[2]. '">';?>
</br>
<input type="text" name="username" value="<?php echo $row[ "name"];?>" readonly />
</br>
<input type="text" name="price" value="<?php echo $row[ "price"];?>" readonly />
<input type="hidden" name="discount" value="<?php echo $row[ "discount"];?>" />
</br>
<input type="text" name="description" value="<?php echo $row[ "description"];?>" readonly />
</br>
<input type="text" value="" name="quantity" />
</br>
<input type="submit" value="add to cart" name="cart" />
</form>
</div>
<?php }//end of while ?>
So the cartReceiver.php file will look like this in this case:
<?php
if (isset($_POST[ "cart"])){
$addtoname=$_POST['username'];
$addtoprice=$_POST['price'];
$addtodiscount=$_POST['discount'];
$addtoid=$_POST['id'];
$addtoimage=$_POST['image'];
$addtoquantity=$_POST[ 'quantity'];
$hostname="localhost" ;
$username="root";
$password="" ;
$database="myproject" ; $con=mysqli_connect($hostname,$username,$password,$database) or die(mysqli_error());
$select=mysqli_select_db($con, "myproject")or die( "cannnot select db"); mysqli_query($con,"INSERT INTO cart(username,quantity,price,image,id,discount) VALUES('$addtoname','$addtoquantity','$addtoprice','$addtoimage','$addtoid','$addtodiscount')");
echo "success";
}
else{
echo "fail";
}
Notice the readonly attirbute in the inputs. It will prevent the users from altering their contents.
Now you can access all the inputs after you submit the form (click the add to cart button) by using $_POST['name'],$_POST['description'] etc
EDIT: Code updated for your needs. Since it seems you dont want to display the discount, you pass it to the form through a hidden field which can be then accessed as the others through $_POST.
First if all you need to make sure your submit button is inside a form that has the action field set to the current script. Second, I would place inside it a hidden field with the id of the image as value, so you know which image was submitted
Related
I have two php files insert.php and function.php
In insert.php
<form action="<?php echo htmlspecialchars( $_SERVER['PHP_SELF'] ); ?>"
method="post">
<input type="hidden" name="Entry" value="<?php print $data; ?>"/>
<hr>
<h3>Entry your new entry</h3>
<?php
include('function.php');
$query_start = "SHOW COLUMNS FROM $data";
$result = mysqli_query( $conn, $query_start );
while($row = mysqli_fetch_array($result)){?>
<label for="<?php echo "$row[0]";?>"><?php display_text($row[0]);?></label>
<br>
<?php display_value($row[0]);
} ?>
<input class="btn btn-primary" type="submit" name="add" value="Add Entry">
</form>
And in function.php
<?php
function display_value($row){?>
<input class='form-control' type='text' placeholder='".$row."' name='".$row."'>
}?>
But I am not getting the form on the web page.
firstly you need to add a <form> tag
<?php
include('function.php');
$query_start = "SHOW COLUMNS FROM $data";
$result = mysqli_query( $conn, $query_start );
while($row = mysqli_fetch_array($result)){
$data = display_value($row[0]);
echo $data;
}
?>
you're just calling that function, for printing the values inside the function, you need to return data from function and store or print the data from you're calling.
$data = display_value($row[0]);
here variable $data will store the output, also you need to return the result from your function
<?php
function display_value($row) {
$data = '<input class="form-control" type="text" placeholder="'. $row .'" name="'. $row .'"><br /><br />';
return $data; //this return will be printed on another page
} ?>
I guess with "form" he meant the fields not the form tag.
#Rohit Sahu answer is wrong.. you dont need to pass the output with a return. Your way of doing it is not a beautiful way. But it is even working this way.
Are you sure your Mysql Query give results?
Example Code:
<?php
// functions.php
function display_value($row) { ?>
<input class='form-control' type="text" placeholder="<?php echo $row;?>" name="<?php echo "$row";?>"><br><br>
<?php } ?>
// insert.php
<form action="....">
<?php
$arr = array(1,2,3,4);
foreach($arr as $row) {
display_value($row);
}?>
</form>
Output:
<form action="....">
<input class='form-control' type="text" placeholder="1" name="1"><br><br>
<input class='form-control' type="text" placeholder="2" name="2"><br><br>
<input class='form-control' type="text" placeholder="3" name="3"><br><br>
<input class='form-control' type="text" placeholder="4" name="4"><br><br>
</form>
Result: It is working fine. You should better post all code to understand where is your mistake. It could be some whitespaces in files, it could be no results from MYSQL.. there are many options. It is difficult to say this way.
Is it possible to update multiple records in one MySQLi query?
There are 4 records to be updated (1 for each element level) when the submit button is clicked.
The results are posted to a separate PHP page which runs the query and returns the user back to the edit page. elementid is 1,2,3,4 and corresponds with Earth, wind, fire, water. These never change (hence readonly or hidden)
<form id="edituser" name="edituser" method="post" action="applylevelchanges.php">
<fieldset>
<legend>Edit Element Level</legend>
<?php
while($userdetails->fetch())
{?>
<input name="clientid" id="clientid" type="text" size="8" value="<?php echo $clientid; ?>" hidden />
<input name="elementid" id="elementid" type="text" size="8" value="<?php echo $elementid;?>" hidden />
<input name="elemname" id="elemname" type="text" size="15" value="<?php echo $elemname; ?>" readonly />
<input name="elemlevel" id="elemlevel" type="text" size="8" required value="<?php echo $elemlevel; ?>" /></br>
</br>
<?php }?>
</fieldset>
<button type="submit">Edit Student Levels</button>
</form>
And the code to apply the changes
<?php
if (isset($_POST['clientid']) && isset($_POST['elementid']) && isset($_POST['elemname']) && isset($_POST['elemlevel'])) {
$db = createConnection();
$clientid = $_POST['clientid'];
$elementid = $_POST['elementid'];
$elemname = $_POST['elemname'];
$elemlevel = $_POST['elemlevel'];
$updatesql = "update stuelement set elemlevel=? where clientid=? and elementid=?";
$doupdate = $db->prepare($updatesql);
$doupdate->bind_param("iii", $elemlevel, $clientid, $elementid);
$doupdate->execute();
$doupdate->close();
$db->close();
header("location: edituserlevel.php");
exit;
} else {
echo "<p>Some parameters are missing, cannot update database</p>";
}
The idea is the page in which you select a category from drop-down menu, then after you click remove button, new form shows(contains yes/no buttons) that asks you if you really want to remove selected category. Problem is the second yes/no script. It works on separate page, but on page with first form it doesn't echo anything nor does it remove a pet. Please help, thanks!
<?php
/*remove a category*/
include("connection.php");
?>
<html><head></head>
<body>
<?php
$PetListquery= "SELECT distinct petType From petType ORDER by petType";
$PetListResult= mysqli_query($cxn,$PetListquery) or die ("Couldn't execute query.");
?>
<div style="border:2px solid;">
<form method="POST" action="removeCategory.php">
<div align='left'>
Choose category you want to remove:
<select name='petType'>
<option value='-1'>Type:</option>
<?php
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
?>
<option value='<?php echo $petType;?>' ><?php echo $petType;?> </option>
<?php }?>
</select>
</div>
<div>
<p><input type='submit' name='Remove' value='Remove Category' />
</div>
</div>
</form>
<?php
foreach($_POST as $field => $value)
{ //second form starts after if
if($field == 'petType')
{
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
<div>
<input name="Yes" type="submit" value="Yes">
<input name="No" type="submit" value="No">
</div>
</form>
<?php
echo "Are you sure you want to delete selected category?";
//clicking any of these buttons doesn't display anything
if(isset($_POST['Yes']))
{
echo "yes";
$DeleteQuery= "DELETE From petType WHERE petType='$petType'";
$DeleteResult= mysqli_query($cxn,$DeleteQuery) or die ("Error1!");
}
if(isset($_POST['No']))
{
echo "No!";
}
}
}
?>
</body></html>
I can't get my update checkbox function to work. I need to be able to remove or add a value which I have chosen to call checked in to my table. The code looks like following.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name <input type"text" name="inputName" value="<?php echo $hemsida['Namn']; ?>" /> </br>
Commentar <input type"text" name="inputComment" value="<?php echo $hemsida['Comment']; ?>" />
<br/>
</br><input type="checkbox" name="all" value="<?php echo $hemsida['All']; ?>"
<?php if($hemsida['All'] == 'checked') echo " checked"; ?> /> Alla
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Redigera">
</form>
and the Update PHP looks like this
if(isset($_POST['submit'])) {
$all = ($_POST['All'] == 1) ? "checked" : "";
$u = "UPDATE hemsida SET `Namn`='$_POST[inputName]', `Comment`='$_POST[inputComment]', `ALL`=$all WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modified";
header("Location: ..//sokh.php");
}
The error is Undefined variable: hemsida on all parts. And also You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ID = 33' at line 1. But I have no problem getting the data in to the Modifier or what I should call it but unable to get it out.
ANSWER !!!
I got it to work but cant answer my own question so i write it down here , i added and removed code until everything broke down. Remove the "$all = ($_POST['All'] == 1) ? checked : ;" part and now it works. I will copy the code underneath it there is an interest
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name <input type"text" name="inputName" value="<?php echo $hemsida['Namn']; ?>" /> </br>
Commentar <input type"text" name="inputComment" value="<?php echo $hemsida['Comment']; ?>" />
<br/>
<input type="checkbox" name="all" value="checked" <?php if($hemsida['All'] == 'checked') echo "checked=\"checked\""; ?>/> Alla
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Redigera">
</form>
the new php
if(isset($_POST['submit'])) {
$u = "UPDATE hemsida SET `Comment`='$_POST[inputComment]', `Namn`='$_POST[inputName]', `All`='$_POST[all]' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modified";
header("Location: ..//sokh.php");
}
It seems that $hemsida is not available at that point in your code, but you are using it.
An input type="checkbox" does notshow up in $_POST when it is not checked. , use isset.
sidenote: you're using XHTML? the correct checked type = checked="checked"
how to display the result after submit the form
i want a display result after submit the form for print
example 1st im filling the form submit the result after the submit i want a screen to display same result
http://www.tizag.com/phpT/examples/formexample.php
how can i do this
please help me to fix this issue.
php form code
<?php
function renderForm($grn, $name, $rollno, $class, $fees, $date, $reference, $error)
{
?>
<?php
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<div>
<p><span class="style9"><strong>G.R.N No:</strong></span><strong> *</strong>
<input name="grn" type="text" id="grn" value="<?php echo $grn; ?>" size="50" />
</p>
<p><span class="style9"><strong>Name:</strong></span><strong> *</strong>
<input name="name" type="text" id="name" value="<?php echo $name; ?>" size="50" />
</p>
<p><span class="style9"><strong>Roll No :</strong></span><strong> *</strong>
<input name="rollno" type="text" id="rollno" value="<?php echo $rollno; ?>" size="50" />
</p>
<p><span class="style9"><strong>Class:</strong></span><strong> *</strong>
<input name="class" type="text" id="class" value="<?php echo $class; ?>" size="50" />
</p>
<p><span class="style9"><strong>Fees Date :</strong></span><strong> *</strong>
<input id="fullDate" name="date" type="text" value="<?php echo $date; ?>" size="50" />
</p>
<p><span class="style9"><strong>Fees :</strong></span><strong> *</strong>
<input name="fees" type="text" value="<?php echo $fees; ?>" size="50" />
</p>
<span class="style9"><strong>Reference</strong></span><strong> *</strong>
<input name="reference" type="text" value="<?php echo $reference; ?>" size="50">
<br/>
<p class="style1">* required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
<?php
}
include('connect-db.php');
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$grn = mysql_real_escape_string(htmlspecialchars($_POST['grn']));
$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$rollno = mysql_real_escape_string(htmlspecialchars($_POST['rollno']));
$class = mysql_real_escape_string(htmlspecialchars($_POST['class']));
$fees = mysql_real_escape_string(htmlspecialchars($_POST['fees']));
$date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
$reference = mysql_real_escape_string(htmlspecialchars($_POST['reference']));
// check to make sure both fields are entered
if ($grn == '' || $name == '' || $rollno == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($grn, $name, $rollno, $class, $fees, $date, $reference, $error);
}
else
{
// save the data to the database
mysql_query("INSERT fees SET grn='$grn', name='$name', rollno='$rollno', class='$class', fees='$fees', date='$date', reference='$reference'")
or die(mysql_error());
echo "<center>KeyWord Submitted!</center>";
// once saved, redirect back to the view page
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','','','','','','');
}
?>
Not quite shure what you are asking.
do you need help displaying the submited form data? or more spesific display for print?
to display it you would just need to make a html page that displays it.
like:
echo '<table><tr>';
echo '<td>';
echo '<Strong>Name:</strong><br/>';
echo $name;
echo '</td>';
echo '</tr></table>';
To display just the result and not the form, when you post to the same page you need to encapsulate the for code with an if statement.
if(isset($_POST['submit'])) {
//code for the php form
}else {
//code to display form
}
Use a new page for the "action" part in the form. On that new page, simply echo the $_POST of values you want to display. If you plan on making some sort of page so that people can check their entries, you could store these $_POST-data in sessions.
Simply call function on the bottom to disply posted values :
function showFormValues()
{
?>
<div>
<p><span class="style9"><strong>G.R.N No:</strong></span><strong> *</strong>
<?php echo $_POST['grn']; ?>
</p>
<p><span class="style9"><strong>Name:</strong></span><strong> *</strong>
<?php echo $_POST['name'];
</p>
and so on.
.
.
.
.
</div>
<?php
}
?>